import { Client, Connection } from '@pgkit/client'; import * as migra from '@pgkit/migra'; import { AsyncLocalStorage } from 'async_hooks'; import { MigratorConfig, MigratorConstructorParams, Logger, Task, MigratorContext, RunnableMigration, Confirm, ListedMigration, PendingMigration, ExecutedMigration } from './types'; export declare const noopLogger: Logger; export declare const noopTask: (name: string, fn: () => Promise) => Promise<{ result: Awaited; }>; export declare class Migrator { configStorage: AsyncLocalStorage>; /** * The config set in the constructor of the class. Note: in almost all cases, you should use @see config instead, * since the client could be overriden. */ protected initialConfig: MigratorConfig; constructor(params: MigratorConstructorParams); get client(): Client; useConfig(config: Partial, fn: () => Promise): Promise; protected get config(): MigratorConfig; get logger(): Logger; get task(): Task; cli(): import("trpc-cli").TrpcCli; /** Gets a hexadecimal integer to pass to postgres's `select pg_advisory_lock()` function */ protected advisoryLockId(): number; protected get migrationTable(): { schema: string | null; table: string; }; protected migrationTableNameIdentifier(): { token: "identifier"; args: [names: readonly string[]]; }; protected applyMigration(params: { name: string; path: string; context: MigratorContext; }): Promise; protected resolver(params: { name: string; path: string; }): RunnableMigration; protected getOrCreateMigrationsTable(): Promise; protected get createMigrationsTableSQL(): import("@pgkit/client").AwaitableSQLQuery, unknown[]>; /** Wait this many milliseconds before logging a message warning that the advisory lock hasn't been acquired yet. */ get lockWarningMs(): number; get definitionsFile(): string; connect(fn: (connection: Connection) => Promise): Promise; waitForAdvisoryLock(): Promise<{ took: number; }>; releaseAdvisoryLock(): Promise; useAdvisoryLock(fn: () => Promise): Promise; protected content(name: string): string; /** * Determine if a given migration name should be considered repeatable. */ protected isRepeatable(name: string): RegExpMatchArray | null; protected logMigration({ name, context }: { name: string; context: MigratorContext; }): Promise; protected unlogMigration({ name, context }: { name: string; context: MigratorContext; }): Promise; /** * Applies pending migrations. */ up(input?: { to?: string; } | { step?: number; }): Promise; unlock(params: { confirm: Confirm; }): Promise; latest(params?: { skipCheck?: boolean; }): Promise; useContext(fn: (context: MigratorContext) => Promise): Promise; /** * Calculates the SQL required to forcible go to a specific migration, and applies it if the `confirm` function returns true. * This can be used to go "down" to a specific migration (or up, but in most cases the `up` command would be more appropriate). * * Remember: "goto considered harmful" - the generated SQL may be destructive, and should be reviewed. * This should usually not be used in production, unless it's required to introduce this tool to an existing system. * In production, you would usually prefer to create a regular migration which does whichever `drop x` or `alter y` commands are necessary. */ goto(params: { name: string; confirm: Confirm; purgeDisk?: boolean; }): Promise; private getMigrationsTableInspected; private getMigrationsTableFixStatements; /** * Marks all migrations up to and including the specified migration as executed in the database. * Useful when introducing this migrator to an existing database, and you know that the database matches the state up to a specific migration. */ baseline(params: { to: string; purgeDisk?: boolean; confirm: Confirm; }): Promise; renderStatement(q: { sql: string; values: unknown[]; }): string; renderConfirmable(queries: { sql: string; values: unknown[]; }[], sep?: string): string; /** * Gets a string prefix for migrations. By default, this is the current date and time in ISO format, with `-`/`:` characters replaced by `.`. * Override this method to change the prefix format. Take care that migrations are generated in lexicographic order, so the prefix should be sortable. * */ filePrefix(): string; /** * Creates a new migration file. By default, uses the definitions file to generate the content. * You can override this behavior by passing in a `content` parameter. * Pass in empty string or something like `-- placeholder` if you're not sure what to write and don't want to use the definitions file. */ create(params?: { name?: string; content?: string; }): Promise<{ name: string; path: string; content: string; }>; get templates(): Record; /** * Rebase migrations since the specified `from` migration. This will: * * 1. baseline migrations to `from`, and delete all migrations from disk after `from` * 2. create a new migration with the diff between the state at `from` and the current database state * 3. baseline the database to the new migration * * Other than baselining the migrations, it does *not* update your database state. * * Use this if you have tinkered with the database manually, with a query editor or another external tool, and now want to port those changes into a migration. * * Often, you may want to rebase from the last production migration, since migrations that have run in production should * usually be considered permanent. Any destructive commands in production should be in an explicit new migration. */ rebase(params: { from: string; confirm: Confirm; name?: string; }): Promise; diffToDefinitions(): Promise; /** * Uses the definitions file to update the database schema. */ updateDbToMatchDefinitions(params: { confirm: Confirm; }): Promise; /** * Uses the current state of the database to overwrite the definitions file. */ updateDefinitionsToMatchDb(params: { confirm: Confirm; }): Promise<{ path: string; changed: boolean; updated: string | null; content: string; }>; /** * Calculates the SQL required to alter the DB to match what it *should* be for the latest executed migration. * Also recreates the migrations records if necessary (in case migration files have been altered or retroactively added to the filesystem). */ getRepairDiff(): Promise[]>; repair(params: { confirm: Confirm; }): Promise<{ drifted: boolean; updated: boolean; }>; check(): Promise; list(): Promise; pending(): Promise; executed(): Promise; wipeDiff(): Promise; wipe(params: { confirm: Confirm; }): Promise; /** * Uses `migra` to generate a diff between the current database and the state of a database at the specified migration. * This is used by @see goto to go "down" to a specific migration. */ getDiffTo(params: { name: string; }): Promise; /** * Uses `migra` to generate a diff between the state of a database at the specified migration, and the current state of the database. */ getDiffFrom(params: { name: string; }): Promise; /** * Creates a temporary database and runs the callback with a client connected to it. * After the callback resolves or rejects, the temporary database is dropped forcefully. */ useShadowClient(cb: (client: Client) => Promise): Promise; /** * Creates a temporary database, and runs the provided callback with the migrator instance connected to the temporary database. * Within the scope of the callback, the migrator instance will be configured to use the temporary database. This is useful for * calculating diffs between the current database and the state of the database at a specific migration. * The callback will be passed an object with the original client as `parent.client`. * After the callback resolves or rejects, the temporary database is dropped forcefully. */ useShadowClientConfig(cb: (params: { parent: { client: Client; }; }) => Promise): Promise; /** * The base implementation just calls `migra.run`. You can override this method to manually reorder statements. * This is necessary sometimes because migra doesn't alwyas generate statements in the right order. https://github.com/djrobstep/migra/issues/196 * * @example * import {Migrator as Base} from '@pgkit/migrator' * * export class Migrator extends Base { * async wrapMigra() { * const migration = await super.wrapMigra() * const firstCreateTahleStatement = migration.statements.findIndex(s => s.match(/create table/)) * migration.statements.sortBy((s, i) => { * return s.match(/create type/) ? Math.min(i, firstCreateTahleStatement - 1) : i * }) * return migration * } * } */ wrapMigra(...args: Parameters): Promise<{ result: migra.Migration; sql: string; }>; } //# sourceMappingURL=migrator.d.ts.map