dialect/sql/schema: add prepare option to mysql dialect

This commit is contained in:
Ariel Mashraki
2020-01-01 19:11:20 +02:00
parent ab732c7654
commit 7fc3689027
4 changed files with 192 additions and 4 deletions

View File

@@ -179,10 +179,15 @@ func (m *Migrate) create(ctx context.Context, tx dialect.Tx, tables ...*Table) e
// apply applies changes on the given table.
func (m *Migrate) apply(ctx context.Context, tx dialect.Tx, table string, change *changes) error {
// constraints should be dropped before dropping columns, because if a column
// Constraints should be dropped before dropping columns, because if a column
// is a part of multi-column constraints (like, unique index), ALTER TABLE
// might fail if the intermediate state violates the constraints.
if m.dropIndexes {
if pr, ok := m.sqlDialect.(preparer); ok {
if err := pr.prepare(ctx, tx, change, table); err != nil {
return err
}
}
for _, idx := range change.index.drop {
if err := m.dropIndex(ctx, tx, idx, table); err != nil {
return fmt.Errorf("drop index of table %q: %v", table, err)
@@ -232,6 +237,16 @@ type changes struct {
}
}
// dropColumn returns the dropped column by name (if any).
func (c *changes) dropColumn(name string) (*Column, bool) {
for _, col := range c.column.drop {
if col.Name == name {
return col, true
}
}
return nil, false
}
// changeSet returns a changes object to be applied on existing table.
// It fails if one of the changes is invalid.
func (m *Migrate) changeSet(curr, new *Table) (*changes, error) {
@@ -444,3 +459,7 @@ type sqlDialect interface {
alterColumn(*Column) []*sql.ColumnBuilder
addIndex(*Index, string) *sql.IndexBuilder
}
type preparer interface {
prepare(context.Context, dialect.Tx, *changes, string) error
}