-
Notifications
You must be signed in to change notification settings - Fork 33
db: introduce sqldbv2 scaffolding #235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bitromortac
merged 2 commits into
lightninglabs:faraday-forwarding-ability
from
bitromortac:2603-fwd-prep-6
Mar 31, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package db | ||
|
|
||
| // QueriesTxOptions defines the set of db txn options the SQLQueries | ||
| // understands. | ||
| type QueriesTxOptions struct { | ||
| // readOnly governs if a read only transaction is needed or not. | ||
| readOnly bool | ||
| } | ||
|
|
||
| // ReadOnly returns true if the transaction should be read only. | ||
| // | ||
| // NOTE: This implements the TxOptions. | ||
| func (a *QueriesTxOptions) ReadOnly() bool { | ||
| return a.readOnly | ||
| } | ||
|
|
||
| // NewQueryReadTx creates a new read transaction option set. | ||
| func NewQueryReadTx() QueriesTxOptions { | ||
| return QueriesTxOptions{ | ||
| readOnly: true, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package db | ||
|
|
||
| const ( | ||
| // LatestMigrationVersion is the latest migration version of the | ||
| // database. This is used to implement downgrade protection for the | ||
| // daemon. | ||
| // | ||
| // NOTE: This MUST be updated when a new migration is added. | ||
| LatestMigrationVersion = 0 | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package db | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| _ "github.com/golang-migrate/migrate/v4/source/file" | ||
| "github.com/lightningnetwork/lnd/sqldb/v2" | ||
| ) | ||
|
|
||
| // NewTestPostgresDB is a helper function that creates a Postgres database for | ||
| // testing. | ||
| func NewTestPostgresDB(t *testing.T) *sqldb.PostgresStore { | ||
| t.Helper() | ||
|
|
||
| t.Logf("Creating new Postgres DB for testing") | ||
|
|
||
| sqlFixture := sqldb.NewTestPgFixture( | ||
| t, sqldb.DefaultPostgresFixtureLifetime, | ||
| ) | ||
| t.Cleanup(func() { | ||
| sqlFixture.TearDown(t) | ||
| }) | ||
|
|
||
| return sqldb.NewTestPostgresDB(t, sqlFixture, FaradayMigrationSets) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package db | ||
|
|
||
| import ( | ||
| "embed" | ||
| _ "embed" | ||
| ) | ||
|
|
||
| var sqlSchemas embed.FS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package db | ||
|
|
||
| import ( | ||
| "github.com/golang-migrate/migrate/v4" | ||
| "github.com/golang-migrate/migrate/v4/database/pgx/v5" | ||
| "github.com/lightningnetwork/lnd/sqldb/v2" | ||
| ) | ||
|
|
||
| var ( | ||
| FaradayMigrationSet = sqldb.MigrationSet{ | ||
| TrackingTableName: pgx.DefaultMigrationsTable, | ||
| SQLFileDirectory: "sqlc/migrations", | ||
| SQLFiles: sqlSchemas, | ||
|
|
||
| // LatestMigrationVersion is the latest migration version of the | ||
| // database. This is used to implement downgrade protection for | ||
| // the daemon. | ||
| // | ||
| // NOTE: This MUST be updated when a new migration is added. | ||
| LatestMigrationVersion: LatestMigrationVersion, | ||
|
|
||
| MakeProgrammaticMigrations: func( | ||
| db *sqldb.BaseDB, | ||
| ) (map[uint]migrate.ProgrammaticMigrEntry, error) { | ||
|
|
||
| return make(map[uint]migrate.ProgrammaticMigrEntry), nil | ||
| }, | ||
| } | ||
| FaradayMigrationSets = []sqldb.MigrationSet{FaradayMigrationSet} | ||
| ) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package db | ||
|
|
||
| import ( | ||
| _ "modernc.org/sqlite" // Register relevant drivers. | ||
| ) | ||
|
|
||
| // SqliteConfig holds all the config arguments needed to interact with our | ||
| // sqlite DB. | ||
| // | ||
| // nolint: lll | ||
| type SqliteConfig struct { | ||
| // SkipMigrations if true, then all the tables will be created on start | ||
| // up if they don't already exist. | ||
| SkipMigrations bool `long:"skipmigrations" description:"Skip applying migrations on startup."` | ||
|
|
||
| // SkipMigrationDbBackup if true, then a backup of the database will not | ||
| // be created before applying migrations. | ||
| SkipMigrationDbBackup bool `long:"skipmigrationdbbackup" description:"Skip creating a backup of the database before applying migrations."` | ||
|
|
||
| // DatabaseFileName is the full file path where the database file can be | ||
| // found. | ||
| DatabaseFileName string `long:"dbfile" description:"The full path to the database."` | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should add this check to the CI workflow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I'll add that when we have the migrations since it would fail at the moment:
sed: can't read db/sqlc/migrations/*.up.sql: No such file or directoryThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense :)