Skip to content

Commit

Permalink
loopdb: add ability to track manual migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
bhandras committed Jun 3, 2024
1 parent 4f5c806 commit c650cdc
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 0 deletions.
7 changes: 7 additions & 0 deletions loopdb/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ type SwapStore interface {
BatchUpdateLoopOutSwapCosts(ctx context.Context,
swaps map[lntypes.Hash]SwapCost) error

// HasMigration returns true if the migration with the given ID has
// been done.
HasMigration(ctx context.Context, migrationID string) (bool, error)

// SetMigration marks the migration with the given ID as done.
SetMigration(ctx context.Context, migrationID string) error

// Close closes the underlying database.
Close() error
}
Expand Down
23 changes: 23 additions & 0 deletions loopdb/sql_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,29 @@ func (b *BaseDB) BatchUpdateLoopOutSwapCosts(ctx context.Context,
})
}

// HasMigration returns true if the migration with the given ID has been done.
func (b *BaseDB) HasMigration(ctx context.Context, migrationID string) (
bool, error) {

migration, err := b.GetMigration(ctx, migrationID)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return false, err
}

return migration.MigrationTs.Valid, nil
}

// SetMigration marks the migration with the given ID as done.
func (b *BaseDB) SetMigration(ctx context.Context, migrationID string) error {
return b.InsertMigration(ctx, sqlc.InsertMigrationParams{
MigrationID: migrationID,
MigrationTs: sql.NullTime{
Time: time.Now().UTC(),
Valid: true,
},
})
}

// loopToInsertArgs converts a SwapContract struct to the arguments needed to
// insert it into the database.
func loopToInsertArgs(hash lntypes.Hash,
Expand Down
16 changes: 16 additions & 0 deletions loopdb/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,22 @@ func TestBatchUpdateCost(t *testing.T) {
require.Equal(t, updateMap[hash2], swapsMap[hash2].State().Cost)
}

// TestMigrationTracker tests the migration tracker functionality.
func TestMigrationTracker(t *testing.T) {
ctxb := context.Background()

// Create a new sqlite store for testing.
sqlDB := NewTestDB(t)
hasMigration, err := sqlDB.HasMigration(ctxb, "test")
require.NoError(t, err)
require.False(t, hasMigration)

require.NoError(t, sqlDB.SetMigration(ctxb, "test"))
hasMigration, err = sqlDB.HasMigration(ctxb, "test")
require.NoError(t, err)
require.True(t, hasMigration)
}

const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

func randomString(length int) string {
Expand Down
45 changes: 45 additions & 0 deletions loopdb/sqlc/migration_tracker.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions loopdb/sqlc/migrations/000008_migration_tracker.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE migration_tracker;

9 changes: 9 additions & 0 deletions loopdb/sqlc/migrations/000008_migration_tracker.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE migration_tracker (
-- migration_id is the id of the migration.
migration_id TEXT NOT NULL,

-- migration_ts is the timestamp at which the migration was run.
migration_ts TIMESTAMP,

PRIMARY KEY (migration_id)
);
5 changes: 5 additions & 0 deletions loopdb/sqlc/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions loopdb/sqlc/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions loopdb/sqlc/queries/migration_tracker.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- name: InsertMigration :exec
INSERT INTO migration_tracker (
migration_id,
migration_ts
) VALUES ($1, $2);

-- name: GetMigration :one
SELECT
migration_id,
migration_ts
FROM
migration_tracker
WHERE
migration_id = $1;
14 changes: 14 additions & 0 deletions loopdb/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1017,3 +1017,17 @@ func (b *boltSwapStore) BatchUpdateLoopOutSwapCosts(ctx context.Context,

return errUnimplemented
}

// HasMigration returns true if the migration with the given ID has been done.
func (b *boltSwapStore) HasMigration(ctx context.Context, migrationID string) (
bool, error) {

return false, errUnimplemented
}

// SetMigration marks the migration with the given ID as done.
func (b *boltSwapStore) SetMigration(ctx context.Context,
migrationID string) error {

return errUnimplemented
}
14 changes: 14 additions & 0 deletions loopdb/store_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,17 @@ func (s *StoreMock) BatchUpdateLoopOutSwapCosts(ctx context.Context,

return nil
}

// HasMigration returns true if the migration with the given ID has been done.
func (s *StoreMock) HasMigration(ctx context.Context, migrationID string) (
bool, error) {

return false, errUnimplemented
}

// SetMigration marks the migration with the given ID as done.
func (s *StoreMock) SetMigration(ctx context.Context,
migrationID string) error {

return errUnimplemented
}

0 comments on commit c650cdc

Please sign in to comment.