-
Notifications
You must be signed in to change notification settings - Fork 215
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
Conversation
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.
f33a283
to
7a97f7a
Compare
Codecov ReportAttention: Patch coverage is
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. |
Also, add a migration that removes stray tables from quicksynced DBs
tryBuild failed: |
Last |
bors try |
tryBuild failed: |
bors try |
tryBuild succeeded: |
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 |
CI rerun due:
Likely unrelated |
bors merge |
## 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`.
Pull request successfully merged into develop. Build succeeded: |
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
andlocal.sql
databases aren't being handled in a consistent manner, withsql.Open
/sql.InMemory
being used for the state database with its associated migrations, andlocalsql.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 fromnode/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 ingo-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
andstate.sql
databases:sql/statesql
andsql/localsql
handle the state db and the local db, respectively, withSchema
,InMemory
andOpen
functions provided. Coded migrations can be added to these packages, toosql/statesql/schema/schema.sql
andsql/statesql/schema/schema.sql
contain current database schemassql/statesql/schema/migrations/*.sql
andsql/statesql/schema/migrations/*.sql
contain database migrationsWhen 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