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

[Merged by Bors] - sql: improve database schema handling #6003

Closed
wants to merge 60 commits into from

Conversation

ivan4th
Copy link
Contributor

@ivan4th ivan4th commented Jun 1, 2024

Motivation

Currently, the database schema is always built by running a series of migrations, even on a fresh database. There's no place in the source code to look for the full database schema in a single place. If schema drift happens for whatever reason (running dev version, a bug in a coded migration, etc.), it may go undetected causing issues at some later point in time.

Another problem is that state.sql and local.sql databases aren't being handled in a consistent manner, with sql.Open / sql.InMemory being used for the state database with its associated migrations, and localsql.Open / localsql.InMemory being derived from them. This has some unpleasant consequences, most importantly that it is hard to implement coded migrations as they'll have to be referred explicitly from node/node.go and possibly in other places.

syncv2 will require some coded migration(s) for storing synctrees in the database, and ahead of these changes, it would be nice to have this kind of refactoring done in go-spacemesh.

Description

When a new database is created, use schema script instead of running all the migrations. Also, check that there's no schema drift by diffing database schema against the schema script after running migrations.

Use consistent package/folder structure for local.sql and state.sql databases:

  • sql/statesql and sql/localsql handle the state db and the local db, respectively, with Schema, InMemory and Open functions provided. Coded migrations can be added to these packages, too
  • sql/statesql/schema/schema.sql and sql/statesql/schema/schema.sql contain current database schemas
  • sql/statesql/schema/migrations/*.sql and sql/statesql/schema/migrations/*.sql contain database migrations

When a new database is created, the schema snapshot from schema.sql is used to initialize it. If the database already exists, its version is checked and if it's old, the migrations are run. In both cases, the schema is retrieved from the database afterwards and diffed against the schema snapshot, logging warnings about any detected differences (schema drift).

Fixes #6026.

Test Plan

Verify on testnet and mainnet nodes

TODO

  • Explain motivation or link existing issue(s)
  • Test changes and document test plan
  • Update changelog as needed

Use consistent package/folder structure for local.sql and state.sql
databases.

When a new database is created, use schema script instead of running
all the migrations. Also, check that there's no schema drift by
diffing database schema against the schema script after running
migrations.
@ivan4th ivan4th force-pushed the feature/schema-snapshot branch from f33a283 to 7a97f7a Compare June 1, 2024 01:39
Copy link

codecov bot commented Jun 1, 2024

Codecov Report

Attention: Patch coverage is 76.11940% with 112 lines in your changes missing coverage. Please review.

Project coverage is 82.0%. Comparing base (48b77e7) to head (2e7e150).
Report is 2 commits behind head on develop.

Files Patch % Lines
sql/schema.go 72.0% 19 Missing and 12 partials ⚠️
sql/schemagen/main.go 0.0% 24 Missing ⚠️
sql/database.go 87.1% 11 Missing and 4 partials ⚠️
sql/localsql/localsql.go 68.5% 8 Missing and 3 partials ⚠️
sql/statesql/statesql.go 72.7% 5 Missing and 4 partials ⚠️
sql/migrations.go 66.6% 5 Missing and 3 partials ⚠️
checkpoint/recovery.go 76.4% 2 Missing and 2 partials ⚠️
node/node.go 63.6% 3 Missing and 1 partial ⚠️
sql/malsync/malsync.go 66.6% 4 Missing ⚠️
syncer/malsync/syncer.go 88.8% 0 Missing and 1 partial ⚠️
... and 1 more
Additional details and impacted files
@@            Coverage Diff            @@
##           develop   #6003     +/-   ##
=========================================
- Coverage     82.0%   82.0%   -0.1%     
=========================================
  Files          307     312      +5     
  Lines        34106   34327    +221     
=========================================
+ Hits         27996   28153    +157     
- Misses        4332    4389     +57     
- Partials      1778    1785      +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ivan4th ivan4th marked this pull request as ready for review June 1, 2024 16:17
@ivan4th ivan4th requested review from dshulyak, fasmat and poszu as code owners June 1, 2024 16:17
sql/database.go Outdated Show resolved Hide resolved
sql/database.go Outdated Show resolved Hide resolved
sql/malsync/malsync.go Outdated Show resolved Hide resolved
sql/schema.go Outdated Show resolved Hide resolved
sql/schema.go Outdated Show resolved Hide resolved
sql/test/test.go Outdated Show resolved Hide resolved
go.mod Outdated Show resolved Hide resolved
sql/database.go Outdated Show resolved Hide resolved
config/config.go Outdated Show resolved Hide resolved
sql/statesql/statesql.go Outdated Show resolved Hide resolved
sql/statesql/statesql.go Outdated Show resolved Hide resolved
sql/test/test.go Outdated Show resolved Hide resolved
sql/test/test.go Outdated Show resolved Hide resolved
sql/localsql/localsql.go Outdated Show resolved Hide resolved
sql/schema.go Outdated Show resolved Hide resolved
sql/schema.go Outdated Show resolved Hide resolved
sql/schema.go Outdated Show resolved Hide resolved
sql/schema.go Outdated Show resolved Hide resolved
sql/schema.go Show resolved Hide resolved
sql/schema.go Outdated Show resolved Hide resolved
sql/localsql/schema/schema.sql Outdated Show resolved Hide resolved
sql/statesql/schema/schema.sql Outdated Show resolved Hide resolved
sql/malsync/malsync.go Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
spacemesh-bors bot added a commit that referenced this pull request Aug 13, 2024
@spacemesh-bors
Copy link

try

Build failed:

@ivan4th
Copy link
Contributor Author

ivan4th commented Aug 13, 2024

Last bors try failed due to a flake in a unittest on Windows.
The systests have passed: https://github.com/spacemeshos/go-spacemesh/actions/runs/10368810152/job/28703211607
Doing another run to ensure stability.

@ivan4th
Copy link
Contributor Author

ivan4th commented Aug 13, 2024

bors try

spacemesh-bors bot added a commit that referenced this pull request Aug 13, 2024
@spacemesh-bors
Copy link

try

Build failed:

@ivan4th
Copy link
Contributor Author

ivan4th commented Aug 15, 2024

bors try

spacemesh-bors bot added a commit that referenced this pull request Aug 15, 2024
@spacemesh-bors
Copy link

try

Build succeeded:

@ivan4th
Copy link
Contributor Author

ivan4th commented Aug 19, 2024

@fasmat @acud @poszu pls recheck this PR after the latest changes that were needed to resolve the conflicts

sql/database.go Outdated Show resolved Hide resolved
sql/localsql/localsql.go Outdated Show resolved Hide resolved
sql/statesql/statesql.go Outdated Show resolved Hide resolved
syncer/malsync/syncer.go Outdated Show resolved Hide resolved
@fasmat
Copy link
Member

fasmat commented Aug 19, 2024

Took me a bit but I made it through all changes, there is also still this comment by @poszu that is open: #6003 (comment)

@ivan4th
Copy link
Contributor Author

ivan4th commented Aug 20, 2024

Took me a bit but I made it through all changes, there is also still this comment by @poszu that is open: #6003 (comment)

Also fixed

sql/schema.go Outdated Show resolved Hide resolved
@ivan4th
Copy link
Contributor Author

ivan4th commented Aug 20, 2024

CI rerun due:

hare_test.go:653: eligibility can't be sent, waited: 10s, stacktraces:

Likely unrelated

@ivan4th
Copy link
Contributor Author

ivan4th commented Aug 20, 2024

bors merge

spacemesh-bors bot pushed a commit that referenced this pull request Aug 20, 2024
## Motivation

Currently, the database schema is always built by running a series of migrations, even on a fresh database. There's no place in the source code to look for the full database schema in a single place. If schema drift happens for whatever reason (running dev version, a bug in a coded migration, etc.), it may go undetected causing issues at some later point in time.

Another problem is that `state.sql` and `local.sql` databases aren't being handled in a consistent manner, with `sql.Open` / `sql.InMemory` being used for the state database with its associated migrations, and `localsql.Open` / `localsql.InMemory` being derived from them. This has some unpleasant consequences, most importantly that it is hard to implement coded migrations as they'll have to be referred explicitly from `node/node.go` and possibly in other places.

`syncv2` will require some coded migration(s) for storing synctrees in the database, and ahead of these changes, it would be nice to have this kind of refactoring done in `go-spacemesh`.
@spacemesh-bors
Copy link

Pull request successfully merged into develop.

Build succeeded:

@spacemesh-bors spacemesh-bors bot changed the title sql: improve database schema handling [Merged by Bors] - sql: improve database schema handling Aug 20, 2024
@spacemesh-bors spacemesh-bors bot closed this Aug 20, 2024
@spacemesh-bors spacemesh-bors bot deleted the feature/schema-snapshot branch August 20, 2024 14:02
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.

Better database schema handling
3 participants