mirror of
https://github.com/ent/ent.git
synced 2026-05-05 00:50:54 +03:00
Merge branch 'master' of https://github.com/day-dreams/ent into day-dreams-master
This commit is contained in:
@@ -59,32 +59,40 @@ func WithFixture(b bool) MigrateOption {
|
||||
}
|
||||
}
|
||||
|
||||
// WithForeignKeys enables creating foreign-key in ddl. Defaults to true.
|
||||
func WithForeignKeys(b bool) MigrateOption {
|
||||
return func(m *Migrate) {
|
||||
m.withForeignKeys = 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.
|
||||
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.
|
||||
withForeignKeys bool // with foreign 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{}
|
||||
m := &Migrate{withForeignKeys: true}
|
||||
for _, opt := range opts {
|
||||
opt(m)
|
||||
}
|
||||
switch d.Dialect() {
|
||||
case dialect.MySQL:
|
||||
m.sqlDialect = &MySQL{Driver: d}
|
||||
case dialect.SQLite:
|
||||
m.sqlDialect = &SQLite{Driver: d}
|
||||
m.sqlDialect = &SQLite{Driver: d, WithForeignKeys: m.withForeignKeys}
|
||||
case dialect.Postgres:
|
||||
m.sqlDialect = &Postgres{Driver: d}
|
||||
default:
|
||||
return nil, fmt.Errorf("sql/schema: unsupported dialect %q", d.Dialect())
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(m)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
@@ -161,6 +169,9 @@ func (m *Migrate) create(ctx context.Context, tx dialect.Tx, tables ...*Table) e
|
||||
}
|
||||
}
|
||||
}
|
||||
if !m.withForeignKeys {
|
||||
return nil
|
||||
}
|
||||
// Create foreign keys after tables were created/altered,
|
||||
// because circular foreign-key constraints are possible.
|
||||
for _, t := range tables {
|
||||
@@ -345,7 +356,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.withForeignKeys || !ok {
|
||||
return nil
|
||||
}
|
||||
rename := make(map[string]*Index)
|
||||
|
||||
Reference in New Issue
Block a user