Draft
Add automatic DB migration runner on app startup#29
Conversation
…artup Co-authored-by: kenpower <105429+kenpower@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add automatic SQL migration application on database setup
Add automatic DB migration runner on app startup
Mar 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No mechanism existed to evolve the database schema over time — the
init.sqlapproach only runs once on a fresh volume and cannot apply incremental changes to an existing database.Approach
src/lib/server/migrate.js): on each startup, creates aschema_migrationstracking table if absent, then applies any.sqlfiles fromdatabase/migrations/not yet recorded — in sorted filename order, each wrapped in a transaction.src/hooks.server.js): triggersrunMigrations()at module load; all incoming requests await completion, so a failed migration surfaces as request errors rather than silent schema drift.database/migrations/V001__initial_schema.sql): existing schema frominit.sqlconverted toIF NOT EXISTS— idempotent against databases already initialised by the old mechanism.database/into the production runner stage so migration files are available at runtime.docker-compose.yml: removes theinit.sqldocker-entrypoint-initdb.dvolume mount; migrations are now the sole owner of schema creation.Adding future migrations
Drop a new
.sqlfile intodatabase/migrations/with a name that sorts after existing ones (e.g.V002__add_column.sql). It will be applied automatically on next startup.Backward compatibility
Existing databases (initialised via the old
init.sql) are handled safely:V001usesIF NOT EXISTS, so the first run records it as applied with no destructive effect.✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.