feat: implement metadata catalog database management
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { CamelCasePlugin, Kysely, PostgresDialect } from "kysely";
|
||||
import { Migrator, type MigrationProvider } from "kysely/migration";
|
||||
import { Pool } from "pg";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import type { CatalogDatabase } from "./repository.js";
|
||||
import * as initialMigration from "./migrations/001_workspace_databases.js";
|
||||
import * as catalogTablesMigration from "./migrations/002_catalog_tables.js";
|
||||
import * as catalogSchemaSyncMigration from "./migrations/003_catalog_schema_sync.js";
|
||||
import * as catalogRuntimeSequencePrivilegesMigration from "./migrations/004_catalog_runtime_sequence_privileges.js";
|
||||
|
||||
const connectionString = process.env.THT_CATALOG_MIGRATOR_DATABASE_URL;
|
||||
const host = process.env.THT_CATALOG_DB_HOST;
|
||||
const database = process.env.THT_CATALOG_DB_NAME;
|
||||
const user = process.env.THT_CATALOG_MIGRATOR_USER;
|
||||
const passwordFile = process.env.THT_CATALOG_MIGRATOR_PASSWORD_FILE;
|
||||
if (!connectionString && (!host || !database || !user || !passwordFile)) {
|
||||
throw new Error("catalog migrator database configuration is required");
|
||||
}
|
||||
const pool = new Pool(connectionString ? { connectionString, max: 1 } : {
|
||||
host,
|
||||
port: Number(process.env.THT_CATALOG_DB_PORT ?? 5432),
|
||||
database,
|
||||
user,
|
||||
password: async () => (await readFile(passwordFile!, "utf8")).trim(),
|
||||
max: 1,
|
||||
});
|
||||
|
||||
const db = new Kysely<CatalogDatabase>({
|
||||
dialect: new PostgresDialect({ pool }),
|
||||
plugins: [new CamelCasePlugin()],
|
||||
});
|
||||
const provider: MigrationProvider = {
|
||||
async getMigrations() {
|
||||
return {
|
||||
"001_workspace_databases": initialMigration,
|
||||
"002_catalog_tables": catalogTablesMigration,
|
||||
"003_catalog_schema_sync": catalogSchemaSyncMigration,
|
||||
"004_catalog_runtime_sequence_privileges": catalogRuntimeSequencePrivilegesMigration,
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
const result = await new Migrator({ db, provider }).migrateToLatest();
|
||||
for (const item of result.results ?? []) {
|
||||
process.stdout.write(`${item.migrationName}: ${item.status}\n`);
|
||||
}
|
||||
if (result.error) throw result.error;
|
||||
} finally {
|
||||
await db.destroy();
|
||||
}
|
||||
Reference in New Issue
Block a user