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
@@ -9,6 +9,7 @@ import type { NestExpressApplication } from "@nestjs/platform-express";
|
||||
import { ScheduleModule } from "@nestjs/schedule";
|
||||
|
||||
import { configureBodyParsers } from "../src/common/body-parsers";
|
||||
import { dataSourceOptions } from "../src/database/data-source";
|
||||
import { ImportModule } from "../src/import/import.module";
|
||||
|
||||
jest.mock("@nestjs/common", () => {
|
||||
@@ -42,25 +43,10 @@ import { SessionModule } from "../src/session/session.module";
|
||||
import { EnvironmentModule } from "../src/environment/environment.module";
|
||||
import { ScenarioModule } from "../src/scenario/scenario.module";
|
||||
import { McpModule } from "../src/mcp/mcp.module";
|
||||
import { SessionEntity } from "../src/session/session.entity";
|
||||
import { EnvironmentEntity } from "../src/environment/environment.entity";
|
||||
import { ScenarioEntity } from "../src/scenario/scenario.entity";
|
||||
import { ScenarioStepEntity } from "../src/scenario/scenario-step.entity";
|
||||
import { ScenarioVersionEntity } from "../src/scenario/scenario-version.entity";
|
||||
import { SnippetVersionEntity } from "../src/snippet/snippet-version.entity";
|
||||
import { ScenarioRunEntity } from "../src/scenario/scenario-run.entity";
|
||||
import { ScenarioRunStepEntity } from "../src/scenario/scenario-run-step.entity";
|
||||
import { ScenarioRunLogEntity } from "../src/scenario/scenario-run-log.entity";
|
||||
import { ScenarioCredentialEntity } from "../src/scenario/scenario-credential.entity";
|
||||
import { CredentialEntity } from "../src/credential/credential.entity";
|
||||
import { SnippetEntity } from "../src/snippet/snippet.entity";
|
||||
import { HealthController } from "../src/health/health.controller";
|
||||
import { HttpExceptionFilter } from "../src/filters/http-exception.filter";
|
||||
import { LoggingInterceptor } from "../src/interceptors/logging.interceptor";
|
||||
import { validateAppConfig } from "../src/config/app.config";
|
||||
import { FileEntity } from "../src/file/file.entity";
|
||||
import { ScenarioFileEntity } from "../src/file/scenario-file.entity";
|
||||
import { ScenarioRunFileEntity } from "../src/file/scenario-run-file.entity";
|
||||
|
||||
export async function buildTestApp(): Promise<INestApplication> {
|
||||
// Each test app stores files in its own temp dir: suites run in parallel and
|
||||
@@ -75,27 +61,10 @@ export async function buildTestApp(): Promise<INestApplication> {
|
||||
ignoreEnvFile: true,
|
||||
validate: validateAppConfig,
|
||||
}),
|
||||
// The real migrations, not synchronize, so tests use the schema production gets
|
||||
TypeOrmModule.forRoot({
|
||||
type: "better-sqlite3",
|
||||
database: ":memory:",
|
||||
entities: [
|
||||
SessionEntity,
|
||||
EnvironmentEntity,
|
||||
ScenarioEntity,
|
||||
ScenarioStepEntity,
|
||||
ScenarioVersionEntity,
|
||||
ScenarioRunEntity,
|
||||
ScenarioRunStepEntity,
|
||||
ScenarioRunLogEntity,
|
||||
ScenarioCredentialEntity,
|
||||
CredentialEntity,
|
||||
SnippetEntity,
|
||||
SnippetVersionEntity,
|
||||
FileEntity,
|
||||
ScenarioFileEntity,
|
||||
ScenarioRunFileEntity,
|
||||
],
|
||||
synchronize: true,
|
||||
...dataSourceOptions(":memory:"),
|
||||
migrationsRun: true,
|
||||
}),
|
||||
ScheduleModule.forRoot(),
|
||||
BrowserModule,
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* The schema is managed by migrations. These tests fail when an entity
|
||||
* changes without a matching migration, and cover adopting databases that
|
||||
* were created by `synchronize` before migrations existed.
|
||||
*/
|
||||
|
||||
import { DataSource } from "typeorm";
|
||||
import { dataSourceOptions } from "../src/database/data-source";
|
||||
import { MIGRATIONS } from "../src/database/migrations";
|
||||
|
||||
async function pendingSchemaChanges(dataSource: DataSource): Promise<string[]> {
|
||||
const sql = await dataSource.driver.createSchemaBuilder().log();
|
||||
return sql.upQueries.map((q) => q.query);
|
||||
}
|
||||
|
||||
describe("Database migrations", () => {
|
||||
it("migrations produce exactly the schema the entities describe", async () => {
|
||||
const dataSource = new DataSource(dataSourceOptions(":memory:"));
|
||||
await dataSource.initialize();
|
||||
try {
|
||||
await dataSource.runMigrations();
|
||||
expect(await dataSource.showMigrations()).toBe(false);
|
||||
// If this fails, generate a migration: see "Database migrations" in docs/development.md
|
||||
expect(await pendingSchemaChanges(dataSource)).toEqual([]);
|
||||
} finally {
|
||||
await dataSource.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
it("adopts a database created by synchronize", async () => {
|
||||
const dataSource = new DataSource({
|
||||
...dataSourceOptions(":memory:"),
|
||||
synchronize: true,
|
||||
});
|
||||
await dataSource.initialize();
|
||||
try {
|
||||
await dataSource.query(
|
||||
`INSERT INTO scenarios (id, name, version) VALUES ('11111111-1111-4111-8111-111111111111', 'kept', 1)`,
|
||||
);
|
||||
const applied = await dataSource.runMigrations();
|
||||
expect(applied.map((m) => m.name)).toEqual(MIGRATIONS.map((m) => m.name));
|
||||
expect(await dataSource.showMigrations()).toBe(false);
|
||||
expect(await pendingSchemaChanges(dataSource)).toEqual([]);
|
||||
const [row] = await dataSource.query(`SELECT name FROM scenarios`);
|
||||
expect(row.name).toBe("kept");
|
||||
} finally {
|
||||
await dataSource.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
it("can revert the initial migration", async () => {
|
||||
const dataSource = new DataSource(dataSourceOptions(":memory:"));
|
||||
await dataSource.initialize();
|
||||
try {
|
||||
await dataSource.runMigrations();
|
||||
await dataSource.undoLastMigration();
|
||||
const tables = await dataSource.query(
|
||||
`SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT IN ('migrations', 'sqlite_sequence')`,
|
||||
);
|
||||
expect(tables).toEqual([]);
|
||||
} finally {
|
||||
await dataSource.destroy();
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user