dialect/sql/schema: inspect outside transaction in auto migrate (#4290)

Since SQLite does not allow enabling/disabling foreign key checks within a transaction, Atlas disabled foreign key checks before opening a transaction and re-enables them after commit/rollback. This involves checking for violations every time the auto migrate tool checks for changing. By opening a transaction only in case there are changes, we can avoid this when not needed.

Closes https://github.com/ariga/atlas/issues/3297
This commit is contained in:
Jannik Clausen
2025-01-09 08:32:47 +01:00
committed by GitHub
parent fc57ede2fe
commit d5c8b282de
2 changed files with 43 additions and 25 deletions

View File

@@ -241,6 +241,23 @@ func TestSQLite(t *testing.T) {
require.NoError(t, err)
defer vdrv.Close()
Versioned(t, vdrv, "sqlite3://file?mode=memory&cache=shared&_fk=1", versioned.NewClient(versioned.Driver(vdrv)))
// In case there are no changes, no transaction should be opened and foreign keys should not be touched.
fkdrv, err := sql.Open("sqlite3", "file:fk_ent?mode=memory&cache=shared&_fk=1")
require.NoError(t, err)
defer fkdrv.Close()
fkclient := entv1.NewClient(entv1.Driver(fkdrv), entv1.Log(func(a ...any) {
s := a[0].(string)
switch {
case strings.HasPrefix(s, "driver.Tx"):
t.Errorf("unexpected transaction, %q", s)
case strings.HasPrefix(s, "driver.Exec") && strings.Contains(s, "PRAGMA foreign_keys"):
t.Errorf("unexpected foreign keys pragma: %q", s)
}
}))
require.NoError(t, fkclient.Schema.Create(ctx))
// Apply again has no changes -> there should not be any pragma changes and no transaction to be opened.
require.NoError(t, fkclient.Debug().Schema.Create(ctx))
}
// https://github.com/ent/ent/issues/2954