Skip to content

[sql-45] package queries & models present at the time of the kvdb to sql migration #1113

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

Draft
wants to merge 20 commits into
base: master
Choose a base branch
from

Conversation

ViktorTigerstrom
Copy link
Contributor

Based on and blocked by #1085

Implements step 2. of "Phase 3" in #917.

This is a draft PR that introduces a new sqlc package called sqlcmig6, which packages the exact SQL queries & models present at the time of the kvdb to sql migration. This PR then also updates the kvdb to sql migrations to use the queries & models from that package, instead of the standard sqlc queries. This is required, as the sqlc package, queries & models may change in such a way in the future, that they are no longer compatible with the kvdb to sql migrations, and we therefore need to enable functionality that runs the migrations with the exact queries & models present at the time of when the kvdb to sql migration was introduced.

Note that this is only a draft PR for now, as the sqldb/v2 is not released, and we therefore need to add forked+local dependencies in order to run this. Once we have an official release of the sqldb/v2 package, I'll update this PR to use that dependency.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @ViktorTigerstrom, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request is a crucial step towards solidifying our database migration strategy. My primary goal is to decouple the KVDB to SQL migration process from ongoing schema evolution in the main application. By packaging the exact database queries and models used at the time of migration into a dedicated, versioned sqlcmig6 module, I'm ensuring that these critical historical migrations remain compatible and functional, regardless of future database changes. This also involves updating existing migration paths and refining our database interaction patterns for improved robustness and clarity.

Highlights

  • Migration Stability: I've introduced a new sqlcmig6 package. This package specifically contains the sqlc generated queries and models exactly as they were at the time of the KVDB to SQL migration. This ensures that future changes to the main sqlc package won't break historical migration logic, providing long-term stability for the database migration process.
  • Migration Code Updates: The existing KVDB to SQL migration logic for accounts and sessions has been updated to utilize the newly introduced sqlcmig6 package. This involves changing import paths, function signatures, and parameter types to align with the frozen schema and queries of sqlcmig6.
  • Database Abstraction Layer Refinement: The underlying database abstraction layer has been improved. This includes updating db.BaseDB to sqldb.BaseDB, introducing SQLQueriesExecutor and SQLMig6QueriesExecutor for better query management, and standardizing transaction execution with sqldb.NoOpReset across various store operations. This enhances flexibility and maintainability for different SQL backends.
  • KVStore Schema Evolution: The kvstores table schema has been updated to rename session_id to group_id. This change is reflected across the sqlc generated code, SQL migration scripts, and related Go structs and functions, improving clarity and consistency in how session groups are referenced.
  • Firewall DB Migration Framework: A new migration framework for the firewalldb has been added, specifically for KV stores. This includes logic to collect data from the BoltDB, insert it into the SQL database using sqlcmig6 types, and validate the migrated data, ensuring a robust transition for firewall-related data.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a snapshot of the database queries and models in a new sqlcmig6 package to ensure the stability of kvdb-to-SQL migrations against future schema changes. It also refactors the database layer to use the new lnd/sqldb/v2 package, centralizing migration and transaction logic. The changes are extensive and well-structured, including new migration logic for firewalldb with thorough tests. My review found a couple of issues to address before this can be merged. Most critically, the go.mod file contains replace directives pointing to a personal fork and a local file path, which must be resolved. I also found a minor typo in a comment.

go.mod Outdated
Comment on lines 252 to 259
replace github.com/lightningnetwork/lnd => github.com/ViktorTigerstrom/lnd v0.0.0-20250710121612-a88fb038013b

// TODO: replace this with your own local fork
replace github.com/lightningnetwork/lnd/sqldb/v2 => ../../lnd_forked/lnd/sqldb

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

These replace directives point to a personal fork and a local file path. While this is understandable for a draft PR that depends on unreleased changes, these must be removed and replaced with official releases before this PR can be merged. The local path in particular will break CI and prevent others from building the project.


// SQLMig6KVStoreQueries is a subset of the sqlcmig6.Queries interface that can
// be used to interact with the kvstore tables.
// 

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This line contains an Apple emoji , which seems to be a typo or an artifact from copy-pasting. It should be removed to maintain code clarity and professionalism.

In upcoming commits, we will introduce support for sqldb/v2 in Lightning
Terminal, in order to support code migrations. This is needed, so that
we can ensure that the kvdb to sql migration is run exactly after an
exact sql migration version, and not after all sql migrations have been
executed. The reason why that's important is that sql table definition
may change with future sql migrations, while the kvdb to sql migration
will remain the same and always expect the same table definition.
Therefore, if the kvdb to sql migration is run after all sql migrations,
it may fail due to a table definition mismatch.

This commit adds the sqldb/v2 dependency to `litd`, so that we can
implement it's usage and execution of the kvdb to sql migration
correctly in the upcoming commits.

