Skip to content

Commit

Permalink
fixup! feat: sqlite: extract common init and migration utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Jul 11, 2024
1 parent 891b251 commit 37b5a56
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions chain/ethhashlookup/eth_transaction_hash_lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func NewTransactionHashLookup(ctx context.Context, path string) (*EthTxHashLooku
}

if err := sqlite.InitDb(ctx, "eth transaction hash lookup", db, ddls, []sqlite.MigrationFunc{}); err != nil {
_ = db.Close()
return nil, xerrors.Errorf("failed to init eth transaction hash lookup db: %w", err)
}

Expand Down
1 change: 1 addition & 0 deletions chain/index/msgindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func PopulateAfterSnapshot(lctx context.Context, path string, cs ChainStore) err
}()

if err := sqlite.InitDb(lctx, "message index", db, ddls, []sqlite.MigrationFunc{}); err != nil {
_ = db.Close()
return xerrors.Errorf("error creating msgindex database: %w", err)
}

Expand Down
4 changes: 4 additions & 0 deletions lib/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func Open(path string) (*sql.DB, bool, error) {
// content. The schemaVersion will be set inside the databse if it is newly created. Otherwise, the
// version is read from the databse and returned. This value should be checked against the expected
// version to determine if the database needs to be upgraded.
// It is up to the caller to close the database if an error is returned by this function.
func InitDb(
ctx context.Context,
name string,
Expand Down Expand Up @@ -122,6 +123,9 @@ func InitDb(

runVacuum := foundVersion != schemaVersion

// run a migration for each version that we have not yet applied, where foundVersion is what is
// currently in the database and schemaVersion is the target version. If they are the same,
// nothing is run.
for i := foundVersion + 1; i <= schemaVersion; i++ {
now := time.Now()

Expand Down

0 comments on commit 37b5a56

Please sign in to comment.