- 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
25 lines
697 B
TypeScript
25 lines
697 B
TypeScript
import dataSource from "./data-source";
|
|
|
|
/**
|
|
* Applies pending migrations to the database at DB_PATH, then exits. Runs
|
|
* before the server in the Docker image (`node dist/database/migrate`).
|
|
*/
|
|
async function migrate(): Promise<void> {
|
|
await dataSource.initialize();
|
|
try {
|
|
const applied = await dataSource.runMigrations({ transaction: "each" });
|
|
console.log(
|
|
applied.length > 0
|
|
? `Applied ${applied.length} migration(s): ${applied.map((m) => m.name).join(", ")}`
|
|
: "Database schema is up to date",
|
|
);
|
|
} finally {
|
|
await dataSource.destroy();
|
|
}
|
|
}
|
|
|
|
migrate().catch((err: unknown) => {
|
|
console.error("Migration failed:", err);
|
|
process.exit(1);
|
|
});
|