mirror of
https://github.com/ent/ent.git
synced 2026-05-05 00:50:54 +03:00
dialect/sql/schema: use withForeighKeys() instead of withForeighKeysDisable(), and add unit tests () (#748)
This commit is contained in:
@@ -59,28 +59,28 @@ func WithFixture(b bool) MigrateOption {
|
||||
}
|
||||
}
|
||||
|
||||
// WithForeighKeysDisable disables creating foreigh-key in ddl. In some case we are
|
||||
// not allowed to use foreigh-key. Defaults to false
|
||||
func WithForeighKeysDisable(b bool) MigrateOption {
|
||||
// WithForeighKeys enables creating foreigh-key in ddl. In some case we are
|
||||
// not allowed to use foreigh-key. Defaults to true
|
||||
func WithForeighKeys(b bool) MigrateOption {
|
||||
return func(m *Migrate) {
|
||||
m.disableForeighKeys = b
|
||||
m.withForeighKeys = b
|
||||
}
|
||||
}
|
||||
|
||||
// Migrate runs the migrations logic for the SQL dialects.
|
||||
type Migrate struct {
|
||||
sqlDialect
|
||||
universalID bool // global unique ids.
|
||||
dropColumns bool // drop deleted columns.
|
||||
dropIndexes bool // drop deleted indexes.
|
||||
withFixture bool // with fks rename fixture.
|
||||
disableForeighKeys bool // do not create foreigh keys
|
||||
typeRanges []string // types order by their range.
|
||||
universalID bool // global unique ids.
|
||||
dropColumns bool // drop deleted columns.
|
||||
dropIndexes bool // drop deleted indexes.
|
||||
withFixture bool // with fks rename fixture.
|
||||
withForeighKeys bool // with foreigh keys
|
||||
typeRanges []string // types order by their range.
|
||||
}
|
||||
|
||||
// NewMigrate create a migration structure for the given SQL driver.
|
||||
func NewMigrate(d dialect.Driver, opts ...MigrateOption) (*Migrate, error) {
|
||||
m := &Migrate{withFixture: true}
|
||||
m := &Migrate{withFixture: true, withForeighKeys: true}
|
||||
switch d.Dialect() {
|
||||
case dialect.MySQL:
|
||||
m.sqlDialect = &MySQL{Driver: d}
|
||||
@@ -128,9 +128,6 @@ func (m *Migrate) Create(ctx context.Context, tables ...*Table) error {
|
||||
func (m *Migrate) create(ctx context.Context, tx dialect.Tx, tables ...*Table) error {
|
||||
for _, t := range tables {
|
||||
m.setupTable(t)
|
||||
if m.disableForeighKeys {
|
||||
t.ForeignKeys = nil
|
||||
}
|
||||
switch exist, err := m.tableExist(ctx, tx, t.Name); {
|
||||
case err != nil:
|
||||
return err
|
||||
@@ -153,7 +150,15 @@ func (m *Migrate) create(ctx context.Context, tx dialect.Tx, tables ...*Table) e
|
||||
return err
|
||||
}
|
||||
default: // !exist
|
||||
// special case: sqllite driver create foreigh keys during table creating period,
|
||||
// we need to set foreigh keys as emtpy before it, and get FKS back after Query().
|
||||
bks := t.ForeignKeys
|
||||
if _, ok := m.sqlDialect.(*SQLite); ok && !m.withForeighKeys {
|
||||
t.ForeignKeys = nil
|
||||
}
|
||||
query, args := m.tBuilder(t).Query()
|
||||
t.ForeignKeys = bks
|
||||
// create table
|
||||
if err := tx.Exec(ctx, query, args, nil); err != nil {
|
||||
return fmt.Errorf("create table %q: %v", t.Name, err)
|
||||
}
|
||||
@@ -176,7 +181,7 @@ func (m *Migrate) create(ctx context.Context, tx dialect.Tx, tables ...*Table) e
|
||||
// Create foreign keys after tables were created/altered,
|
||||
// because circular foreign-key constraints are possible.
|
||||
for _, t := range tables {
|
||||
if len(t.ForeignKeys) == 0 {
|
||||
if !m.withForeighKeys || len(t.ForeignKeys) == 0 {
|
||||
continue
|
||||
}
|
||||
fks := make([]*ForeignKey, 0, len(t.ForeignKeys))
|
||||
@@ -357,7 +362,7 @@ func (m *Migrate) changeSet(curr, new *Table) (*changes, error) {
|
||||
// fixture is a special migration code for renaming foreign-key columns (issue-#285).
|
||||
func (m *Migrate) fixture(ctx context.Context, tx dialect.Tx, curr, new *Table) error {
|
||||
d, ok := m.sqlDialect.(fkRenamer)
|
||||
if !m.withFixture || !ok {
|
||||
if !m.withFixture || !m.withForeighKeys || !ok {
|
||||
return nil
|
||||
}
|
||||
rename := make(map[string]*Index)
|
||||
|
||||
Reference in New Issue
Block a user