Skip to content

Commit 1d9100e

Browse files
multi: introduce dev migrations
1 parent 71ddae3 commit 1d9100e

File tree

7 files changed

+71
-3
lines changed

7 files changed

+71
-3
lines changed

db/migrations.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ const (
2323
//
2424
// NOTE: This MUST be updated when a new migration is added.
2525
LatestMigrationVersion = 5
26+
27+
// LatestDevMigrationVersion is the latest dev migration version of the
28+
// database. This is used to implement downgrade protection for the
29+
// daemon. This represents the latest number used in the migrations_dev
30+
// directory.
31+
//
32+
// NOTE: This MUST be updated when a migration is added or removed, from
33+
// the migrations_dev directory.
34+
LatestDevMigrationVersion = 1
2635
)
2736

2837
// MigrationTarget is a functional option that can be passed to applyMigrations

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)