Files
household-bot/packages/db/src/migrate.ts
whekin 0747973c8f feat(db): add rent_payment_destinations column and multi-schema support
- Add migration 0020 for rent_payment_destinations jsonb column
- Add DB_SCHEMA env var support for multi-schema deployments
- Create custom migrate.ts script with proper search_path handling
- Update drizzle.config.ts and client.ts to use DB_SCHEMA
- Add db_schema variable to Terraform with dev=test/prod=public defaults
- Update CD workflow to set DB_SCHEMA based on branch
2026-03-15 20:26:26 +04:00

36 lines
992 B
TypeScript

import postgres from 'postgres'
import { drizzle } from 'drizzle-orm/postgres-js'
import { migrate } from 'drizzle-orm/postgres-js/migrator'
import path from 'path'
const databaseUrl = process.env.DATABASE_URL
if (!databaseUrl) {
throw new Error('DATABASE_URL is not set')
}
const dbSchema = process.env.DB_SCHEMA || 'public'
console.log(`Running migrations for schema: ${dbSchema}...`)
const migrationClient = postgres(databaseUrl, {
max: 1,
onnotice: () => {}
})
// Explicitly set search_path to the target schema
// This ensures that 'CREATE TABLE "x"' goes into the right schema
await migrationClient.unsafe(`SET search_path TO ${dbSchema}`)
const db = drizzle(migrationClient)
// This runs migrations from the 'drizzle' folder
await migrate(db, {
migrationsFolder: path.resolve(__dirname, '../drizzle'),
migrationsSchema: dbSchema,
migrationsTable: '__drizzle_migrations'
})
console.log('Migrations applied successfully!')
await migrationClient.end()
process.exit(0)