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

tapdb: add minimalistic test framework for testing DB migrations #707

Merged
merged 7 commits into from
Dec 9, 2023

Conversation

guggero
Copy link
Member

@guggero guggero commented Nov 29, 2023

This is an attempt at solving #694.
There are many ways to solve this, and this is IMO the most easy to implement one.

So this is mainly a proposal to get the discussion started. If we feel like we need something more involved, we can discuss based on this example code.

I mainly had this PR in mind when coming up with the design for this: #684
There we need to have a way to insert existing data into the DB before applying the latest migration.

Here are the pros/cons of the current approach:

Pros:

  • Simple to implement, understand, use and maintain
  • Should allow us to fully test data manipulation migrations
  • No need to carry along multiple versions of Golang model code
  • No impact on production code or run time

Cons:

  • Not very easy to test schema manipulation migrations, since Golang model is always the very latest version
  • If we need to test changes to the actual schema, we need to do it with raw SQL queries
  • Need to manually create test data SQL code

Fixes #694

This commit extracts the actual execution of the DB migration code into
its separate method.
This commit allows us to either run all migrations or up to a given
version on the respective stores. This will enable us to test individual
migration steps in unit tests.
This commit adds database specific detection for errors that represent a
mismatch between the query and the actual database schema.
This will mainly be used for unit tests and should never occur in
production code.
This was found in the unit test that will be added in the next commit.
We need to wrap database errors in order for us to parse them correctly
as database specific typed errors.
This test is mostly a demo for how to use the migration testing
framework.
We start with the DB that only contains the very first migration file,
which we assert by making sure we encounter a schema error when
attempting to read assets.
We then move forward to version 11, insert some dummy data, then run all
migrations up to the latest version.
@guggero guggero requested review from Roasbeef and ffranr November 29, 2023 20:06
@guggero guggero linked an issue Nov 29, 2023 that may be closed by this pull request
@bhandras
Copy link
Member

Super cool!

@GeorgeTsagk
Copy link
Member

Thanks for opening this PR!

One question I'd have w.r.t to the approach is what kind of tests we're exactly running here?
e.g if we already have 10 migrations in the DB and a new PR is trying to add n.11, are we gonna test that migration against all possible states a tapd could be upgrading from?

Copy link
Contributor

@ffranr ffranr left a comment

Choose a reason for hiding this comment

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

Migration changes make sense to me. Nice PR, very clear! I'm not sure where you're going with the last commit. But I think we should merge regardless.

@guggero
Copy link
Member Author

guggero commented Dec 4, 2023

e.g if we already have 10 migrations in the DB and a new PR is trying to add n.11, are we gonna test that migration against all possible states a tapd could be upgrading from?

The idea is that we have a way to test migrations that do data manipulations. Those are quite hard to test with just a unit test that looks at the state after all migrations have been applied. One of those PRs is #684 (which was taken as the inspiring PR to influence some of the design in this PR).

@jharveyb jharveyb self-requested a review December 8, 2023 20:39
Copy link
Contributor

@jharveyb jharveyb left a comment

Choose a reason for hiding this comment

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

Very neat! I think this would have worked for validating the latest changes to the anchor_txid view, and repro'ing that user issue.

Re: test data; I imagine we could generate a 'golden' DB state from one of itests and use that for testing future migrations. Feels like using this in future tests shouldn't be too much work.

Needs rebase.

Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

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

Was a bit skeptical initially, but I think this nicely resolves issues that would pop up in a purely unit test approach, namely: we wouldn't be able to programmatically create older versions of certain tables as the Go code is generated from the latest version of the migration.

It's also the case that all the generated code already has SQL fragments in them, and you can look at the older versions to extract raw SQL used for the insertion files.

LGTM 🥕

@@ -861,7 +861,7 @@ func fetchAssetsWithWitness(ctx context.Context, q ActiveAssetsStore,
// First, we'll fetch all the assets we know of on disk.
dbAssets, err := q.QueryAssets(ctx, assetFilter)
if err != nil {
return nil, nil, fmt.Errorf("unable to read db assets: %v", err)
return nil, nil, fmt.Errorf("unable to read db assets: %w", err)
Copy link
Member

Choose a reason for hiding this comment

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

👍

@Roasbeef Roasbeef merged commit bb38de7 into main Dec 9, 2023
13 of 14 checks passed
@guggero guggero deleted the migrations-tests branch December 11, 2023 08:40
bhandras added a commit to bhandras/lnd that referenced this pull request Jun 27, 2024
bhandras added a commit to bhandras/lnd that referenced this pull request Jun 28, 2024
bhandras added a commit to bhandras/lnd that referenced this pull request Jun 28, 2024
bhandras added a commit to bhandras/lnd that referenced this pull request Jul 2, 2024
bhandras added a commit to bhandras/lnd that referenced this pull request Jul 3, 2024
bhandras added a commit to bhandras/lnd that referenced this pull request Jul 8, 2024
bhandras added a commit to bhandras/lnd that referenced this pull request Jul 9, 2024
MPins pushed a commit to MPins/lnd that referenced this pull request Jul 22, 2024
MPins pushed a commit to MPins/lnd that referenced this pull request Jul 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

Add test framework for SQL DB migrations
6 participants