mirror of
https://github.com/whekin/household-bot.git
synced 2026-03-31 15:34:03 +00:00
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
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
import { defineConfig } from 'drizzle-kit'
|
||||
|
||||
const dbCredentials = process.env.DATABASE_URL
|
||||
? {
|
||||
url: process.env.DATABASE_URL
|
||||
}
|
||||
: undefined
|
||||
|
||||
export default defineConfig({
|
||||
dialect: 'postgresql',
|
||||
schema: './packages/db/src/schema.ts',
|
||||
out: './packages/db/drizzle',
|
||||
dbCredentials
|
||||
dbCredentials: {
|
||||
url: process.env.DATABASE_URL!
|
||||
},
|
||||
migrations: {
|
||||
schema: process.env.DB_SCHEMA || 'public',
|
||||
table: '__drizzle_migrations'
|
||||
}
|
||||
})
|
||||
|
||||
1
packages/db/drizzle/0020_natural_mauler.sql
Normal file
1
packages/db/drizzle/0020_natural_mauler.sql
Normal file
@@ -0,0 +1 @@
|
||||
ALTER TABLE "household_billing_settings" ADD COLUMN "rent_payment_destinations" jsonb;
|
||||
3447
packages/db/drizzle/meta/0020_snapshot.json
Normal file
3447
packages/db/drizzle/meta/0020_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -141,6 +141,13 @@
|
||||
"when": 1773327708167,
|
||||
"tag": "0019_faithful_madame_masque",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 20,
|
||||
"version": "7",
|
||||
"when": 1773590603863,
|
||||
"tag": "0020_natural_mauler",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -7,9 +7,19 @@ export interface DbClientOptions {
|
||||
}
|
||||
|
||||
export function createDbClient(databaseUrl: string, options: DbClientOptions = {}) {
|
||||
const dbSchema = process.env.DB_SCHEMA || 'public'
|
||||
|
||||
const queryClient = postgres(databaseUrl, {
|
||||
max: options.max ?? 5,
|
||||
prepare: options.prepare ?? false
|
||||
prepare: options.prepare ?? false,
|
||||
onnotice: () => {},
|
||||
connection: {
|
||||
search_path: dbSchema
|
||||
},
|
||||
transform: {
|
||||
...postgres.camel,
|
||||
undefined: null
|
||||
}
|
||||
})
|
||||
|
||||
const db = drizzle(queryClient)
|
||||
|
||||
35
packages/db/src/migrate.ts
Normal file
35
packages/db/src/migrate.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
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)
|
||||
Reference in New Issue
Block a user