conditions.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package migrator
  2. type MigrationCondition interface {
  3. Sql(dialect Dialect) (string, []interface{})
  4. IsFulfilled(results []map[string][]byte) bool
  5. }
  6. type ExistsMigrationCondition struct{}
  7. func (c *ExistsMigrationCondition) IsFulfilled(results []map[string][]byte) bool {
  8. return len(results) >= 1
  9. }
  10. type NotExistsMigrationCondition struct{}
  11. func (c *NotExistsMigrationCondition) IsFulfilled(results []map[string][]byte) bool {
  12. return len(results) == 0
  13. }
  14. type IfIndexExistsCondition struct {
  15. ExistsMigrationCondition
  16. TableName string
  17. IndexName string
  18. }
  19. func (c *IfIndexExistsCondition) Sql(dialect Dialect) (string, []interface{}) {
  20. return dialect.IndexCheckSql(c.TableName, c.IndexName)
  21. }
  22. type IfIndexNotExistsCondition struct {
  23. NotExistsMigrationCondition
  24. TableName string
  25. IndexName string
  26. }
  27. func (c *IfIndexNotExistsCondition) Sql(dialect Dialect) (string, []interface{}) {
  28. return dialect.IndexCheckSql(c.TableName, c.IndexName)
  29. }
  30. type IfColumnNotExistsCondition struct {
  31. NotExistsMigrationCondition
  32. TableName string
  33. ColumnName string
  34. }
  35. func (c *IfColumnNotExistsCondition) Sql(dialect Dialect) (string, []interface{}) {
  36. return dialect.ColumnCheckSql(c.TableName, c.ColumnName)
  37. }