Skip to content

Commit a7c048f

Browse files
multi: introduce dev migrations
When the kvdb to sql migration is initially introduced, we will want to ensure that it is only run under dev builds during the testing phase. We therefore introduce the functionality to have separate dev migrations, which are only included in a separate migration stream that is used only in dev builds. Note that these dev migrations are currently not included in the `sqlc.yaml` file, to ensure that the main sqlc models doesn't include the dev migrations.
1 parent c36359b commit a7c048f

File tree

7 files changed

+92
-4
lines changed

7 files changed

+92
-4
lines changed

db/migrations.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ const (
1313
//
1414
// NOTE: This MUST be updated when a new migration is added.
1515
LatestMigrationVersion = 5
16+
17+
// LatestDevMigrationVersion is the latest dev migration version of the
18+
// database. This is used to implement downgrade protection for the
19+
// daemon. This represents the latest number used in the migrations_dev
20+
// directory.
21+
//
22+
// NOTE: This MUST be updated when a migration is added or removed, from
23+
// the migrations_dev directory.
24+
LatestDevMigrationVersion = 1
1625
)
1726

1827
// MakeTestMigrationStreams creates the migration streams for the unit test
@@ -41,5 +50,25 @@ func MakeTestMigrationStreams() []sqldb.MigrationStream {
4150
},
4251
}
4352

44-
return []sqldb.MigrationStream{migStream}
53+
migStreamDev := sqldb.MigrationStream{
54+
MigrateTableName: pgx.DefaultMigrationsTable + "_dev",
55+
SQLFileDirectory: "sqlc/migrations_dev",
56+
Schemas: SqlSchemas,
57+
58+
// LatestMigrationVersion is the latest migration version of the
59+
// dev migrations database. This is used to implement downgrade
60+
// protection for the daemon.
61+
//
62+
// NOTE: This MUST be updated when a new dev migration is added.
63+
LatestMigrationVersion: LatestDevMigrationVersion,
64+
65+
MakePostMigrationChecks: func(
66+
db *sqldb.BaseDB) (map[uint]migrate.PostStepCallback,
67+
error) {
68+
69+
return make(map[uint]migrate.PostStepCallback), nil
70+
},
71+
}
72+
73+
return []sqldb.MigrationStream{migStream, migStreamDev}
4574
}

db/migrationstreams/sql_migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build !dev
2+
13
package migrationstreams
24

35
import (
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//go:build dev
2+
3+
package migrationstreams
4+
5+
import (
6+
"github.com/golang-migrate/migrate/v4"
7+
"github.com/golang-migrate/migrate/v4/database/pgx/v5"
8+
"github.com/lightninglabs/lightning-terminal/db"
9+
"github.com/lightningnetwork/lnd/sqldb/v2"
10+
)
11+
12+
var (
13+
// Create the prod migration stream.
14+
migStream = sqldb.MigrationStream{
15+
MigrateTableName: pgx.DefaultMigrationsTable,
16+
SQLFileDirectory: "sqlc/migrations",
17+
Schemas: db.SqlSchemas,
18+
19+
// LatestMigrationVersion is the latest migration version of the
20+
// database. This is used to implement downgrade protection for
21+
// the daemon.
22+
//
23+
// NOTE: This MUST be updated when a new migration is added.
24+
LatestMigrationVersion: db.LatestMigrationVersion,
25+
26+
MakePostMigrationChecks: func(
27+
db *sqldb.BaseDB) (map[uint]migrate.PostStepCallback,
28+
error) {
29+
30+
return make(map[uint]migrate.PostStepCallback), nil
31+
},
32+
}
33+
34+
// Create the dev migration stream.
35+
migStreamDev = sqldb.MigrationStream{
36+
MigrateTableName: pgx.DefaultMigrationsTable + "_dev",
37+
SQLFileDirectory: "sqlc/migrations_dev",
38+
Schemas: db.SqlSchemas,
39+
40+
// LatestMigrationVersion is the latest migration version of the
41+
// dev migrations database. This is used to implement downgrade
42+
// protection for the daemon.
43+
//
44+
// NOTE: This MUST be updated when a new dev migration is added.
45+
LatestMigrationVersion: db.LatestDevMigrationVersion,
46+
47+
MakePostMigrationChecks: func(
48+
db *sqldb.BaseDB) (map[uint]migrate.PostStepCallback,
49+
error) {
50+
51+
return make(map[uint]migrate.PostStepCallback), nil
52+
},
53+
}
54+
LitdMigrationStreams = []sqldb.MigrationStream{migStream, migStreamDev}
55+
)

db/schemas.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ import (
55
_ "embed"
66
)
77

8-
//go:embed sqlc/migrations/*.*.sql
8+
//go:embed sqlc/migration*/*.*.sql
99
var SqlSchemas embed.FS
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-- Comment to ensure the file created and picked up in the migration stream.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-- Comment to ensure the file created and picked up in the migration stream.

scripts/gen_sqlc_docker.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ set -e
55
# restore_files is a function to restore original schema files.
66
restore_files() {
77
echo "Restoring SQLite bigint patch..."
8-
for file in db/sqlc/migrations/*.up.sql.bak; do
8+
for file in db/sqlc/{migrations,migrations_dev}/*.up.sql.bak; do
99
mv "$file" "${file%.bak}"
1010
done
1111
}
@@ -30,7 +30,7 @@ GOMODCACHE=$(go env GOMODCACHE)
3030
# source schema SQL files to use "BIGINT PRIMARY KEY" instead of "INTEGER
3131
# PRIMARY KEY".
3232
echo "Applying SQLite bigint patch..."
33-
for file in db/sqlc/migrations/*.up.sql; do
33+
for file in db/sqlc/{migrations,migrations_dev}/*.up.sql; do
3434
echo "Patching $file"
3535
sed -i.bak -E 's/INTEGER PRIMARY KEY/BIGINT PRIMARY KEY/g' "$file"
3636
done

0 commit comments

Comments
 (0)