From 85d0739019e3e48797a5245f3557aa75fcbd3dd5 Mon Sep 17 00:00:00 2001 From: Viktor Torstensson Date: Tue, 12 May 2026 12:55:19 +0200 Subject: [PATCH] db: move kvdb to SQL programmatic migration to prod Move the programmatic KVDB-to-SQL migration out of the dev-only migration stream and into the production SQL migration set. This also requires that we add a new migration file for the prod migration, which will trigger the kvdb to sql programmatic migration to run in production. --- db/migrations.go | 9 ++++- ...ions_dev.go => programmatic_migrations.go} | 2 -- db/migsets/sql_migrations.go | 20 ++++++++--- db/migsets/sql_migrations_dev.go | 34 ++++++++++++------- .../000006_code_migration_kvdb_to_sql.up.sql | 3 ++ 5 files changed, 48 insertions(+), 20 deletions(-) rename db/migsets/{programmatic_migrations_dev.go => programmatic_migrations.go} (99%) create mode 100644 db/sqlc/migrations/000006_code_migration_kvdb_to_sql.up.sql diff --git a/db/migrations.go b/db/migrations.go index 155ef403..04605e00 100644 --- a/db/migrations.go +++ b/db/migrations.go @@ -12,7 +12,14 @@ const ( // daemon. // // NOTE: This MUST be updated when a new migration is added. - LatestMigrationVersion = 5 + LatestMigrationVersion = 6 + + // KVDBtoSQLMigVersion is the version of the migration that migrates the + // kvdb to the sql database. + // + // NOTE: This version value should not be updated when a new migration + // is added, as this represents a specific migration. + KVDBtoSQLMigVersion = 6 // LatestDevMigrationVersion is the latest dev migration version of the // database. This is used to implement downgrade protection for the diff --git a/db/migsets/programmatic_migrations_dev.go b/db/migsets/programmatic_migrations.go similarity index 99% rename from db/migsets/programmatic_migrations_dev.go rename to db/migsets/programmatic_migrations.go index d41500f4..2c51699b 100644 --- a/db/migsets/programmatic_migrations_dev.go +++ b/db/migsets/programmatic_migrations.go @@ -1,5 +1,3 @@ -//go:build dev - package migsets import ( diff --git a/db/migsets/sql_migrations.go b/db/migsets/sql_migrations.go index e4a71468..3a76adb5 100644 --- a/db/migsets/sql_migrations.go +++ b/db/migsets/sql_migrations.go @@ -14,8 +14,8 @@ import ( ) // MakeMigrationSets creates the migration sets for production environments. -func MakeMigrationSets(_ context.Context, _ lnrpc.LightningClient, _ string, - _ clock.Clock) []sqldb.MigrationSet { +func MakeMigrationSets(ctx context.Context, basicClient lnrpc.LightningClient, + macPath string, clock clock.Clock) []sqldb.MigrationSet { // migSet defines the SQL migration set used to create and upgrade LiT's // SQL schema. @@ -31,10 +31,22 @@ func MakeMigrationSets(_ context.Context, _ lnrpc.LightningClient, _ string, // NOTE: This MUST be updated when a new migration is added. LatestMigrationVersion: db.LatestMigrationVersion, - MakeProgrammaticMigrations: func(db *sqldb.BaseDB) ( + MakeProgrammaticMigrations: func(baseDB *sqldb.BaseDB) ( map[uint]migrate.ProgrammaticMigrEntry, error) { - return make(map[uint]migrate.ProgrammaticMigrEntry), nil + // Any programmatic migrations added to this map will be + // executed when the migration number for the uint key + // is applied. If no entry exists for a given uint, then + // no programmatic migration will be executed for that + // migration number. + res := make(map[uint]migrate.ProgrammaticMigrEntry) + + res[db.KVDBtoSQLMigVersion] = Mig6ProgrammaticMigration( + ctx, basicClient, baseDB, macPath, clock, + db.KVDBtoSQLMigVersion, + ) + + return res, nil }, } diff --git a/db/migsets/sql_migrations_dev.go b/db/migsets/sql_migrations_dev.go index 62cdfe02..5484b1b8 100644 --- a/db/migsets/sql_migrations_dev.go +++ b/db/migsets/sql_migrations_dev.go @@ -14,13 +14,9 @@ import ( ) const ( - // KVDBtoSQLMigVersion is the version of the migration that migrates the - // kvdb to the sql database. - // - // TODO: When this the kvdb to sql migration goes live into prod, this - // should be moved to non dev db/migrations.go file, and this constant - // value should be updated to reflect the real migration number. - KVDBtoSQLMigVersion = 1 + // DevKVDBtoSQLMigVersion is the dev version of the migration that + // migrates the kvdb to the sql database. + DevKVDBtoSQLMigVersion = 1 ) // MakeMigrationSets creates the migration sets for the dev environments. @@ -41,10 +37,22 @@ func MakeMigrationSets(ctx context.Context, // NOTE: This MUST be updated when a new migration is added. LatestMigrationVersion: db.LatestMigrationVersion, - MakeProgrammaticMigrations: func(db *sqldb.BaseDB) ( + MakeProgrammaticMigrations: func(baseDB *sqldb.BaseDB) ( map[uint]migrate.ProgrammaticMigrEntry, error) { - return make(map[uint]migrate.ProgrammaticMigrEntry), nil + // Any programmatic migrations added to this map will be + // executed when the migration number for the uint key + // is applied. If no entry exists for a given uint, then + // no programmatic migration will be executed for that + // migration number. + res := make(map[uint]migrate.ProgrammaticMigrEntry) + + res[db.KVDBtoSQLMigVersion] = Mig6ProgrammaticMigration( + ctx, basicClient, baseDB, macPath, clock, + db.KVDBtoSQLMigVersion, + ) + + return res, nil }, } @@ -61,7 +69,7 @@ func MakeMigrationSets(ctx context.Context, // NOTE: This MUST be updated when a new dev migration is added. LatestMigrationVersion: db.LatestDevMigrationVersion, - MakeProgrammaticMigrations: func(db *sqldb.BaseDB) ( + MakeProgrammaticMigrations: func(baseDB *sqldb.BaseDB) ( map[uint]migrate.ProgrammaticMigrEntry, error) { // Any programmatic migrations added to this map will be @@ -71,9 +79,9 @@ func MakeMigrationSets(ctx context.Context, // executed for that migration number. res := make(map[uint]migrate.ProgrammaticMigrEntry) - res[KVDBtoSQLMigVersion] = Mig6ProgrammaticMigration( - ctx, basicClient, db, macPath, clock, - KVDBtoSQLMigVersion, + res[DevKVDBtoSQLMigVersion] = Mig6ProgrammaticMigration( + ctx, basicClient, baseDB, macPath, clock, + DevKVDBtoSQLMigVersion, ) return res, nil diff --git a/db/sqlc/migrations/000006_code_migration_kvdb_to_sql.up.sql b/db/sqlc/migrations/000006_code_migration_kvdb_to_sql.up.sql new file mode 100644 index 00000000..3a35f449 --- /dev/null +++ b/db/sqlc/migrations/000006_code_migration_kvdb_to_sql.up.sql @@ -0,0 +1,3 @@ +-- Comment to ensure the file created and picked up in the migration stream. +-- NOTE: There's intentionally no corresponding .down file for this migration +-- to ensure that the KVDB -> SQL migration isn't executed on db downgrades. \ No newline at end of file