From 10949c7bb88e9e0b766059d17226464a4fe66907 Mon Sep 17 00:00:00 2001 From: Viktor Torstensson Date: Tue, 12 May 2026 12:58:16 +0200 Subject: [PATCH] db: remove kvdb to sql migration from dev db stream Remove the KVDB-to-SQL migration from the dev SQL migration set now that this migration is now part of the production migration set. As the migrations_dev folder no longer contains any migrations, we also update the db logic and the gen_sqlc_docker.sh script to not error when the folder(s) are empty. --- db/migrations.go | 22 ++++++++++++++- db/migsets/sql_migrations_dev.go | 28 ++++++------------- ...000001_code_migration_kvdb_to_sql.down.sql | 1 - .../000001_code_migration_kvdb_to_sql.up.sql | 1 - scripts/gen_sqlc_docker.sh | 1 + 5 files changed, 30 insertions(+), 23 deletions(-) delete mode 100644 db/sqlc/migrations_dev/000001_code_migration_kvdb_to_sql.down.sql delete mode 100644 db/sqlc/migrations_dev/000001_code_migration_kvdb_to_sql.up.sql diff --git a/db/migrations.go b/db/migrations.go index 04605e00..ef07f9b4 100644 --- a/db/migrations.go +++ b/db/migrations.go @@ -1,6 +1,8 @@ package db import ( + "io/fs" + "github.com/golang-migrate/migrate/v4" "github.com/golang-migrate/migrate/v4/database/pgx/v5" "github.com/lightningnetwork/lnd/sqldb/v2" @@ -28,9 +30,21 @@ const ( // // NOTE: This MUST be updated when a migration is added or removed, from // the migrations_dev directory. - LatestDevMigrationVersion = 1 + LatestDevMigrationVersion = 0 ) +// HasDevMigrations reports whether any dev SQL migration files are embedded in +// the current build. This lets dev builds omit the separate dev migration set +// cleanly when the directory exists but currently contains no migration files. +func HasDevMigrations() bool { + files, err := fs.Glob(SqlSchemas, "sqlc/migrations_dev/*.*.sql") + if err != nil { + return false + } + + return len(files) > 0 +} + // MakeTestMigrationSets creates the migration sets for the unit test // environment. // @@ -57,6 +71,12 @@ func MakeTestMigrationSets() []sqldb.MigrationSet { }, } + // If there are no dev migrations in the sqlc/migrations_dev folder, we + // can return early. + if !HasDevMigrations() { + return []sqldb.MigrationSet{migSet} + } + migSetDev := sqldb.MigrationSet{ TrackingTableName: pgx.DefaultMigrationsTable + "_dev", SQLFileDirectory: "sqlc/migrations_dev", diff --git a/db/migsets/sql_migrations_dev.go b/db/migsets/sql_migrations_dev.go index 5484b1b8..8cb54a26 100644 --- a/db/migsets/sql_migrations_dev.go +++ b/db/migsets/sql_migrations_dev.go @@ -13,12 +13,6 @@ import ( "github.com/lightningnetwork/lnd/sqldb/v2" ) -const ( - // 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. func MakeMigrationSets(ctx context.Context, basicClient lnrpc.LightningClient, macPath string, @@ -56,6 +50,12 @@ func MakeMigrationSets(ctx context.Context, }, } + // If there are no dev migrations in the sqlc/migrations_dev folder, we + // can return early. + if !db.HasDevMigrations() { + return []sqldb.MigrationSet{migSet} + } + // Create the dev migration set. migSetDev := sqldb.MigrationSet{ TrackingTableName: pgx.DefaultMigrationsTable + "_dev", @@ -69,22 +69,10 @@ func MakeMigrationSets(ctx context.Context, // NOTE: This MUST be updated when a new dev migration is added. LatestMigrationVersion: db.LatestDevMigrationVersion, - MakeProgrammaticMigrations: func(baseDB *sqldb.BaseDB) ( + MakeProgrammaticMigrations: func(_ *sqldb.BaseDB) ( map[uint]migrate.ProgrammaticMigrEntry, error) { - // Any programmatic migrations added to this map will be - // executed when the dev 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[DevKVDBtoSQLMigVersion] = Mig6ProgrammaticMigration( - ctx, basicClient, baseDB, macPath, clock, - DevKVDBtoSQLMigVersion, - ) - - return res, nil + return make(map[uint]migrate.ProgrammaticMigrEntry), nil }, } diff --git a/db/sqlc/migrations_dev/000001_code_migration_kvdb_to_sql.down.sql b/db/sqlc/migrations_dev/000001_code_migration_kvdb_to_sql.down.sql deleted file mode 100644 index 0d246b2d..00000000 --- a/db/sqlc/migrations_dev/000001_code_migration_kvdb_to_sql.down.sql +++ /dev/null @@ -1 +0,0 @@ --- Comment to ensure the file created and picked up in the migration stream. \ No newline at end of file diff --git a/db/sqlc/migrations_dev/000001_code_migration_kvdb_to_sql.up.sql b/db/sqlc/migrations_dev/000001_code_migration_kvdb_to_sql.up.sql deleted file mode 100644 index 0d246b2d..00000000 --- a/db/sqlc/migrations_dev/000001_code_migration_kvdb_to_sql.up.sql +++ /dev/null @@ -1 +0,0 @@ --- Comment to ensure the file created and picked up in the migration stream. \ No newline at end of file diff --git a/scripts/gen_sqlc_docker.sh b/scripts/gen_sqlc_docker.sh index 3d93f37f..26dd0289 100755 --- a/scripts/gen_sqlc_docker.sh +++ b/scripts/gen_sqlc_docker.sh @@ -1,6 +1,7 @@ #!/bin/bash set -e +shopt -s nullglob # restore_files is a function to restore original schema files. restore_files() {