|
@@ -13,6 +13,8 @@ const (
|
|
|
|
|
|
|
|
type Migration interface {
|
|
type Migration interface {
|
|
|
Sql(dialect Dialect) string
|
|
Sql(dialect Dialect) string
|
|
|
|
|
+ Id() string
|
|
|
|
|
+ SetId(string)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type ColumnType string
|
|
type ColumnType string
|
|
@@ -22,7 +24,15 @@ const (
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
type MigrationBase struct {
|
|
type MigrationBase struct {
|
|
|
- desc string
|
|
|
|
|
|
|
+ id string
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (m *MigrationBase) Id() string {
|
|
|
|
|
+ return m.id
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (m *MigrationBase) SetId(id string) {
|
|
|
|
|
+ m.id = id
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type RawSqlMigration struct {
|
|
type RawSqlMigration struct {
|
|
@@ -53,11 +63,6 @@ func (m *RawSqlMigration) Mysql(sql string) *RawSqlMigration {
|
|
|
return m
|
|
return m
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (m *RawSqlMigration) Desc(desc string) *RawSqlMigration {
|
|
|
|
|
- m.desc = desc
|
|
|
|
|
- return m
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
type AddColumnMigration struct {
|
|
type AddColumnMigration struct {
|
|
|
MigrationBase
|
|
MigrationBase
|
|
|
tableName string
|
|
tableName string
|
|
@@ -90,16 +95,12 @@ func (m *AddColumnMigration) Sql(dialect Dialect) string {
|
|
|
return fmt.Sprintf("ALTER TABLE %s ADD COLUMN %s %s", m.tableName, m.columnName, dialect.ToDBTypeSql(m.columnType, m.length))
|
|
return fmt.Sprintf("ALTER TABLE %s ADD COLUMN %s %s", m.tableName, m.columnName, dialect.ToDBTypeSql(m.columnType, m.length))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (m *AddColumnMigration) Desc(desc string) *AddColumnMigration {
|
|
|
|
|
- m.desc = desc
|
|
|
|
|
- return m
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
type AddIndexMigration struct {
|
|
type AddIndexMigration struct {
|
|
|
MigrationBase
|
|
MigrationBase
|
|
|
tableName string
|
|
tableName string
|
|
|
columns string
|
|
columns string
|
|
|
indexName string
|
|
indexName string
|
|
|
|
|
+ unique string
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (m *AddIndexMigration) Name(name string) *AddIndexMigration {
|
|
func (m *AddIndexMigration) Name(name string) *AddIndexMigration {
|
|
@@ -112,11 +113,16 @@ func (m *AddIndexMigration) Table(tableName string) *AddIndexMigration {
|
|
|
return m
|
|
return m
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func (m *AddIndexMigration) Unique() *AddIndexMigration {
|
|
|
|
|
+ m.unique = "UNIQUE"
|
|
|
|
|
+ return m
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func (m *AddIndexMigration) Columns(columns ...string) *AddIndexMigration {
|
|
func (m *AddIndexMigration) Columns(columns ...string) *AddIndexMigration {
|
|
|
m.columns = strings.Join(columns, ",")
|
|
m.columns = strings.Join(columns, ",")
|
|
|
return m
|
|
return m
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (m *AddIndexMigration) Sql(dialect Dialect) string {
|
|
func (m *AddIndexMigration) Sql(dialect Dialect) string {
|
|
|
- return fmt.Sprintf("CREATE UNIQUE INDEX %s ON %s(%s)", m.indexName, m.tableName, m.columns)
|
|
|
|
|
|
|
+ return fmt.Sprintf("CREATE %s INDEX %s ON %s(%s)", m.unique, m.indexName, m.tableName, m.columns)
|
|
|
}
|
|
}
|