Skip to content
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

[sql-6] accounts: SQL boilerplate and accounts schemas and queries #953

Merged
merged 3 commits into from
Jan 29, 2025

Conversation

ellemouton
Copy link
Member

@ellemouton ellemouton commented Jan 23, 2025

don't fear the large number of changed lines - this is mostly boilerplate and generated code

Fixes #956

Here, we add the set of account tables and queries we will need to implement the SQL version of the accounts store.

Since this is the first PR that makes use of sqlc, it also includes all the boilerplate code we need. The boiler plate code is copied from the taproot-assets repo.

While it would have been ideal to have a single PR that adds the boiler plate code and then a separate one for adding the accounts schemas, this is not possible due to the make sqlc command not working if no queries are defined.

Context:

In our current KVDB store impl of the accounts store, we use an AccountID to refer to a unique account. It is an 8 byte slice that uniquely identifies an account. These 8 bytes are used by users (via CLI) to interact with a specific account AND these 8 bytes are also embedded into the caveat of the macaroon generated for interacting with the account so that when a request comes in, we can check the macaroon caveat to see if it is aimed at a specific account.

In SQL land, it is standard to use an auto-incrementing ID (int64) to refer to records. So in our SQL implementation, we will use int64 IDs for accounts and any relations at the SQL level will refer to accounts using its int64 ID. However, we obviously want to remain backwards compatible with any existing accounts which use the old AccountID identifier. And so what we will do here is to have both an id (int64) field and an alias (BLOB NOT NULL UNIQUE) field which will contain the 8 byte AccountID. I've called it alias instead of legacy_id since we can't really deprecate this field ever if we want to remain backwards compatible.

Then, for any queries, we always query by id at the SQL level since that is more idiomatic. So then at the CRUD level, we will first query the id linked to an alias and then continue with the query from there.

Reviewing:

If you'd like to see how this code is used (ie, the CRUD) then it might help to glance at #954 while reviewing this.

But to keep review load manageable, when reviewing this PR, try to just focus on correctness of the schemas and queries. We can always do minor tweaks to the queries later on if we realise things on the CRUD layer need to change.

Schemas and relations:

sql-accounts drawio

Copy link
Contributor

@bitromortac bitromortac left a comment

Choose a reason for hiding this comment

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

Did a first pass and only looked at the schemas. I've got a few questions to build up knowledge in the SQL area.

db/sqlc/migrations/000001_accounts.up.sql Show resolved Hide resolved
db/sqlc/migrations/000001_accounts.up.sql Outdated Show resolved Hide resolved
db/sqlc/migrations/000001_accounts.up.sql Outdated Show resolved Hide resolved
db/sqlc/migrations/000001_accounts.up.sql Outdated Show resolved Hide resolved
db/sqlc/migrations/000001_accounts.down.sql Outdated Show resolved Hide resolved
Copy link
Contributor

@bitromortac bitromortac left a comment

Choose a reason for hiding this comment

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

Looking good, nice initial additions! I've got some more SQL-related questions.

Expiration time.Time
}

type AccountIndicy struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

it's the return type that would be returned if we were to have a query such as "select * from account_indices"
but we only have "select value from account_indices where name = x", so this doesnt get used. But it is still generated

Copy link
Contributor

Choose a reason for hiding this comment

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

I found it strange that it singularizes AccountIndicies to Indicy 😄, I think it uses https://github.com/jinzhu/inflection under the hood. ah, I think we should change it to account_indices then it gets converted to AccountIndex

Copy link
Member Author

Choose a reason for hiding this comment

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

indeed! cool - updated :)

db/sqlc/queries/accounts.sql Outdated Show resolved Hide resolved
db/sqlc/queries/accounts.sql Outdated Show resolved Hide resolved
Copy link
Contributor

@ViktorTigerstrom ViktorTigerstrom left a comment

Choose a reason for hiding this comment

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

Niiiiice, uTACK LGTM 🚀🔥! Leaving two comments which probably falls out of scope for this PR.

db/sqlc/migrations/000001_accounts.up.sql Show resolved Hide resolved
db/sqlc/queries/accounts.sql Outdated Show resolved Hide resolved
Copy link
Contributor

@bitromortac bitromortac left a comment

Choose a reason for hiding this comment

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

LGTM 🎉 nice to see this ⚡ (one suggestion left)

Expiration time.Time
}

type AccountIndicy struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

I found it strange that it singularizes AccountIndicies to Indicy 😄, I think it uses https://github.com/jinzhu/inflection under the hood. ah, I think we should change it to account_indices then it gets converted to AccountIndex

This commit also contains the sqlc.yaml file, the `make sqlc` command
and the script for generating sqlc code. This must be done in this
commit as the script only works if there are queries to generate from.
This commit adds the boilerplate code we will need in order to start
using SQL backed stores (namely sqlite and postgres) for our DB backend.

NOTE that this has been copied from the taproot-assets repo.
@ellemouton ellemouton merged commit ac160bb into lightninglabs:master Jan 29, 2025
16 of 17 checks passed
@ellemouton ellemouton deleted the sql6Accounts6 branch January 29, 2025 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-changelog This PR is does not require a release notes entry sql-ize
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[sqlize] Accounts: define schemas & queries
3 participants