NOTE: currently the sqldb/v2 dependency hasn't been shipped in any
lnd release, so this dependency currently a forked version of
sqldb/v2. Therefore to use the sqldb/v2 dependency, you need to
link the dependency to your local lnd fork, which has the
sqldb/v2 code in it.
A core component of `sqldb/v2` useage, is that the package allows and
requires that the callsite defines a `sqldb.MigrationStream` that will
be run during the initialization of the `sqldb/v2 database instance.
The `sqldb.MigrationStream` defines the exact sql migrations to run,
as well as additional code migrations will be run after each individual
migration version.

This commit introduces the core definition of the migration stream for
`litd`, and will be further exteded in upcoming commits that will
introduce kvdb to sql code migration.

Note that as of this commit, as no part of the `litd` codebase uses the
`sqldb/v2` database instances, this migration stream is not yet
used.
The `sqldb/v2` package now provides a definition for the `BackendType`
type. As useage of `sqldb/v2` requires useage of that type, we update
`litd` to use the `BackendType` definition from `sqldb/v2`, instead of
it's own definition.
The upcoming implementation of `sqldb/v2` will extensively create
a `Queries` object on the fly. To make more intuitive how to create
the queries object for specific database types, we introduce a
`NewForType` helper method.
This also mimics how `tapd` creates the `Queries` object, and in order
not let `litd` have it's own definition of how `Queries` object are
created on the fly, the upcoming `sqldb/v2` usage will utilize this
helper method.
As we will change the `accounts`, `session` & `firewalldb` packages to
use the `sqldb/v2` package, we need to make those packages use the
`sqldb/v2` `PostgresStore` when setting up their test postgres
databases, instead of `litd`'s own `PostgresStore` version.

In order to enable that functionality, we add a new helper function that
creates a `PostgresStore` using the `sqldb/v2` package, in addition to
helper function that creates a `PostgresStore` using the `litd`
version.

Once we have shifted all of `litd`'s code to use the `sqldb/v2`
definition, we will remove the `litd` version.
@ViktorTigerstrom ViktorTigerstrom force-pushed the 2025-07-sqldb-v2-mig6-package branch from dc5ff92 to 9412a12 Compare July 24, 2025 12:59
@ViktorTigerstrom ViktorTigerstrom self-assigned this Jul 24, 2025
@ViktorTigerstrom ViktorTigerstrom force-pushed the 2025-07-sqldb-v2-mig6-package branch from 9412a12 to 18441a9 Compare July 24, 2025 15:16
Update the accounts package to use the `sqldb/v2` package instead of the
older version.
Update the session package to use the `sqldb/v2` package instead of the
older version.
Previous commits had forgotten to add the `ListAllKVStoresRecords` query
to the `firewalldb.SQLKVStoreQueries` interface. As that is required to
make the query useable when defining the `sqldb/v2`
`TransactionExecutor` for the `firewalldb` package, this commit
adds it to the interface.
rename `sqlStore` to `store` in the firewalldb sql migration test file,
to make the name shorted. This is done in preparation for future commits
which will lengthen the lines where `sqlStore` is used, which otherwise
would make the lines exceed the 80 character limit.
Update the firewalldb package to use the `sqldb/v2` package instead of
the older version.
As we've now switched over to using sqldb v2 for most of the db objects,
we can remove a lot of deprecated code that's no longer used in the litd
project. This commit removes that code.
As the legacy `NewTestPostgresDB` function is no longer used and has
been removed, it no longer makes sense to have a `V2` suffix on the
`NewTestPostgresV2DB` function. This commit renames it to
`NewTestPostgresDB`, to indicate that this function now replaces the
legacy function.
This commit introduces the `sqlcmig6` package, which at the time of this
commit contains the same queries and models as `sqlc` package.
Importantly though, once the kvdb to sql migration is made available in
production, the `sqlcmig6` package will not change, as it is intended
to represent the sql db as it was at the time of the migration.

The sqlcmig6 package is therefore intended to be used in the kvdb to sql
migration code, as it is will always be compatible with the sql database
when all sql migrations prior to the kvdb to sql migration are applied.

When additional sql migrations are added in the future, they may effect
the `sqlc` package in such a way that the standard `sqlc` queries and
models aren't compatible with kvdb to sql migration code any longer.

By preserving the `sqlcmig6` package, we ensure that the kvdb to sql
migration code can always use the same queries and models that were
available at the time of the migration, even if the `sqlc` package
changes in the future.

Note that the `sqlcmig6` package have not been generated by `sqlc` (the
queries and models are copied from the `sqlc` package), as it is not
intended to be changed in the future.
Add the `sqlcmig6` defintion of the the accounts queries to the accounts
package, along with a transaction executor that uses those queries.

Note that while the standard Queiries interface, that use the standard
sqlc queries, may change, the `SQLMig6Queries` interface is intended to
remain the same.
This commit updates the accounts package to use the new `sqlcmig6`
package for kvdb to SQL migration.
Add the `sqlcmig6` defintion of the the session queries to the session
package, along with a transaction executor that uses those queries.

Note that while the standard Queiries interface, that use the standard
sqlc queries, may change, the `SQLMig6Queries` interface is intended to
remain the same.
This commit updates the session package to use the new `sqlcmig6`
package for kvdb to SQL migration.
Add the `sqlcmig6` defintion of the the firewalldb queries to the
firewalldb package, along with a transaction executor that uses those
queries.

Note that while the standard Queiries interface, that use the standard
sqlc queries, may change, the `SQLMig6Queries` interface is intended to
remain the same.
As the firewalldb package kvdb to sql migration tests creates `sqlc`
models to assert the migration results, we will need to update those
call sites to instead use the `sqlcmig6` models instead, in order to
be compatible with the `SQLMig6Queries` queries.

However, since we can't update the `SQLDB` methods to use `sqlcmig6`
models as params, we need to update the test code assertion to instead
use the `SQLQueries` object directly instead of the `SQLDB` object. This
makes it easy to swap that `SQLQueries` object to a `SQLMig6Queries`
object in the commit that updates the firewalldb package to use the
`sqlcmig6` package for the kvdb to sql migration.
This commit updates the firewalldb package to use the new `sqlcmig6`
package for kvdb to SQL migration.
@ViktorTigerstrom ViktorTigerstrom force-pushed the 2025-07-sqldb-v2-mig6-package branch from 18441a9 to 06b8f1c Compare July 24, 2025 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant