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
@@ -5,29 +5,15 @@ import { ServeStaticModule } from "@nestjs/serve-static";
|
||||
import { TypeOrmModule } from "@nestjs/typeorm";
|
||||
import { BrowserModule } from "./browser/browser.module";
|
||||
import { clientStaticOptions } from "./common/client-static";
|
||||
import { dataSourceOptions } from "./database/data-source";
|
||||
import { AppConfig, validateAppConfig } from "./config/app.config";
|
||||
import { CredentialEntity } from "./credential/credential.entity";
|
||||
import { CredentialModule } from "./credential/credential.module";
|
||||
import { EnvironmentEntity } from "./environment/environment.entity";
|
||||
import { EnvironmentModule } from "./environment/environment.module";
|
||||
import { FileEntity } from "./file/file.entity";
|
||||
import { FileModule } from "./file/file.module";
|
||||
import { ScenarioFileEntity } from "./file/scenario-file.entity";
|
||||
import { ScenarioRunFileEntity } from "./file/scenario-run-file.entity";
|
||||
import { HealthModule } from "./health/health.module";
|
||||
import { ImportModule } from "./import/import.module";
|
||||
import { McpModule } from "./mcp/mcp.module";
|
||||
import { ScenarioCredentialEntity } from "./scenario/scenario-credential.entity";
|
||||
import { ScenarioRunLogEntity } from "./scenario/scenario-run-log.entity";
|
||||
import { ScenarioRunStepEntity } from "./scenario/scenario-run-step.entity";
|
||||
import { ScenarioRunEntity } from "./scenario/scenario-run.entity";
|
||||
import { ScenarioStepEntity } from "./scenario/scenario-step.entity";
|
||||
import { ScenarioVersionEntity } from "./scenario/scenario-version.entity";
|
||||
import { SnippetVersionEntity } from "./snippet/snippet-version.entity";
|
||||
import { ScenarioEntity } from "./scenario/scenario.entity";
|
||||
import { ScenarioModule } from "./scenario/scenario.module";
|
||||
import { SessionEntity } from "./session/session.entity";
|
||||
import { SnippetEntity } from "./snippet/snippet.entity";
|
||||
import { SnippetModule } from "./snippet/snippet.module";
|
||||
|
||||
@Module({
|
||||
@@ -45,28 +31,8 @@ import { SnippetModule } from "./snippet/snippet.module";
|
||||
}),
|
||||
TypeOrmModule.forRootAsync({
|
||||
inject: [ConfigService],
|
||||
useFactory: (config: ConfigService<AppConfig, true>) => ({
|
||||
type: "better-sqlite3",
|
||||
database: config.get("DB_PATH"),
|
||||
entities: [
|
||||
SessionEntity,
|
||||
EnvironmentEntity,
|
||||
CredentialEntity,
|
||||
ScenarioEntity,
|
||||
ScenarioStepEntity,
|
||||
ScenarioVersionEntity,
|
||||
ScenarioRunEntity,
|
||||
ScenarioRunStepEntity,
|
||||
ScenarioRunLogEntity,
|
||||
ScenarioCredentialEntity,
|
||||
SnippetEntity,
|
||||
SnippetVersionEntity,
|
||||
FileEntity,
|
||||
ScenarioFileEntity,
|
||||
ScenarioRunFileEntity,
|
||||
],
|
||||
synchronize: true,
|
||||
}),
|
||||
useFactory: (config: ConfigService<AppConfig, true>) =>
|
||||
dataSourceOptions(config.get("DB_PATH")),
|
||||
}),
|
||||
BrowserModule,
|
||||
EnvironmentModule,
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import * as fs from "node:fs";
|
||||
import { DataSource, type DataSourceOptions } from "typeorm";
|
||||
import { validateAppConfig } from "../config/app.config";
|
||||
import { ENTITIES } from "./entities";
|
||||
import { MIGRATIONS } from "./migrations";
|
||||
|
||||
/**
|
||||
* TypeORM options shared by the app, the migration runner and the TypeORM
|
||||
* CLI. The schema is managed by migrations only (`synchronize: false`).
|
||||
*/
|
||||
export function dataSourceOptions(database: string): DataSourceOptions {
|
||||
return {
|
||||
type: "better-sqlite3",
|
||||
database,
|
||||
entities: ENTITIES,
|
||||
migrations: MIGRATIONS,
|
||||
synchronize: false,
|
||||
};
|
||||
}
|
||||
|
||||
// Outside Nest (migration runner, TypeORM CLI) nothing loads .env; load it
|
||||
// like ConfigModule does: optional, and without overriding set variables
|
||||
if (fs.existsSync(".env")) process.loadEnvFile(".env");
|
||||
|
||||
/**
|
||||
* Data source for the TypeORM CLI and `migrate.ts`, using DB_PATH from the
|
||||
* environment or .env (default `data/liqa.db`).
|
||||
*/
|
||||
export default new DataSource(
|
||||
dataSourceOptions(validateAppConfig(process.env).DB_PATH),
|
||||
);
|
||||
@@ -0,0 +1,34 @@
|
||||
import { CredentialEntity } from "../credential/credential.entity";
|
||||
import { EnvironmentEntity } from "../environment/environment.entity";
|
||||
import { FileEntity } from "../file/file.entity";
|
||||
import { ScenarioFileEntity } from "../file/scenario-file.entity";
|
||||
import { ScenarioRunFileEntity } from "../file/scenario-run-file.entity";
|
||||
import { ScenarioCredentialEntity } from "../scenario/scenario-credential.entity";
|
||||
import { ScenarioRunLogEntity } from "../scenario/scenario-run-log.entity";
|
||||
import { ScenarioRunStepEntity } from "../scenario/scenario-run-step.entity";
|
||||
import { ScenarioRunEntity } from "../scenario/scenario-run.entity";
|
||||
import { ScenarioStepEntity } from "../scenario/scenario-step.entity";
|
||||
import { ScenarioVersionEntity } from "../scenario/scenario-version.entity";
|
||||
import { ScenarioEntity } from "../scenario/scenario.entity";
|
||||
import { SessionEntity } from "../session/session.entity";
|
||||
import { SnippetVersionEntity } from "../snippet/snippet-version.entity";
|
||||
import { SnippetEntity } from "../snippet/snippet.entity";
|
||||
|
||||
/** Every TypeORM entity of the app (used by the app, the migrations CLI and tests). */
|
||||
export const ENTITIES = [
|
||||
SessionEntity,
|
||||
EnvironmentEntity,
|
||||
CredentialEntity,
|
||||
ScenarioEntity,
|
||||
ScenarioStepEntity,
|
||||
ScenarioVersionEntity,
|
||||
ScenarioRunEntity,
|
||||
ScenarioRunStepEntity,
|
||||
ScenarioRunLogEntity,
|
||||
ScenarioCredentialEntity,
|
||||
SnippetEntity,
|
||||
SnippetVersionEntity,
|
||||
FileEntity,
|
||||
ScenarioFileEntity,
|
||||
ScenarioRunFileEntity,
|
||||
];
|
||||
@@ -0,0 +1,24 @@
|
||||
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);
|
||||
});
|
||||
@@ -0,0 +1,277 @@
|
||||
import { MigrationInterface, QueryRunner } from "typeorm";
|
||||
|
||||
/**
|
||||
* Initial schema. Databases created before migrations existed (by TypeORM
|
||||
* `synchronize`) already have this schema: for them this migration only gets
|
||||
* recorded as applied, and later migrations run from there.
|
||||
*/
|
||||
export class Initial1790793447827 implements MigrationInterface {
|
||||
name = "Initial1790793447827";
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
if (await queryRunner.hasTable("scenarios")) return;
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "credentials" ("id" varchar PRIMARY KEY NOT NULL, "name" varchar NOT NULL, "data" text, "lastUsedAt" datetime, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "environments" ("id" varchar PRIMARY KEY NOT NULL, "name" varchar NOT NULL, "description" text, "data" text NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_9d00da4836959b86d7590f60c57" UNIQUE ("name"))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "files" ("id" varchar PRIMARY KEY NOT NULL, "originalName" varchar NOT NULL, "mimeType" varchar NOT NULL, "size" integer NOT NULL, "sha256" varchar NOT NULL, "filePath" varchar NOT NULL, "expiresAt" datetime, "createdAt" datetime NOT NULL DEFAULT (datetime('now')))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "scenario_credentials" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "credentialId" varchar NOT NULL, "alias" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_02756536236c408dbb58b3fc964" UNIQUE ("scenarioId", "alias"))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "scenario_steps" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "order" integer NOT NULL DEFAULT (0), "title" text, "execCode" text, "timeoutSeconds" integer, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "scenarios" ("id" varchar PRIMARY KEY NOT NULL, "name" varchar NOT NULL, "description" text, "environmentId" varchar, "timeoutSeconds" integer, "version" integer NOT NULL DEFAULT (1), "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "scenario_files" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "fileId" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "sessions" ("id" varchar PRIMARY KEY NOT NULL, "sessionName" varchar NOT NULL, "token" text NOT NULL, "cookies" text NOT NULL, "localStorage" text NOT NULL DEFAULT ('{}'), "status" varchar NOT NULL DEFAULT ('closed'), "lastUsedAt" datetime, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_b8f17f1ce30f015f494188c50bb" UNIQUE ("sessionName"))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "scenario_run_steps" ("id" varchar PRIMARY KEY NOT NULL, "runId" varchar NOT NULL, "scenarioStepId" varchar, "title" text, "execCode" text, "timeoutSeconds" integer, "status" text NOT NULL DEFAULT ('waiting'), "order" integer NOT NULL DEFAULT (0), "description" text, "output" text, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "scenario_runs" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "environmentId" varchar, "sessionId" varchar, "scenarioVersion" integer, "status" text NOT NULL DEFAULT ('pending'), "saveSession" boolean NOT NULL DEFAULT (0), "traceMode" text, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "scenario_run_files" ("id" varchar PRIMARY KEY NOT NULL, "runId" varchar NOT NULL, "fileId" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "scenario_run_logs" ("id" varchar PRIMARY KEY NOT NULL, "runId" varchar NOT NULL, "stepRunId" varchar, "level" text NOT NULL DEFAULT ('log'), "message" text NOT NULL, "seq" integer NOT NULL DEFAULT (0), "createdAt" datetime NOT NULL DEFAULT (datetime('now')))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "scenario_versions" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "version" integer NOT NULL, "name" varchar NOT NULL, "description" text, "environmentId" text, "timeoutSeconds" integer, "steps" text NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_e1a56dce2b7e0379bb5bbdf8c1f" UNIQUE ("scenarioId", "version"))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "snippets" ("id" varchar PRIMARY KEY NOT NULL, "alias" varchar, "title" varchar NOT NULL DEFAULT (''), "description" text, "code" text NOT NULL, "version" integer NOT NULL DEFAULT (1), "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_bd062c68591ae917b720eb3427c" UNIQUE ("alias"))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "snippet_versions" ("id" varchar PRIMARY KEY NOT NULL, "snippetId" varchar NOT NULL, "version" integer NOT NULL, "alias" varchar NOT NULL, "title" varchar NOT NULL DEFAULT (''), "description" text, "code" text NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_2487f4aabeb20c4192ee30e09a2" UNIQUE ("snippetId", "version"))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "temporary_scenario_credentials" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "credentialId" varchar NOT NULL, "alias" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_02756536236c408dbb58b3fc964" UNIQUE ("scenarioId", "alias"), CONSTRAINT "FK_37f19dadd8e524b22a807ece1a9" FOREIGN KEY ("scenarioId") REFERENCES "scenarios" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_96676b988693d5ca2923782530c" FOREIGN KEY ("credentialId") REFERENCES "credentials" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`INSERT INTO "temporary_scenario_credentials"("id", "scenarioId", "credentialId", "alias", "createdAt") SELECT "id", "scenarioId", "credentialId", "alias", "createdAt" FROM "scenario_credentials"`,
|
||||
);
|
||||
await queryRunner.query(`DROP TABLE "scenario_credentials"`);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "temporary_scenario_credentials" RENAME TO "scenario_credentials"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "temporary_scenario_steps" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "order" integer NOT NULL DEFAULT (0), "title" text, "execCode" text, "timeoutSeconds" integer, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "FK_4bab3637cc3bf37677bb01c7446" FOREIGN KEY ("scenarioId") REFERENCES "scenarios" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`INSERT INTO "temporary_scenario_steps"("id", "scenarioId", "order", "title", "execCode", "timeoutSeconds", "createdAt", "updatedAt") SELECT "id", "scenarioId", "order", "title", "execCode", "timeoutSeconds", "createdAt", "updatedAt" FROM "scenario_steps"`,
|
||||
);
|
||||
await queryRunner.query(`DROP TABLE "scenario_steps"`);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "temporary_scenario_steps" RENAME TO "scenario_steps"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "temporary_scenarios" ("id" varchar PRIMARY KEY NOT NULL, "name" varchar NOT NULL, "description" text, "environmentId" varchar, "timeoutSeconds" integer, "version" integer NOT NULL DEFAULT (1), "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "FK_ffdcf9186d95114f0dab068e20a" FOREIGN KEY ("environmentId") REFERENCES "environments" ("id") ON DELETE SET NULL ON UPDATE NO ACTION)`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`INSERT INTO "temporary_scenarios"("id", "name", "description", "environmentId", "timeoutSeconds", "version", "createdAt", "updatedAt") SELECT "id", "name", "description", "environmentId", "timeoutSeconds", "version", "createdAt", "updatedAt" FROM "scenarios"`,
|
||||
);
|
||||
await queryRunner.query(`DROP TABLE "scenarios"`);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "temporary_scenarios" RENAME TO "scenarios"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "temporary_scenario_files" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "fileId" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "FK_c41719984235f8984d1c6b92ef5" FOREIGN KEY ("scenarioId") REFERENCES "scenarios" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_67013a49e7948fb530a7d956e2b" FOREIGN KEY ("fileId") REFERENCES "files" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`INSERT INTO "temporary_scenario_files"("id", "scenarioId", "fileId", "createdAt") SELECT "id", "scenarioId", "fileId", "createdAt" FROM "scenario_files"`,
|
||||
);
|
||||
await queryRunner.query(`DROP TABLE "scenario_files"`);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "temporary_scenario_files" RENAME TO "scenario_files"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "temporary_scenario_run_steps" ("id" varchar PRIMARY KEY NOT NULL, "runId" varchar NOT NULL, "scenarioStepId" varchar, "title" text, "execCode" text, "timeoutSeconds" integer, "status" text NOT NULL DEFAULT ('waiting'), "order" integer NOT NULL DEFAULT (0), "description" text, "output" text, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "FK_213c27405af3433011121babb58" FOREIGN KEY ("runId") REFERENCES "scenario_runs" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_e84cd4c7725bdc5dfd09d18d92f" FOREIGN KEY ("scenarioStepId") REFERENCES "scenario_steps" ("id") ON DELETE SET NULL ON UPDATE NO ACTION)`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`INSERT INTO "temporary_scenario_run_steps"("id", "runId", "scenarioStepId", "title", "execCode", "timeoutSeconds", "status", "order", "description", "output", "createdAt", "updatedAt") SELECT "id", "runId", "scenarioStepId", "title", "execCode", "timeoutSeconds", "status", "order", "description", "output", "createdAt", "updatedAt" FROM "scenario_run_steps"`,
|
||||
);
|
||||
await queryRunner.query(`DROP TABLE "scenario_run_steps"`);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "temporary_scenario_run_steps" RENAME TO "scenario_run_steps"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "temporary_scenario_runs" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "environmentId" varchar, "sessionId" varchar, "scenarioVersion" integer, "status" text NOT NULL DEFAULT ('pending'), "saveSession" boolean NOT NULL DEFAULT (0), "traceMode" text, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "FK_1d2c2595dfff133de062f9c692a" FOREIGN KEY ("scenarioId") REFERENCES "scenarios" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_5076ffbd1b4fa71f5de011eba1f" FOREIGN KEY ("environmentId") REFERENCES "environments" ("id") ON DELETE SET NULL ON UPDATE NO ACTION, CONSTRAINT "FK_068b01cdfa49095433f245d6708" FOREIGN KEY ("sessionId") REFERENCES "sessions" ("id") ON DELETE SET NULL ON UPDATE NO ACTION)`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`INSERT INTO "temporary_scenario_runs"("id", "scenarioId", "environmentId", "sessionId", "scenarioVersion", "status", "saveSession", "traceMode", "createdAt", "updatedAt") SELECT "id", "scenarioId", "environmentId", "sessionId", "scenarioVersion", "status", "saveSession", "traceMode", "createdAt", "updatedAt" FROM "scenario_runs"`,
|
||||
);
|
||||
await queryRunner.query(`DROP TABLE "scenario_runs"`);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "temporary_scenario_runs" RENAME TO "scenario_runs"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "temporary_scenario_run_files" ("id" varchar PRIMARY KEY NOT NULL, "runId" varchar NOT NULL, "fileId" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "FK_5fc35a4bfc42d7566a0f7706902" FOREIGN KEY ("runId") REFERENCES "scenario_runs" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_637619025bdb0b6a89fae6e178d" FOREIGN KEY ("fileId") REFERENCES "files" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`INSERT INTO "temporary_scenario_run_files"("id", "runId", "fileId", "createdAt") SELECT "id", "runId", "fileId", "createdAt" FROM "scenario_run_files"`,
|
||||
);
|
||||
await queryRunner.query(`DROP TABLE "scenario_run_files"`);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "temporary_scenario_run_files" RENAME TO "scenario_run_files"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "temporary_scenario_run_logs" ("id" varchar PRIMARY KEY NOT NULL, "runId" varchar NOT NULL, "stepRunId" varchar, "level" text NOT NULL DEFAULT ('log'), "message" text NOT NULL, "seq" integer NOT NULL DEFAULT (0), "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "FK_dde48478a42823e583a360dd468" FOREIGN KEY ("runId") REFERENCES "scenario_runs" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_62d82b9fce3a6a2947ed1136c67" FOREIGN KEY ("stepRunId") REFERENCES "scenario_run_steps" ("id") ON DELETE SET NULL ON UPDATE NO ACTION)`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`INSERT INTO "temporary_scenario_run_logs"("id", "runId", "stepRunId", "level", "message", "seq", "createdAt") SELECT "id", "runId", "stepRunId", "level", "message", "seq", "createdAt" FROM "scenario_run_logs"`,
|
||||
);
|
||||
await queryRunner.query(`DROP TABLE "scenario_run_logs"`);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "temporary_scenario_run_logs" RENAME TO "scenario_run_logs"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "temporary_scenario_versions" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "version" integer NOT NULL, "name" varchar NOT NULL, "description" text, "environmentId" text, "timeoutSeconds" integer, "steps" text NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_e1a56dce2b7e0379bb5bbdf8c1f" UNIQUE ("scenarioId", "version"), CONSTRAINT "FK_f5ccfb45c7847cfab74cf4f17b6" FOREIGN KEY ("scenarioId") REFERENCES "scenarios" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`INSERT INTO "temporary_scenario_versions"("id", "scenarioId", "version", "name", "description", "environmentId", "timeoutSeconds", "steps", "createdAt") SELECT "id", "scenarioId", "version", "name", "description", "environmentId", "timeoutSeconds", "steps", "createdAt" FROM "scenario_versions"`,
|
||||
);
|
||||
await queryRunner.query(`DROP TABLE "scenario_versions"`);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "temporary_scenario_versions" RENAME TO "scenario_versions"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "temporary_snippet_versions" ("id" varchar PRIMARY KEY NOT NULL, "snippetId" varchar NOT NULL, "version" integer NOT NULL, "alias" varchar NOT NULL, "title" varchar NOT NULL DEFAULT (''), "description" text, "code" text NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_2487f4aabeb20c4192ee30e09a2" UNIQUE ("snippetId", "version"), CONSTRAINT "FK_0d3a004957c9553619be11635fd" FOREIGN KEY ("snippetId") REFERENCES "snippets" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`INSERT INTO "temporary_snippet_versions"("id", "snippetId", "version", "alias", "title", "description", "code", "createdAt") SELECT "id", "snippetId", "version", "alias", "title", "description", "code", "createdAt" FROM "snippet_versions"`,
|
||||
);
|
||||
await queryRunner.query(`DROP TABLE "snippet_versions"`);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "temporary_snippet_versions" RENAME TO "snippet_versions"`,
|
||||
);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "snippet_versions" RENAME TO "temporary_snippet_versions"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "snippet_versions" ("id" varchar PRIMARY KEY NOT NULL, "snippetId" varchar NOT NULL, "version" integer NOT NULL, "alias" varchar NOT NULL, "title" varchar NOT NULL DEFAULT (''), "description" text, "code" text NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_2487f4aabeb20c4192ee30e09a2" UNIQUE ("snippetId", "version"))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`INSERT INTO "snippet_versions"("id", "snippetId", "version", "alias", "title", "description", "code", "createdAt") SELECT "id", "snippetId", "version", "alias", "title", "description", "code", "createdAt" FROM "temporary_snippet_versions"`,
|
||||
);
|
||||
await queryRunner.query(`DROP TABLE "temporary_snippet_versions"`);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "scenario_versions" RENAME TO "temporary_scenario_versions"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "scenario_versions" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "version" integer NOT NULL, "name" varchar NOT NULL, "description" text, "environmentId" text, "timeoutSeconds" integer, "steps" text NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_e1a56dce2b7e0379bb5bbdf8c1f" UNIQUE ("scenarioId", "version"))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`INSERT INTO "scenario_versions"("id", "scenarioId", "version", "name", "description", "environmentId", "timeoutSeconds", "steps", "createdAt") SELECT "id", "scenarioId", "version", "name", "description", "environmentId", "timeoutSeconds", "steps", "createdAt" FROM "temporary_scenario_versions"`,
|
||||
);
|
||||
await queryRunner.query(`DROP TABLE "temporary_scenario_versions"`);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "scenario_run_logs" RENAME TO "temporary_scenario_run_logs"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "scenario_run_logs" ("id" varchar PRIMARY KEY NOT NULL, "runId" varchar NOT NULL, "stepRunId" varchar, "level" text NOT NULL DEFAULT ('log'), "message" text NOT NULL, "seq" integer NOT NULL DEFAULT (0), "createdAt" datetime NOT NULL DEFAULT (datetime('now')))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`INSERT INTO "scenario_run_logs"("id", "runId", "stepRunId", "level", "message", "seq", "createdAt") SELECT "id", "runId", "stepRunId", "level", "message", "seq", "createdAt" FROM "temporary_scenario_run_logs"`,
|
||||
);
|
||||
await queryRunner.query(`DROP TABLE "temporary_scenario_run_logs"`);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "scenario_run_files" RENAME TO "temporary_scenario_run_files"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "scenario_run_files" ("id" varchar PRIMARY KEY NOT NULL, "runId" varchar NOT NULL, "fileId" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`INSERT INTO "scenario_run_files"("id", "runId", "fileId", "createdAt") SELECT "id", "runId", "fileId", "createdAt" FROM "temporary_scenario_run_files"`,
|
||||
);
|
||||
await queryRunner.query(`DROP TABLE "temporary_scenario_run_files"`);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "scenario_runs" RENAME TO "temporary_scenario_runs"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "scenario_runs" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "environmentId" varchar, "sessionId" varchar, "scenarioVersion" integer, "status" text NOT NULL DEFAULT ('pending'), "saveSession" boolean NOT NULL DEFAULT (0), "traceMode" text, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`INSERT INTO "scenario_runs"("id", "scenarioId", "environmentId", "sessionId", "scenarioVersion", "status", "saveSession", "traceMode", "createdAt", "updatedAt") SELECT "id", "scenarioId", "environmentId", "sessionId", "scenarioVersion", "status", "saveSession", "traceMode", "createdAt", "updatedAt" FROM "temporary_scenario_runs"`,
|
||||
);
|
||||
await queryRunner.query(`DROP TABLE "temporary_scenario_runs"`);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "scenario_run_steps" RENAME TO "temporary_scenario_run_steps"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "scenario_run_steps" ("id" varchar PRIMARY KEY NOT NULL, "runId" varchar NOT NULL, "scenarioStepId" varchar, "title" text, "execCode" text, "timeoutSeconds" integer, "status" text NOT NULL DEFAULT ('waiting'), "order" integer NOT NULL DEFAULT (0), "description" text, "output" text, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`INSERT INTO "scenario_run_steps"("id", "runId", "scenarioStepId", "title", "execCode", "timeoutSeconds", "status", "order", "description", "output", "createdAt", "updatedAt") SELECT "id", "runId", "scenarioStepId", "title", "execCode", "timeoutSeconds", "status", "order", "description", "output", "createdAt", "updatedAt" FROM "temporary_scenario_run_steps"`,
|
||||
);
|
||||
await queryRunner.query(`DROP TABLE "temporary_scenario_run_steps"`);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "scenario_files" RENAME TO "temporary_scenario_files"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "scenario_files" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "fileId" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`INSERT INTO "scenario_files"("id", "scenarioId", "fileId", "createdAt") SELECT "id", "scenarioId", "fileId", "createdAt" FROM "temporary_scenario_files"`,
|
||||
);
|
||||
await queryRunner.query(`DROP TABLE "temporary_scenario_files"`);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "scenarios" RENAME TO "temporary_scenarios"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "scenarios" ("id" varchar PRIMARY KEY NOT NULL, "name" varchar NOT NULL, "description" text, "environmentId" varchar, "timeoutSeconds" integer, "version" integer NOT NULL DEFAULT (1), "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`INSERT INTO "scenarios"("id", "name", "description", "environmentId", "timeoutSeconds", "version", "createdAt", "updatedAt") SELECT "id", "name", "description", "environmentId", "timeoutSeconds", "version", "createdAt", "updatedAt" FROM "temporary_scenarios"`,
|
||||
);
|
||||
await queryRunner.query(`DROP TABLE "temporary_scenarios"`);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "scenario_steps" RENAME TO "temporary_scenario_steps"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "scenario_steps" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "order" integer NOT NULL DEFAULT (0), "title" text, "execCode" text, "timeoutSeconds" integer, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`INSERT INTO "scenario_steps"("id", "scenarioId", "order", "title", "execCode", "timeoutSeconds", "createdAt", "updatedAt") SELECT "id", "scenarioId", "order", "title", "execCode", "timeoutSeconds", "createdAt", "updatedAt" FROM "temporary_scenario_steps"`,
|
||||
);
|
||||
await queryRunner.query(`DROP TABLE "temporary_scenario_steps"`);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "scenario_credentials" RENAME TO "temporary_scenario_credentials"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`CREATE TABLE "scenario_credentials" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "credentialId" varchar NOT NULL, "alias" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_02756536236c408dbb58b3fc964" UNIQUE ("scenarioId", "alias"))`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`INSERT INTO "scenario_credentials"("id", "scenarioId", "credentialId", "alias", "createdAt") SELECT "id", "scenarioId", "credentialId", "alias", "createdAt" FROM "temporary_scenario_credentials"`,
|
||||
);
|
||||
await queryRunner.query(`DROP TABLE "temporary_scenario_credentials"`);
|
||||
await queryRunner.query(`DROP TABLE "snippet_versions"`);
|
||||
await queryRunner.query(`DROP TABLE "snippets"`);
|
||||
await queryRunner.query(`DROP TABLE "scenario_versions"`);
|
||||
await queryRunner.query(`DROP TABLE "scenario_run_logs"`);
|
||||
await queryRunner.query(`DROP TABLE "scenario_run_files"`);
|
||||
await queryRunner.query(`DROP TABLE "scenario_runs"`);
|
||||
await queryRunner.query(`DROP TABLE "scenario_run_steps"`);
|
||||
await queryRunner.query(`DROP TABLE "sessions"`);
|
||||
await queryRunner.query(`DROP TABLE "scenario_files"`);
|
||||
await queryRunner.query(`DROP TABLE "scenarios"`);
|
||||
await queryRunner.query(`DROP TABLE "scenario_steps"`);
|
||||
await queryRunner.query(`DROP TABLE "scenario_credentials"`);
|
||||
await queryRunner.query(`DROP TABLE "files"`);
|
||||
await queryRunner.query(`DROP TABLE "environments"`);
|
||||
await queryRunner.query(`DROP TABLE "credentials"`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { Initial1790793447827 } from "./1790793447827-Initial";
|
||||
|
||||
/**
|
||||
* All migrations, oldest first. Listed explicitly (instead of a glob) so the
|
||||
* same list works from compiled JS, ts-jest and the TypeORM CLI.
|
||||
*/
|
||||
export const MIGRATIONS = [Initial1790793447827];
|
||||
+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