mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-10-11 07:13:28 +02:00
sqldb: establish a base DB version even if it's not yet tracked
Previously, if a DB version wasn't available, we re-ran all schema migrations without verifying the schema version. However, setting a base schema version is essential because some earlier migrations were not idempotent. This commit addresses the issue by using the current schema version provided by sqlc as the base.
This commit is contained in:
@@ -158,6 +158,10 @@ func (s *SqliteStore) ApplyAllMigrations(ctx context.Context,
|
||||
return ApplyMigrations(ctx, s.BaseDB, s, migrations)
|
||||
}
|
||||
|
||||
func errSqliteMigration(err error) error {
|
||||
return fmt.Errorf("error creating sqlite migration: %w", err)
|
||||
}
|
||||
|
||||
// ExecuteMigrations runs migrations for the sqlite database, depending on the
|
||||
// target given, either all migrations or up to a given version.
|
||||
func (s *SqliteStore) ExecuteMigrations(target MigrationTarget) error {
|
||||
@@ -165,7 +169,7 @@ func (s *SqliteStore) ExecuteMigrations(target MigrationTarget) error {
|
||||
s.DB, &sqlite_migrate.Config{},
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating sqlite migration: %w", err)
|
||||
return errSqliteMigration(err)
|
||||
}
|
||||
|
||||
// Populate the database with our set of schemas based on our embedded
|
||||
@@ -176,6 +180,23 @@ func (s *SqliteStore) ExecuteMigrations(target MigrationTarget) error {
|
||||
)
|
||||
}
|
||||
|
||||
// GetSchemaVersion returns the current schema version of the SQLite database.
|
||||
func (s *SqliteStore) GetSchemaVersion() (int, bool, error) {
|
||||
driver, err := sqlite_migrate.WithInstance(
|
||||
s.DB, &sqlite_migrate.Config{},
|
||||
)
|
||||
if err != nil {
|
||||
return 0, false, errSqliteMigration(err)
|
||||
}
|
||||
|
||||
version, dirty, err := driver.Version()
|
||||
if err != nil {
|
||||
return 0, dirty, err
|
||||
}
|
||||
|
||||
return version, dirty, nil
|
||||
}
|
||||
|
||||
// NewTestSqliteDB is a helper function that creates an SQLite database for
|
||||
// testing.
|
||||
func NewTestSqliteDB(t *testing.T) *SqliteStore {
|
||||
|
Reference in New Issue
Block a user