db: Drizzle schemas on domain Postgres schemas, baseline migration, reorg SQL
- pgSchema tables for auth, money, house, ref, games, cs, animaldex, site, meta (lists excluded via schemaFilter; runtime list tables are managed by meta.*) - drizzle/ baseline matches the reorged DB exactly (catalog diff = 0 on rehearsal) - db/reorg: one-time SQL that moves the flat public schema (rehearsed on a prod copy) - scripts/db-migrate.ts: migrator with --init to record the baseline and a guard against running the baseline on a non-empty, unrecorded database
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import { date, index, integer, numeric, pgTable, text, unique } from 'drizzle-orm/pg-core';
|
||||
import { boolean, check, date, index, integer, numeric, text, unique } from 'drizzle-orm/pg-core';
|
||||
import { sql } from 'drizzle-orm';
|
||||
import { moneySchema } from './schemas';
|
||||
import { id, timestamps } from './helpers';
|
||||
import { user } from './auth';
|
||||
|
||||
export const moneyAccounts = pgTable(
|
||||
'money_accounts',
|
||||
export const moneyAccounts = moneySchema.table(
|
||||
'accounts',
|
||||
{
|
||||
id: id(),
|
||||
userId: text('user_id')
|
||||
@@ -15,11 +17,15 @@ export const moneyAccounts = pgTable(
|
||||
color: text('color'),
|
||||
...timestamps
|
||||
},
|
||||
(table) => [unique().on(table.userId, table.name), index().on(table.userId)]
|
||||
(table) => [
|
||||
unique().on(table.userId, table.name),
|
||||
index().on(table.userId),
|
||||
check('accounts_kind_check', sql`kind IN ('spendable','investment')`)
|
||||
]
|
||||
);
|
||||
|
||||
export const moneyTransactions = pgTable(
|
||||
'money_transactions',
|
||||
export const moneyTransactions = moneySchema.table(
|
||||
'transactions',
|
||||
{
|
||||
id: id(),
|
||||
userId: text('user_id')
|
||||
@@ -36,8 +42,8 @@ export const moneyTransactions = pgTable(
|
||||
(table) => [index().on(table.userId, table.date, table.id), index().on(table.accountId)]
|
||||
);
|
||||
|
||||
export const moneyRecurring = pgTable(
|
||||
'money_recurring',
|
||||
export const moneyRecurring = moneySchema.table(
|
||||
'recurring',
|
||||
{
|
||||
id: id(),
|
||||
userId: text('user_id')
|
||||
@@ -51,11 +57,14 @@ export const moneyRecurring = pgTable(
|
||||
accountId: integer('account_id').references(() => moneyAccounts.id, { onDelete: 'set null' }),
|
||||
...timestamps
|
||||
},
|
||||
(table) => [index().on(table.userId)]
|
||||
(table) => [
|
||||
index().on(table.userId),
|
||||
check('recurring_cadence_check', sql`cadence IN ('weekly','monthly','yearly')`)
|
||||
]
|
||||
);
|
||||
|
||||
export const moneyForecasts = pgTable(
|
||||
'money_forecasts',
|
||||
export const moneyForecasts = moneySchema.table(
|
||||
'forecasts',
|
||||
{
|
||||
id: id(),
|
||||
userId: text('user_id')
|
||||
@@ -70,8 +79,8 @@ export const moneyForecasts = pgTable(
|
||||
(table) => [index().on(table.userId, table.date)]
|
||||
);
|
||||
|
||||
export const moneyUtilitySettings = pgTable(
|
||||
'money_utility_settings',
|
||||
export const moneyUtilitySettings = moneySchema.table(
|
||||
'utility_settings',
|
||||
{
|
||||
id: id(),
|
||||
userId: text('user_id')
|
||||
@@ -83,11 +92,14 @@ export const moneyUtilitySettings = pgTable(
|
||||
billingStart: date('billing_start', { mode: 'string' }),
|
||||
...timestamps
|
||||
},
|
||||
(table) => [unique().on(table.userId, table.kind)]
|
||||
(table) => [
|
||||
unique().on(table.userId, table.kind),
|
||||
check('utility_settings_kind_check', sql`kind IN ('electricity','gas','water')`)
|
||||
]
|
||||
);
|
||||
|
||||
export const moneyUtilityReadings = pgTable(
|
||||
'money_utility_readings',
|
||||
export const moneyUtilityReadings = moneySchema.table(
|
||||
'utility_readings',
|
||||
{
|
||||
id: id(),
|
||||
userId: text('user_id')
|
||||
@@ -98,11 +110,14 @@ export const moneyUtilityReadings = pgTable(
|
||||
reading: numeric('reading', { precision: 12, scale: 3 }).notNull(),
|
||||
...timestamps
|
||||
},
|
||||
(table) => [index().on(table.userId, table.kind, table.date)]
|
||||
(table) => [
|
||||
index().on(table.userId, table.kind, table.date),
|
||||
check('utility_readings_kind_check', sql`kind IN ('electricity','gas','water')`)
|
||||
]
|
||||
);
|
||||
|
||||
export const moneyWishlist = pgTable(
|
||||
'money_wishlist',
|
||||
export const moneyWishlist = moneySchema.table(
|
||||
'wishlist',
|
||||
{
|
||||
id: id(),
|
||||
userId: text('user_id')
|
||||
@@ -117,8 +132,8 @@ export const moneyWishlist = pgTable(
|
||||
(table) => [index().on(table.userId)]
|
||||
);
|
||||
|
||||
export const moneyProjectionSettings = pgTable(
|
||||
'money_projection_settings',
|
||||
export const moneyProjectionSettings = moneySchema.table(
|
||||
'projection_settings',
|
||||
{
|
||||
id: id(),
|
||||
userId: text('user_id')
|
||||
@@ -132,16 +147,22 @@ export const moneyProjectionSettings = pgTable(
|
||||
.notNull()
|
||||
.default('5.00'),
|
||||
contributionMode: text('contribution_mode').notNull().default('excess'),
|
||||
includeRecurring: integer('include_recurring').notNull().default(1),
|
||||
includeRecurring: boolean('include_recurring').notNull().default(true),
|
||||
fixedMonthlyCents: integer('fixed_monthly_cents').notNull().default(0),
|
||||
endDate: date('end_date', { mode: 'string' }),
|
||||
...timestamps
|
||||
},
|
||||
(table) => [unique().on(table.userId)]
|
||||
(table) => [
|
||||
unique().on(table.userId),
|
||||
check(
|
||||
'projection_settings_contribution_mode_check',
|
||||
sql`contribution_mode IN ('excess','fixed')`
|
||||
)
|
||||
]
|
||||
);
|
||||
|
||||
export const moneyInsurances = pgTable(
|
||||
'money_insurances',
|
||||
export const moneyInsurances = moneySchema.table(
|
||||
'insurances',
|
||||
{
|
||||
id: id(),
|
||||
userId: text('user_id')
|
||||
@@ -154,5 +175,8 @@ export const moneyInsurances = pgTable(
|
||||
cadence: text('cadence').notNull().default('monthly'),
|
||||
...timestamps
|
||||
},
|
||||
(table) => [index().on(table.userId)]
|
||||
(table) => [
|
||||
index().on(table.userId),
|
||||
check('insurances_cadence_check', sql`cadence IN ('weekly','monthly','yearly')`)
|
||||
]
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user