import postgres from "postgres"; import { type MigrationDeploymentMode, type MigrationPhase, type MigrationPhases } from "./migration-phases.js"; export { migrationDeploymentMode } from "./migration-phases.js"; export type { MigrationDeploymentMode } from "./migration-phases.js"; import { type InterruptedTurnRetirementManifest } from "./interrupted-turn-retirement-migration.js"; export interface ConcurrentIndexMigration { indexName: string; /** Historical directive metadata; concurrent builds must wait for old snapshots. */ lockTimeout: string; statement: string; } export type MigrationRuntimeOptions = { /** Read migration files from this directory instead of the shipped ones. */ migrationsDirectory?: string; /** Cumulative execution: pre; pre + maintenance; or everything (default). */ phase?: MigrationPhase; /** Called after successful execution and policy reconciliation, under the lock. */ onPhaseComplete?: (result: MigrationPhaseResult) => void; /** Explicit historical retirement of interrupted turns, never inferred from * Pause or row age. The exact version/sequence-fenced manifest runs before * pending migrations and needs an existing Opengeni database. */ interruptedTurnRetirement?: InterruptedTurnRetirementManifest; maxNestedAgentDepth?: number; /** * For managed Postgres where an administrator preinstalls pgvector but the * migration owner cannot execute CREATE EXTENSION, verify the public vector * type and omit only the baseline's exact vector installation statement. The * default executes the shipped baseline verbatim. */ preinstalledVector?: boolean; /** * Exact database login roles that may run an Opengeni API or worker against * this target. Maintenance cutovers use this list to reject a live mixed- * version fleet, and rolling ACL migrations use it to preserve old-binary * readiness until later role provisioning converges. Dedicated-schema and * custom-role deployments must supply it. */ applicationDatabaseRoles?: string[]; }; type DeploymentDepthPolicy = { maxNestedAgentDepth: number; source: "deployment" | "default"; }; /** * Most migration files intentionally execute as one implicit transaction. * PostgreSQL forbids CREATE INDEX CONCURRENTLY there, so a migration may opt * into one narrowly validated transactionless statement with: * * -- opengeni:concurrent-index lock-timeout=5s * CREATE [UNIQUE] INDEX CONCURRENTLY IF NOT EXISTS ...; * * The directive is deliberately not a generic "no transaction" escape hatch: * only one idempotent concurrent-index statement is accepted, and an invalid * artifact left by a failed concurrent build * is removed before retry. This keeps additive large-table indexes online * without making arbitrary partially-applied migration scripts possible. * The historical lock-timeout directive is parsed without rewriting shipped * files, but is no longer applied: PostgreSQL also uses lock_timeout for its * safe wait for old snapshots, including readers of unrelated tables when the * index is partial or has expressions. Ordinary DDL keeps its short timeout. */ export declare function parseConcurrentIndexMigration(file: string, sqlText: string): ConcurrentIndexMigration | null; export declare function parseBatchedBackfillMigration(file: string, sqlText: string): { batchSize: number; lockTimeout: string; statementTimeout: string; statement: string; } | null; export declare function executeMigrationFile(sql: postgres.Sql, file: string, sqlText: string, options?: Pick & { /** Record `file` in schema_migrations atomically with an ordinary body. */ recordApplied?: boolean; }): Promise; /** An explicit option wins; an option the caller left out falls back to the * deployment environment, as the command-line runner passes only its phase. */ export declare function preinstalledVectorPolicy(options: MigrationRuntimeOptions | undefined): boolean; export declare function deploymentDepthPolicy(options: MigrationRuntimeOptions | undefined): DeploymentDepthPolicy; export declare function migrationApplicationRoles(schema: string | undefined, options: MigrationRuntimeOptions | undefined): string[]; /** * Apply the Opengeni SQL migration chain. * * STANDALONE (default): `migrate()` / `migrate(databaseUrl)` runs the whole * chain with NO search_path manipulation, so every unqualified * table/index/policy lands in the server default schema (`public`). * * EMBEDDED SCHEMA MODE: pass a `schema` (or set * `OPENGENI_DB_SCHEMA`). The migrate session then `CREATE SCHEMA IF NOT EXISTS` * for both `` and `opengeni_private`, and sets * `search_path = "", "opengeni_private", "public"`, so EVERY unqualified * DDL statement lands in the dedicated schema with NO per-statement SQL rewrite * (the schema-isolation contract). Migration SQL that must name the schema * (policy-existence guards, dynamic SQL) uses `current_schema()`, and the * baseline's routine bodies use a `{{schema}}` placeholder the runner fills in. * `public` stays LAST on the path so extensions an administrator installed * there still resolve. The `opengeni_private.*` helpers are always called with * an absolute prefix. * Dedicated-schema callers must also pass the exact API/worker database login * identities in `applicationDatabaseRoles`; maintenance cutovers cannot infer * host-managed or scoped runtime roles from the admin connection. * * `OPENGENI_DB_SCHEMA` defaults UNSET → `public` → standalone, so the default * binding never regresses. */ export declare function migrate(databaseUrl?: string, schema?: string | undefined, runtimeOptions?: MigrationRuntimeOptions): Promise; /** * SDK entry point (Step I): run the migration chain over a host-supplied admin * connection string against an explicit target schema. This is the embedded * topology's named entry — a host calls `runMigrations(adminConnection, * targetSchema, { applicationDatabaseRoles })` from its own provisioning code * instead of relying on env vars. * `targetSchema` undefined → `public` → standalone behavior. Thin wrapper over * `migrate` so there is one migration engine. */ export declare function runMigrations(adminConnection: string, targetSchema?: string, runtimeOptions?: MigrationRuntimeOptions): Promise; export interface MigrationPhaseResult { schemaVersion: "opengeni/migration-phase/v1"; phase: MigrationPhase; /** Names applied by this invocation, not the cumulative database ledger. */ applied: string[]; remaining: string[]; } /** Malformed or unsupported files are never deferred; execution still reports * their original parser error at the original point in the default path. */ export declare function isPostReleaseEligible(file: string, sqlText: string): boolean; export declare function isPreReleaseEligible(file: string, sqlText: string): boolean; export interface MigrationPlan { phaseProtocol: 1; phases: MigrationPhases; /** Shipped migrations this database has not applied yet, in apply order. */ pending: { name: string; deploymentMode: MigrationDeploymentMode; }[]; /** * True when any pending migration is not `rolling`. Rolling migrations run * while the previous release keeps serving; anything else needs every * API/control/turn process drained before the migration job runs. */ requiresDrain: boolean; } /** * Read-only: compare the shipped migration chain with the database's applied * list. Takes no lock and creates nothing, so it is safe to run against a live * deployment before deciding whether an upgrade needs a drain. */ export declare function planMigrations(databaseUrl?: string, schema?: string | undefined, migrationsDir?: string): Promise; /** Command-line entry. Bundled release images call this explicitly; see * scripts/assemble-server-runtime.ts. */ export declare function runMigrateCli(): Promise;