sqldb: separate migration execution from construction

This commit separates the execution of SQL and in-code migrations
from their construction. This change is necessary because,
currently, the SQL schema is migrated during the construction
phase in the lncfg package. However, migrations are typically
executed when individual stores are constructed within the
configuration builder.
This commit is contained in:
Andras Banki-Horvath
2024-11-25 20:29:08 +01:00
parent b789fb2db3
commit 91c3e1496f
8 changed files with 137 additions and 57 deletions

View File

@@ -314,16 +314,19 @@ func TestCustomMigration(t *testing.T) {
for i := 0; i < 3; i++ {
db, err = NewSqliteStore(&SqliteConfig{
SkipMigrations: false,
}, dbFileName, test.migrations)
if db != nil {
dbToCleanup := db.DB
t.Cleanup(func() {
require.NoError(
t, dbToCleanup.Close(),
)
})
}
}, dbFileName)
require.NoError(t, err)
dbToCleanup := db.DB
t.Cleanup(func() {
require.NoError(
t, dbToCleanup.Close(),
)
})
err = db.ApplyAllMigrations(
ctxb, test.migrations,
)
if test.expectedSuccess {
require.NoError(t, err)
} else {
@@ -333,7 +336,7 @@ func TestCustomMigration(t *testing.T) {
// so we can read versions.
db, err = NewSqliteStore(&SqliteConfig{
SkipMigrations: true,
}, dbFileName, nil)
}, dbFileName)
require.NoError(t, err)
}
@@ -399,8 +402,12 @@ func TestCustomMigration(t *testing.T) {
// are idempotent.
for i := 0; i < 3; i++ {
cfg.SkipMigrations = false
db, err = NewPostgresStore(cfg, test.migrations)
db, err = NewPostgresStore(cfg)
require.NoError(t, err)
err = db.ApplyAllMigrations(
ctxb, test.migrations,
)
if test.expectedSuccess {
require.NoError(t, err)
} else {
@@ -409,7 +416,7 @@ func TestCustomMigration(t *testing.T) {
// Also repoen the DB without migrations
// so we can read versions.
cfg.SkipMigrations = true
db, err = NewPostgresStore(cfg, nil)
db, err = NewPostgresStore(cfg)
require.NoError(t, err)
}