fix: Produktions-Crash durch TypeORM-synchronize beheben
Build and Push Multi-Platform Images / build-and-push (push) Successful in 44s
Build and Push Multi-Platform Images / build-and-push (push) Successful in 44s
NODE_ENV=production deaktiviert synchronize (zerstörerischer ADD/DROP-COLUMN- Churn auf MariaDB, der die 8126-Byte-Zeilengröße sprengte) und aktiviert migrationsRun. Neue data-source.ts als einzige Konfigquelle (Laufzeit + CLI), Migrations-Workflow (generate/run/revert) inkl. dotenv ergänzt. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
import 'reflect-metadata';
|
||||
import { join } from 'path';
|
||||
import { config as loadEnv } from 'dotenv';
|
||||
import { DataSource, DataSourceOptions } from 'typeorm';
|
||||
import {
|
||||
Client,
|
||||
DocumentType,
|
||||
DocumentField,
|
||||
Task,
|
||||
Postprocessing,
|
||||
PostprocessingAction,
|
||||
PostprocessingLog,
|
||||
ExportTarget,
|
||||
Setting,
|
||||
Kontonummer,
|
||||
Document,
|
||||
UserClient,
|
||||
Email,
|
||||
Attachment,
|
||||
Content,
|
||||
ApiKey,
|
||||
CorrespondentSetting,
|
||||
BarcodeTemplate,
|
||||
InboxDocument,
|
||||
InboxPostprocessingAction,
|
||||
CorrespondentEmailMapping,
|
||||
UserSettings,
|
||||
LabelPrintJob,
|
||||
} from './entities';
|
||||
|
||||
// CLI-Kontext: .env laden (Laufzeit im Container liefert die Variablen via Docker,
|
||||
// dotenv ist dort ein No-Op). dotenv überschreibt bereits gesetzte Variablen nicht.
|
||||
loadEnv();
|
||||
loadEnv({ path: join(process.cwd(), '..', '.env') });
|
||||
|
||||
export const entities = [
|
||||
Client,
|
||||
DocumentType,
|
||||
DocumentField,
|
||||
Task,
|
||||
Postprocessing,
|
||||
PostprocessingAction,
|
||||
PostprocessingLog,
|
||||
ExportTarget,
|
||||
Setting,
|
||||
Kontonummer,
|
||||
Document,
|
||||
UserClient,
|
||||
Email,
|
||||
Attachment,
|
||||
Content,
|
||||
ApiKey,
|
||||
CorrespondentSetting,
|
||||
BarcodeTemplate,
|
||||
InboxDocument,
|
||||
InboxPostprocessingAction,
|
||||
CorrespondentEmailMapping,
|
||||
UserSettings,
|
||||
LabelPrintJob,
|
||||
];
|
||||
|
||||
const isProduction = process.env.NODE_ENV === 'production';
|
||||
|
||||
export const dataSourceOptions: DataSourceOptions = {
|
||||
type: 'mysql',
|
||||
host: process.env.DB_HOST ?? 'localhost',
|
||||
port: Number(process.env.DB_PORT ?? 3306),
|
||||
username: process.env.DB_USERNAME ?? 'root',
|
||||
password: process.env.DB_PASSWORD ?? '',
|
||||
database: process.env.DB_DATABASE ?? 'paperlessadd',
|
||||
charset: 'utf8mb4',
|
||||
entities,
|
||||
migrations: [join(__dirname, 'migrations', '*.{ts,js}')],
|
||||
// In Produktion: kein synchronize (zerstörerischer ALTER-Churn), Migrationen
|
||||
// werden beim Start automatisch ausgeführt. In Dev: synchronize wie bisher.
|
||||
synchronize: !isProduction,
|
||||
migrationsRun: isProduction,
|
||||
};
|
||||
|
||||
// Wird von der TypeORM-CLI verwendet (migration:generate / :run / :revert).
|
||||
export default new DataSource(dataSourceOptions);
|
||||
@@ -1,75 +1,12 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import {
|
||||
Client,
|
||||
DocumentType,
|
||||
DocumentField,
|
||||
Task,
|
||||
Postprocessing,
|
||||
PostprocessingAction,
|
||||
PostprocessingLog,
|
||||
ExportTarget,
|
||||
Setting,
|
||||
Kontonummer,
|
||||
Document,
|
||||
UserClient,
|
||||
Email,
|
||||
Attachment,
|
||||
Content,
|
||||
ApiKey,
|
||||
CorrespondentSetting,
|
||||
BarcodeTemplate,
|
||||
InboxDocument,
|
||||
InboxPostprocessingAction,
|
||||
CorrespondentEmailMapping,
|
||||
UserSettings,
|
||||
LabelPrintJob,
|
||||
} from './entities';
|
||||
|
||||
const entities = [
|
||||
Client,
|
||||
DocumentType,
|
||||
DocumentField,
|
||||
Task,
|
||||
Postprocessing,
|
||||
PostprocessingAction,
|
||||
PostprocessingLog,
|
||||
ExportTarget,
|
||||
Setting,
|
||||
Kontonummer,
|
||||
Document,
|
||||
UserClient,
|
||||
Email,
|
||||
Attachment,
|
||||
Content,
|
||||
ApiKey,
|
||||
CorrespondentSetting,
|
||||
BarcodeTemplate,
|
||||
InboxDocument,
|
||||
InboxPostprocessingAction,
|
||||
CorrespondentEmailMapping,
|
||||
UserSettings,
|
||||
LabelPrintJob,
|
||||
];
|
||||
import { dataSourceOptions, entities } from './data-source';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forRootAsync({
|
||||
imports: [ConfigModule],
|
||||
inject: [ConfigService],
|
||||
useFactory: (config: ConfigService) => ({
|
||||
type: 'mysql' as const,
|
||||
host: config.get<string>('DB_HOST', 'localhost'),
|
||||
port: config.get<number>('DB_PORT', 3306),
|
||||
username: config.get<string>('DB_USERNAME', 'root'),
|
||||
password: config.get<string>('DB_PASSWORD', ''),
|
||||
database: config.get<string>('DB_DATABASE', 'paperlessadd'),
|
||||
entities,
|
||||
synchronize: config.get<string>('NODE_ENV') !== 'production',
|
||||
charset: 'utf8mb4',
|
||||
}),
|
||||
}),
|
||||
// Eine einzige Konfigurationsquelle (data-source.ts), geteilt zwischen
|
||||
// NestJS-Laufzeit und TypeORM-CLI (Migrationen).
|
||||
TypeOrmModule.forRoot(dataSourceOptions),
|
||||
TypeOrmModule.forFeature(entities),
|
||||
],
|
||||
exports: [TypeOrmModule],
|
||||
|
||||
Reference in New Issue
Block a user