Skip to content

Add automatic DB migration runner on app startup#29

Draft
kenpower with Copilot wants to merge 2 commits into
mainfrom
copilot/create-db-migration-mechanism
Draft

Add automatic DB migration runner on app startup#29
kenpower with Copilot wants to merge 2 commits into
mainfrom
copilot/create-db-migration-mechanism

Conversation

Copilot AI commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

No mechanism existed to evolve the database schema over time — the init.sql approach only runs once on a fresh volume and cannot apply incremental changes to an existing database.

Approach

  • Migration runner (src/lib/server/migrate.js): on each startup, creates a schema_migrations tracking table if absent, then applies any .sql files from database/migrations/ not yet recorded — in sorted filename order, each wrapped in a transaction.
  • Server hook (src/hooks.server.js): triggers runMigrations() at module load; all incoming requests await completion, so a failed migration surfaces as request errors rather than silent schema drift.
  • Initial migration (database/migrations/V001__initial_schema.sql): existing schema from init.sql converted to IF NOT EXISTS — idempotent against databases already initialised by the old mechanism.
  • Dockerfile: copies database/ into the production runner stage so migration files are available at runtime.
  • docker-compose.yml: removes the init.sql docker-entrypoint-initdb.d volume mount; migrations are now the sole owner of schema creation.

Adding future migrations

Drop a new .sql file into database/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: V001 uses IF 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.

…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
Copilot AI requested a review from kenpower March 19, 2026 14:01
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.

2 participants