mirror of
https://github.com/ent/ent.git
synced 2026-05-04 08:30:57 +03:00
dialect/sql/schema: add prepare option to mysql dialect
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -227,6 +227,55 @@ func (d *MySQL) dropIndex(ctx context.Context, tx dialect.Tx, idx *Index, table
|
||||
return tx.Exec(ctx, query, args, new(sql.Result))
|
||||
}
|
||||
|
||||
// prepare runs preparation work that needs to be done to apply the change-set.
|
||||
func (d *MySQL) prepare(ctx context.Context, tx dialect.Tx, change *changes, table string) error {
|
||||
for _, idx := range change.index.drop {
|
||||
switch n := len(idx.columns); {
|
||||
case n == 0:
|
||||
return fmt.Errorf("index %q has no columns", idx.Name)
|
||||
case n > 1:
|
||||
continue // not a foreign-key index.
|
||||
}
|
||||
var qr sql.Querier
|
||||
Switch:
|
||||
switch col, ok := change.dropColumn(idx.columns[0]); {
|
||||
// If both the index and the column need to be dropped, the foreign-key
|
||||
// constraint that is associated with them need to be dropped as well.
|
||||
case ok:
|
||||
names, err := fkNames(ctx, tx, table, col.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(names) == 1 {
|
||||
qr = sql.AlterTable(table).DropForeignKey(names[0])
|
||||
}
|
||||
// If the uniqueness was dropped from a foreign-key column,
|
||||
// create a "simple index" if no other index exist for it.
|
||||
case !ok && idx.Unique && len(idx.Columns) > 0:
|
||||
col := idx.Columns[0]
|
||||
for _, idx2 := range col.indexes {
|
||||
if idx2 != idx && len(idx2.columns) == 1 {
|
||||
break Switch
|
||||
}
|
||||
}
|
||||
names, err := fkNames(ctx, tx, table, col.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(names) == 1 {
|
||||
qr = sql.CreateIndex(names[0]).Table(table).Columns(col.Name)
|
||||
}
|
||||
}
|
||||
if qr != nil {
|
||||
query, args := qr.Query()
|
||||
if err := tx.Exec(ctx, query, args, new(sql.Result)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// scanColumn scans the column information from MySQL column description.
|
||||
func (d *MySQL) scanColumn(c *Column, rows *sql.Rows) error {
|
||||
var (
|
||||
@@ -360,3 +409,28 @@ func (d *MySQL) scanIndexes(rows *sql.Rows) (Indexes, error) {
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
|
||||
// fkNames returns the foreign-key names of a column.
|
||||
func fkNames(ctx context.Context, tx dialect.Tx, table, column string) ([]string, error) {
|
||||
query, args := sql.Select("CONSTRAINT_NAME").From(sql.Table("INFORMATION_SCHEMA.KEY_COLUMN_USAGE").Unquote()).
|
||||
Where(sql.
|
||||
EQ("TABLE_NAME", table).
|
||||
And().EQ("COLUMN_NAME", column).
|
||||
// NULL for unique and primary-key constraints.
|
||||
And().NotNull("POSITION_IN_UNIQUE_CONSTRAINT").
|
||||
And().EQ("TABLE_SCHEMA", sql.Raw("(SELECT DATABASE())")),
|
||||
).
|
||||
Query()
|
||||
var (
|
||||
names []string
|
||||
rows = &sql.Rows{}
|
||||
)
|
||||
if err := tx.Query(ctx, query, args, rows); err != nil {
|
||||
return nil, fmt.Errorf("mysql: reading constraint names %v", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
if err := sql.ScanSlice(rows, &names); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
@@ -702,6 +702,10 @@ func TestMySQL_Create(t *testing.T) {
|
||||
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "non_unique", "seq_in_index"}).
|
||||
AddRow("PRIMARY", "id", "0", "1").
|
||||
AddRow("age", "age", "0", "1"))
|
||||
// check if a foreign-key needs to be dropped.
|
||||
mock.ExpectQuery(escape("SELECT `CONSTRAINT_NAME` FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE `TABLE_NAME` = ? AND `COLUMN_NAME` = ? AND `POSITION_IN_UNIQUE_CONSTRAINT` IS NOT NULL AND `TABLE_SCHEMA` = (SELECT DATABASE())")).
|
||||
WithArgs("users", "age").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"CONSTRAINT_NAME"}))
|
||||
// drop the unique index.
|
||||
mock.ExpectExec(escape("DROP INDEX `age` ON `users`")).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
@@ -758,6 +762,97 @@ func TestMySQL_Create(t *testing.T) {
|
||||
mock.ExpectCommit()
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "drop foreign key with column and index",
|
||||
tables: []*Table{
|
||||
{
|
||||
Name: "users",
|
||||
Columns: []*Column{
|
||||
{Name: "id", Type: field.TypeInt, Increment: true},
|
||||
},
|
||||
PrimaryKey: []*Column{
|
||||
{Name: "id", Type: field.TypeInt, Increment: true},
|
||||
},
|
||||
},
|
||||
},
|
||||
options: []MigrateOption{WithDropIndex(true), WithDropColumn(true)},
|
||||
before: func(mock sqlmock.Sqlmock) {
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery(escape("SHOW VARIABLES LIKE 'version'")).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"Variable_name", "Value"}).AddRow("version", "5.7.23"))
|
||||
mock.ExpectQuery(escape("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE `TABLE_SCHEMA` = (SELECT DATABASE()) AND `TABLE_NAME` = ?")).
|
||||
WithArgs("users").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1))
|
||||
mock.ExpectQuery(escape("SELECT `column_name`, `column_type`, `is_nullable`, `column_key`, `column_default`, `extra`, `character_set_name`, `collation_name` FROM INFORMATION_SCHEMA.COLUMNS WHERE `TABLE_SCHEMA` = (SELECT DATABASE()) AND `TABLE_NAME` = ?")).
|
||||
WithArgs("users").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"column_name", "column_type", "is_nullable", "column_key", "column_default", "extra", "character_set_name", "collation_name"}).
|
||||
AddRow("id", "bigint(20)", "NO", "PRI", "NULL", "auto_increment", "", "").
|
||||
AddRow("parent_id", "bigint(20)", "YES", "NULL", "NULL", "", "", ""))
|
||||
mock.ExpectQuery(escape("SELECT `index_name`, `column_name`, `non_unique`, `seq_in_index` FROM INFORMATION_SCHEMA.STATISTICS WHERE `TABLE_SCHEMA` = (SELECT DATABASE()) AND `TABLE_NAME` = ?")).
|
||||
WithArgs("users").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "non_unique", "seq_in_index"}).
|
||||
AddRow("PRIMARY", "id", "0", "1").
|
||||
AddRow("parent_id", "parent_id", "0", "1"))
|
||||
// check if a foreign-key needs to be dropped.
|
||||
mock.ExpectQuery(escape("SELECT `CONSTRAINT_NAME` FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE `TABLE_NAME` = ? AND `COLUMN_NAME` = ? AND `POSITION_IN_UNIQUE_CONSTRAINT` IS NOT NULL AND `TABLE_SCHEMA` = (SELECT DATABASE())")).
|
||||
WithArgs("users", "parent_id").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"CONSTRAINT_NAME"}).AddRow("users_parent_id"))
|
||||
mock.ExpectExec(escape("ALTER TABLE `users` DROP FOREIGN KEY `users_parent_id`")).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
// drop the unique index.
|
||||
mock.ExpectExec(escape("DROP INDEX `parent_id` ON `users`")).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
// drop the unique index.
|
||||
mock.ExpectExec(escape("ALTER TABLE `users` DROP COLUMN `parent_id`")).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
mock.ExpectCommit()
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "create a new simple-index for the foreign-key",
|
||||
tables: []*Table{
|
||||
{
|
||||
Name: "users",
|
||||
Columns: []*Column{
|
||||
{Name: "id", Type: field.TypeInt, Increment: true},
|
||||
{Name: "parent_id", Type: field.TypeInt, Nullable: true},
|
||||
},
|
||||
PrimaryKey: []*Column{
|
||||
{Name: "id", Type: field.TypeInt, Increment: true},
|
||||
},
|
||||
},
|
||||
},
|
||||
options: []MigrateOption{WithDropIndex(true), WithDropColumn(true)},
|
||||
before: func(mock sqlmock.Sqlmock) {
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery(escape("SHOW VARIABLES LIKE 'version'")).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"Variable_name", "Value"}).AddRow("version", "5.7.23"))
|
||||
mock.ExpectQuery(escape("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE `TABLE_SCHEMA` = (SELECT DATABASE()) AND `TABLE_NAME` = ?")).
|
||||
WithArgs("users").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1))
|
||||
mock.ExpectQuery(escape("SELECT `column_name`, `column_type`, `is_nullable`, `column_key`, `column_default`, `extra`, `character_set_name`, `collation_name` FROM INFORMATION_SCHEMA.COLUMNS WHERE `TABLE_SCHEMA` = (SELECT DATABASE()) AND `TABLE_NAME` = ?")).
|
||||
WithArgs("users").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"column_name", "column_type", "is_nullable", "column_key", "column_default", "extra", "character_set_name", "collation_name"}).
|
||||
AddRow("id", "bigint(20)", "NO", "PRI", "NULL", "auto_increment", "", "").
|
||||
AddRow("parent_id", "bigint(20)", "YES", "NULL", "NULL", "", "", ""))
|
||||
mock.ExpectQuery(escape("SELECT `index_name`, `column_name`, `non_unique`, `seq_in_index` FROM INFORMATION_SCHEMA.STATISTICS WHERE `TABLE_SCHEMA` = (SELECT DATABASE()) AND `TABLE_NAME` = ?")).
|
||||
WithArgs("users").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "non_unique", "seq_in_index"}).
|
||||
AddRow("PRIMARY", "id", "0", "1").
|
||||
AddRow("parent_id", "parent_id", "0", "1"))
|
||||
// check if there's a foreign-key that is associated with this index.
|
||||
mock.ExpectQuery(escape("SELECT `CONSTRAINT_NAME` FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE `TABLE_NAME` = ? AND `COLUMN_NAME` = ? AND `POSITION_IN_UNIQUE_CONSTRAINT` IS NOT NULL AND `TABLE_SCHEMA` = (SELECT DATABASE())")).
|
||||
WithArgs("users", "parent_id").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"CONSTRAINT_NAME"}).AddRow("users_parent_id"))
|
||||
// create a new index, to replace the old one (that needs to be dropped).
|
||||
mock.ExpectExec(escape("CREATE INDEX `users_parent_id` ON `users`(`parent_id`)")).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
// drop the unique index.
|
||||
mock.ExpectExec(escape("DROP INDEX `parent_id` ON `users`")).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
mock.ExpectCommit()
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "add edge to table",
|
||||
tables: func() []*Table {
|
||||
|
||||
@@ -69,13 +69,13 @@ func (t *Table) AddIndex(name string, unique bool, columns []string) *Table {
|
||||
Name: name,
|
||||
Unique: unique,
|
||||
columns: columns,
|
||||
Columns: make([]*Column, len(columns)),
|
||||
Columns: make([]*Column, 0, len(columns)),
|
||||
}
|
||||
for i, name := range columns {
|
||||
for _, name := range columns {
|
||||
c, ok := t.columns[name]
|
||||
if ok {
|
||||
idx.Columns[i] = c
|
||||
c.indexes = append(c.indexes, idx)
|
||||
idx.Columns = append(idx.Columns, c)
|
||||
}
|
||||
}
|
||||
t.Indexes = append(t.Indexes, idx)
|
||||
|
||||
Reference in New Issue
Block a user