feat(database): manage the schema with typeorm migrations
- replace synchronize with migrations, starting with a generated initial migration that adopts databases created by synchronize - apply pending migrations in the docker container before the server, and refuse to start the server while migrations are pending - share one entity list and data source between app, migration runner, typeorm cli and tests; tests run the real migrations - add migration:run, migration:show and migration:generate scripts and a test that fails when entities and migrations differ - exit with code 1 when startup fails - bump version to 1.20.0
This commit is contained in:
1 parent
5de9272281
commit
9f341719ef
17 files changed
+526
-80
No files matched your search
+19
-1
@@ -1,6 +1,7 @@
|
||||
import { RequestMethod, ValidationPipe } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { NestFactory } from "@nestjs/core";
|
||||
import { DataSource } from "typeorm";
|
||||
import type { NestExpressApplication } from "@nestjs/platform-express";
|
||||
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
|
||||
import { AppModule } from "./app.module";
|
||||
@@ -36,6 +37,15 @@ async function bootstrap() {
|
||||
logger: new TraceLogger("Bootstrap", { timestamp: true }),
|
||||
});
|
||||
|
||||
// The schema is managed by migrations; refuse to run against an outdated one.
|
||||
// Checked before app.init(), i.e. before any module touches the tables.
|
||||
if (await app.get(DataSource).showMigrations()) {
|
||||
throw new Error(
|
||||
"The database has pending migrations. Run `npm -w server run migration:run` " +
|
||||
"(the Docker image runs them automatically before starting the server).",
|
||||
);
|
||||
}
|
||||
|
||||
app.setGlobalPrefix(API_PREFIX, {
|
||||
exclude: [
|
||||
{ path: "health", method: RequestMethod.ALL },
|
||||
@@ -77,4 +87,12 @@ async function bootstrap() {
|
||||
logger.log(`MCP endpoint available at http://localhost:${port}/mcp`);
|
||||
}
|
||||
|
||||
bootstrap();
|
||||
// Startup failures (pending migrations, port in use, ...) must stop the
|
||||
// process with an error code; the handlers above only keep a running server alive
|
||||
bootstrap().catch((err: unknown) => {
|
||||
processLogger.error(
|
||||
`Startup failed: ${err instanceof Error ? err.message : String(err)}`,
|
||||
err instanceof Error ? err.stack : undefined,
|
||||
);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in new issue
Block a user