Appearance
Database Migrations
This directory contains database migrations for MonoTask.
Migration System
MonoTask uses a simple migration tracking system:
- Migrations are numbered sequentially (e.g.,
002-add-feature.ts,003-update-schema.ts) - Applied migrations are tracked in the
schema_migrationstable - The baseline schema (
001-baseline) was created on 2025-10-19 and represents the consolidation of all previous migrations
Creating a New Migration
Create a new file in this directory with the format:
{version}-{description}.ts- Version should be the next sequential number (e.g.,
002,003) - Description should be kebab-case and descriptive
- Version should be the next sequential number (e.g.,
Use this template:
typescript
#!/usr/bin/env bun
import type { Migration } from '@monotask/shared/migration-types';
import type { Database } from 'bun:sqlite';
const migration: Migration = {
version: '002-your-feature',
name: 'Add your feature description',
async up(db: Database) {
// Apply migration
db.run(`
CREATE TABLE your_table (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
)
`);
},
async down(db: Database) {
// Rollback migration (optional but recommended)
db.run('DROP TABLE IF EXISTS your_table');
}
};
export default migration;- Test your migration:
bash
# Check current status
bun run db:migrate:status
# Run pending migrations
bun run db:migrate
# Verify the migration was applied
bun run db:migrate:statusMigration Best Practices
- Atomic Changes: Each migration should represent one logical change
- Idempotent: Use
IF NOT EXISTS,IF EXISTSwhere possible - Reversible: Provide a
downfunction when possible for rollbacks - Test First: Test migrations on a copy of your database first
- No Data Loss: Be careful with
DROPstatements; backup data first - Sequential Numbering: Use the next available number in sequence
Available Commands
bash
# Initialize a fresh database (with baseline schema)
bun run db:init
# Run all pending migrations
bun run db:migrate
# Check migration status
bun run db:migrate:status
# Reset database (WARNING: Deletes all data)
bun run db:resetMigration History
001-baseline: Consolidated schema from all migrations up to 2025-10-19- See
scripts/init-database-LEGACY.tsfor previous migration history - 73 tables, full production schema
- See
Troubleshooting
Migration failed partway through
- Check
schema_migrationstable to see what was applied - Fix the issue in the migration file
- Re-run
bun run db:migrate
Need to rollback a migration
- Migrations with
downfunctions can be rolled back - Currently, manual rollback is required (automatic rollback coming soon)
Migration already applied error
- Check
bun run db:migrate:statusto see applied migrations - Ensure you're using a unique version number
See Also
packages/shared/src/migration-types.ts- Migration type definitionspackages/shared/src/migration-runner.ts- Migration runner utilitiesscripts/run-migrations.ts- Migration execution scriptscripts/migration-status.ts- Status checking script