Skip to content

Commit c650cdc

Browse files
committed
loopdb: add ability to track manual migrations
1 parent 4f5c806 commit c650cdc

File tree

11 files changed

+151
-0
lines changed

11 files changed

+151
-0
lines changed

loopdb/interface.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ type SwapStore interface {
7070
BatchUpdateLoopOutSwapCosts(ctx context.Context,
7171
swaps map[lntypes.Hash]SwapCost) error
7272

73+
// HasMigration returns true if the migration with the given ID has
74+
// been done.
75+
HasMigration(ctx context.Context, migrationID string) (bool, error)
76+
77+
// SetMigration marks the migration with the given ID as done.
78+
SetMigration(ctx context.Context, migrationID string) error
79+
7380
// Close closes the underlying database.
7481
Close() error
7582
}

loopdb/sql_store.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,29 @@ func (b *BaseDB) BatchUpdateLoopOutSwapCosts(ctx context.Context,
439439
})
440440
}
441441

442+
// HasMigration returns true if the migration with the given ID has been done.
443+
func (b *BaseDB) HasMigration(ctx context.Context, migrationID string) (
444+
bool, error) {
445+
446+
migration, err := b.GetMigration(ctx, migrationID)
447+
if err != nil && !errors.Is(err, sql.ErrNoRows) {
448+
return false, err
449+
}
450+
451+
return migration.MigrationTs.Valid, nil
452+
}
453+
454+
// SetMigration marks the migration with the given ID as done.
455+
func (b *BaseDB) SetMigration(ctx context.Context, migrationID string) error {
456+
return b.InsertMigration(ctx, sqlc.InsertMigrationParams{
457+
MigrationID: migrationID,
458+
MigrationTs: sql.NullTime{
459+
Time: time.Now().UTC(),
460+
Valid: true,
461+
},
462+
})
463+
}
464+
442465
// loopToInsertArgs converts a SwapContract struct to the arguments needed to
443466
// insert it into the database.
444467
func loopToInsertArgs(hash lntypes.Hash,

loopdb/sql_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,22 @@ func TestBatchUpdateCost(t *testing.T) {
515515
require.Equal(t, updateMap[hash2], swapsMap[hash2].State().Cost)
516516
}
517517

518+
// TestMigrationTracker tests the migration tracker functionality.
519+
func TestMigrationTracker(t *testing.T) {
520+
ctxb := context.Background()
521+
522+
// Create a new sqlite store for testing.
523+
sqlDB := NewTestDB(t)
524+
hasMigration, err := sqlDB.HasMigration(ctxb, "test")
525+
require.NoError(t, err)
526+
require.False(t, hasMigration)
527+
528+
require.NoError(t, sqlDB.SetMigration(ctxb, "test"))
529+
hasMigration, err = sqlDB.HasMigration(ctxb, "test")
530+
require.NoError(t, err)
531+
require.True(t, hasMigration)
532+
}
533+
518534
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
519535

520536
func randomString(length int) string {

loopdb/sqlc/migration_tracker.sql.go

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DROP TABLE migration_tracker;
2+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE TABLE migration_tracker (
2+
-- migration_id is the id of the migration.
3+
migration_id TEXT NOT NULL,
4+
5+
-- migration_ts is the timestamp at which the migration was run.
6+
migration_ts TIMESTAMP,
7+
8+
PRIMARY KEY (migration_id)
9+
);

loopdb/sqlc/models.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

loopdb/sqlc/querier.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- name: InsertMigration :exec
2+
INSERT INTO migration_tracker (
3+
migration_id,
4+
migration_ts
5+
) VALUES ($1, $2);
6+
7+
-- name: GetMigration :one
8+
SELECT
9+
migration_id,
10+
migration_ts
11+
FROM
12+
migration_tracker
13+
WHERE
14+
migration_id = $1;

loopdb/store.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,3 +1017,17 @@ func (b *boltSwapStore) BatchUpdateLoopOutSwapCosts(ctx context.Context,
10171017

10181018
return errUnimplemented
10191019
}
1020+
1021+
// HasMigration returns true if the migration with the given ID has been done.
1022+
func (b *boltSwapStore) HasMigration(ctx context.Context, migrationID string) (
1023+
bool, error) {
1024+
1025+
return false, errUnimplemented
1026+
}
1027+
1028+
// SetMigration marks the migration with the given ID as done.
1029+
func (b *boltSwapStore) SetMigration(ctx context.Context,
1030+
migrationID string) error {
1031+
1032+
return errUnimplemented
1033+
}

0 commit comments

Comments
 (0)