forked from wangziqi/gongxue-base
feat: Docker Compose deployment + auto migration on startup
- docker-compose.yml: 移除 MySQL 容器,连宿主机 MySQL - main.ts: 启动前通过独立 DataSource 执行 runMigrations - migration-runner.ts: 独立 migration 执行器 - app.module.ts: TypeORM 配置增加 migrations - 修复 InitialSchema 中 room_expenses 和 financial_operations 重复索引
This commit is contained in:
24
apps/server/src/migration-runner.ts
Normal file
24
apps/server/src/migration-runner.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { DataSource } from 'typeorm';
|
||||
import { InitialSchema1784520727860 } from './migrations/1784520727860-InitialSchema';
|
||||
import { config } from 'dotenv';
|
||||
|
||||
config();
|
||||
|
||||
const isMySQL = (process.env.DB_TYPE || 'sqlite') === 'mysql';
|
||||
|
||||
export async function runMigrationsOnStartup(): Promise<void> {
|
||||
const ds = new DataSource({
|
||||
type: isMySQL ? 'mysql' : 'better-sqlite3',
|
||||
host: isMySQL ? (process.env.DB_HOST || 'localhost') : undefined,
|
||||
port: isMySQL ? Number(process.env.DB_PORT || 3306) : undefined,
|
||||
username: isMySQL ? (process.env.DB_USERNAME || 'root') : undefined,
|
||||
password: isMySQL ? (process.env.DB_PASSWORD || '') : undefined,
|
||||
database: process.env.DB_DATABASE || (isMySQL ? 'dorm_billing' : 'dorm_billing.db'),
|
||||
charset: isMySQL ? 'utf8mb4' : undefined,
|
||||
migrations: [InitialSchema1784520727860],
|
||||
});
|
||||
|
||||
await ds.initialize();
|
||||
await ds.runMigrations();
|
||||
await ds.destroy();
|
||||
}
|
||||
Reference in New Issue
Block a user