CORE-2.4: Implement Migration Application Script
Ticket ID: CORE-2.4
Milestone: 2 - Database Connectivity & Data Integrity
Priority: 🔴 Critical
Status: Not Started
Description
Create a simple Node.js script that uses Drizzle’s migrator function to apply any pending migrations from the /migrations folder to the database.
Context
Automated migration application is essential for consistent database deployments across environments (development, staging, production). This script will be used in CI/CD pipelines and local development.
Acceptance Criteria
- ✅ An
npm run db:migratescript is added topackage.jsonthat executes the migration script - ✅ After running the script, the
users,tenants, andtenant_userstables physically exist in the PostgreSQL database - ✅ The script tracks which migrations have been applied (migration history)
- ✅ The script can be run multiple times safely (idempotent)
- ✅ The script provides clear output about which migrations were applied
- ✅ The script handles errors gracefully with informative error messages
Technical Details
Migration Script Structure
// scripts/migrate.ts
import { drizzle } from 'drizzle-orm/node-postgres';
import { migrate } from 'drizzle-orm/node-postgres/migrator';
import { Pool } from 'pg';
const pool = new Pool({
connectionString: process.env.DATABASE_URL!,
});
const db = drizzle(pool);
async function main() {
console.log('Running migrations...');
await migrate(db, { migrationsFolder: './migrations' });
console.log('Migrations completed!');
await pool.end();
}
main().catch((error) => {
console.error('Migration failed:', error);
process.exit(1);
});
Package.json Script
{
"scripts": {
"db:migrate": "tsx scripts/migrate.ts"
}
}
Implementation Notes
- Use Drizzle’s built-in migrator function
- Ensure the script connects to the correct database (from DATABASE_URL)
- Add proper error handling and logging
- Consider adding a dry-run option for testing
- Implement migration rollback capability (future enhancement)
- Add migration status checking (which migrations are applied)
Related Documentation
- Database Schema Guide - 5-tier database architecture
- OLTP Schema Guide - OLTP tier specifications
- Production Deployment - Deployment workflows
Dependencies
- CORE-2.1 - Drizzle ORM must be configured
- CORE-2.3 - Migration file must be generated
- Database connection must be available
Testing
- Run
npm run db:migrateand verify it executes without errors - Verify tables are created in the database
- Check that running the script multiple times doesn’t cause errors
- Verify migration history is tracked correctly
- Test error handling (e.g., invalid connection string, missing migrations folder)
- Verify the script works with an empty database
- Test that the script can apply multiple migrations in order
Related Documentation
- Routes: core-app-structure
- API: API Reference