import type { Knex } from 'knex'; import type { Database } from '..'; import type { AttributeRenamesOperation, RenameColumnOperation, RenameTableOperation, UpdateRowsOperation } from '../migrations/file-builder'; /** * Runtime helpers used by generated rename migrations (see * `migrations/file-builder.ts`). Keeping the guards, logging and dialect * specifics here instead of in the generated file means a fix in the database * package applies to migrations users already generated. * * Every helper: * - runs on the transaction it is given (never on `db.connection`, which may * be exhausted by that very transaction on single-connection pools); * - checks the source exists and the target does not before doing anything, * so it is a safe no-op on a fresh database (schema sync creates the tables * afterwards) or on one that already moved on. For `updateRows` the source * is the rows matching `where`: with none, nothing is deleted or updated, so * re-running a partly applied migration (MySQL commits DDL implicitly) * cannot mistake the rows it already renamed for orphans; * - logs when it skips: `info` when the source is missing (expected on a fresh * database), `warn` when the target already exists (the environment drifted * and the rename could not be applied — schema sync will then drop the old * artifact, so this is worth a look); * - renames the indexes/constraints whose names embed the renamed identifier * where the dialect supports it (Postgres), so schema sync sees no diff * afterwards. On other dialects it drops them instead and schema sync * re-creates them under the new name. Leaving them under the old name is not * an option: sync only drops indexes it tracked on a table of the same name, * so re-adding the old table/column later would collide with them. */ export type RenameSkipReason = 'source-missing' | 'target-exists'; /** * Attribute renames of one save, composed per model: `uid -> { origin: final }`. */ export interface AttributeRenames { renames: Record>; comment?: string; } /** * Updates a store keyed by attribute name (e.g. admin field permissions) for * the given renames. Runs inside the migration transaction `trx`. */ export type AttributeRenameHandler = (trx: Knex, renames: AttributeRenames['renames']) => Promise; interface RenameHelpersDeps { db: Database; } export declare const createRenameHelpers: ({ db }: RenameHelpersDeps) => { /** * Renames a column, then the index / unique / foreign-key constraints named * after it (`__index|unique|fk`) where supported. */ renameColumn(trx: Knex, op: RenameColumnOperation): Promise; /** * Renames a table (typically a relation join table), then the indexes and * constraints named after it where supported. */ renameTable(trx: Knex, op: RenameTableOperation): Promise; /** * Rewrites a stored value (e.g. the `field` or `component_type` column of a * component link table). Guarded on the table and `guardColumn` (defaults * to the first updated column) existing, and on rows matching `where`. */ updateRows(trx: Knex, op: UpdateRowsOperation): Promise; /** * Registers a handler for `applyAttributeRenames`. Plugins that keep data * keyed by attribute name register one during their register phase, before * user migrations run. Returns a function that unregisters it. */ registerAttributeRenameHandler(handler: AttributeRenameHandler): () => void; /** * Dispatches one save's attribute renames to every registered handler, in * registration order, inside `trx`. The database package itself stores * nothing keyed by attribute name, so with no handler this is a no-op. */ applyAttributeRenames(trx: Knex, op: AttributeRenamesOperation): Promise; }; export type RenameHelpers = ReturnType; export {}; //# sourceMappingURL=rename-helpers.d.ts.map