import { D as DrizzleAdapter } from './adapter-D1vI8njz.js'; import { M as MigrationRecord, a as Migration, b as MigrationStatus, c as MigrationOptions } from './migration-XV8CXMZT.js'; export { d as MigrationResult } from './migration-XV8CXMZT.js'; import 'drizzle-orm'; import './core-CVO7WYDj.js'; import './schema-BIQ0YQZ_.js'; import './error-um1d_3Uo.js'; /** * Database migration utilities. * * @remarks * Provides core utilities for migration management including checksum calculation, * sorting, filtering, and validation. Also includes optional helpers for adapters * to use for common migration operations. * * @packageDocumentation */ /** * Calculate SHA-256 checksum for a migration. * * @remarks * Creates a checksum of the entire migration object including id, name, timestamp, * and the string representation of up/down functions. This allows detection of * any changes to the migration after it has been applied. * * @param migration - Migration to calculate checksum for * @returns Hexadecimal SHA-256 hash * * @example * ```typescript * const migration: Migration = { * id: "20250104_001_create_users", * name: "Create users table", * timestamp: 1704326400000, * up: "CREATE TABLE users (id UUID PRIMARY KEY);", * }; * * const checksum = calculateChecksum(migration); * // Returns: "a3f5b8c9d2e1f0..." * ``` * * @public */ declare function calculateChecksum(migration: Migration): string; /** * Sort migrations by timestamp in ascending order. * * @remarks * Provides deterministic ordering of migrations. If timestamps are equal, * falls back to sorting by migration ID alphabetically. * * @param migrations - Array of migrations to sort * @returns New sorted array (original array is not modified) * * @example * ```typescript * const migrations = [ * { id: "003", timestamp: 1704412800000, ... }, * { id: "001", timestamp: 1704326400000, ... }, * { id: "002", timestamp: 1704326400000, ... }, * ]; * * const sorted = sortMigrations(migrations); * // Returns: [001, 002, 003] (001 and 002 same timestamp, sorted by id) * ``` * * @public */ declare function sortMigrations(migrations: Migration[]): Migration[]; /** * Filter migrations to get only pending (unapplied) migrations. * * @remarks * Returns migrations that have not been applied to the database yet. * A migration is considered pending if its ID is not present in the * applied migration records. * * @param migrations - All available migrations * @param applied - Records of applied migrations from database * @returns Array of pending migrations * * @example * ```typescript * const allMigrations = [ * { id: "001_create_users", ... }, * { id: "002_create_posts", ... }, * { id: "003_create_comments", ... }, * ]; * * const appliedRecords = [ * { id: "001_create_users", appliedAt: new Date(), ... }, * ]; * * const pending = filterPending(allMigrations, appliedRecords); * // Returns: [002_create_posts, 003_create_comments] * ``` * * @public */ declare function filterPending(migrations: Migration[], applied: MigrationRecord[]): Migration[]; /** * Filter migration records to get only those that match given migrations. * * @remarks * Returns applied migration records that correspond to the given migrations. * Useful for getting the subset of applied records relevant to a specific * set of migrations. * * @param migrations - Migrations to match * @param applied - All applied migration records from database * @returns Array of matching applied records * * @example * ```typescript * const migrations = [ * { id: "001_create_users", ... }, * { id: "002_create_posts", ... }, * ]; * * const allApplied = [ * { id: "001_create_users", appliedAt: new Date(), ... }, * { id: "002_create_posts", appliedAt: new Date(), ... }, * { id: "003_create_comments", appliedAt: new Date(), ... }, * ]; * * const relevant = filterApplied(migrations, allApplied); * // Returns: [001_create_users, 002_create_posts] * ``` * * @public */ declare function filterApplied(migrations: Migration[], applied: MigrationRecord[]): MigrationRecord[]; /** * Validate that a migration's checksum matches its applied record. * * @remarks * Compares the calculated checksum of a migration against the stored checksum * in its migration record. Returns false if checksums don't match or if the * record has no checksum. * * @param migration - Migration to validate * @param record - Applied migration record with stored checksum * @returns true if checksums match, false otherwise * * @example * ```typescript * const migration: Migration = { id: "001", ... }; * const record: MigrationRecord = { * id: "001", * checksum: "a3f5b8c9...", * ... * }; * * const isValid = validateChecksum(migration, record); * // Returns: true if checksums match, false if modified * ``` * * @public */ declare function validateChecksum(migration: Migration, record: MigrationRecord): boolean; /** * Detect migrations that have been modified after being applied. * * @remarks * Compares checksums of migrations against their applied records to detect * any modifications. Returns records for migrations that have been changed * since they were applied to the database. * * This is critical for detecting potentially dangerous situations where a * migration that has already run has been modified. * * @param migrations - Current migrations * @param applied - Applied migration records with checksums * @returns Array of records for modified migrations * * @example * ```typescript * const migrations = [{ id: "001", up: "CREATE TABLE users_v2 ..." }]; * const applied = [{ id: "001", checksum: "original_hash", ... }]; * * const modified = detectModified(migrations, applied); * // Returns: [{ id: "001", ... }] if migration was changed * ``` * * @public */ declare function detectModified(migrations: Migration[], applied: MigrationRecord[]): MigrationRecord[]; /** * Get comprehensive migration status. * * @remarks * Analyzes migrations and applied records to produce a complete status report * including current migration, applied/pending counts, and full lists. * * @param migrations - All available migrations * @param applied - Applied migration records from database * @returns Complete migration status information * * @example * ```typescript * const status = getMigrationStatus(allMigrations, appliedRecords); * console.log(`Current: ${status.current}`); * console.log(`Applied: ${status.appliedCount}, Pending: ${status.pendingCount}`); * ``` * * @public */ declare function getMigrationStatus(migrations: Migration[], applied: MigrationRecord[]): MigrationStatus; /** * Validation result for migrations. * * @public */ interface MigrationValidationResult { /** Whether all validations passed */ valid: boolean; /** List of validation errors */ errors: string[]; /** List of validation warnings */ warnings: string[]; /** Migrations that have been modified */ modified: MigrationRecord[]; /** Duplicate migration IDs found */ duplicates: string[]; } /** * Validate migrations for common issues. * * @remarks * Performs comprehensive validation including: * - Duplicate migration IDs * - Modified applied migrations (checksum mismatch) * - Missing required fields * - Invalid timestamps * * @param migrations - Migrations to validate * @param applied - Applied migration records (for checksum validation) * @param options - Migration options (for strictness configuration) * @returns Validation result with errors and warnings * * @example * ```typescript * const result = validateMigrations(migrations, applied, { * strictChecksums: true * }); * * if (!result.valid) { * console.error("Validation errors:", result.errors); * throw new Error("Migration validation failed"); * } * ``` * * @public */ declare function validateMigrations(migrations: Migration[], applied: MigrationRecord[], options?: MigrationOptions): MigrationValidationResult; /** * Helper functions for managing migrations in database adapters. * * @remarks * These helpers reduce boilerplate code in dialect-specific adapters by * providing common operations like creating migration tables, recording * migrations, and querying migration status. * * Adapters can use these helpers or implement their own logic. * * @public */ interface MigrationHelpers { /** * Create the migrations tracking table if it doesn't exist. * * @remarks * Creates a table named `nextly_migrations` with columns: * - id (string, primary key) * - name (string) * - applied_at (timestamp) * - checksum (string, optional) * * Uses CREATE TABLE IF NOT EXISTS for safety. * * @param adapter - Database adapter to use * @returns Promise that resolves when table is created */ createMigrationsTable(adapter: DrizzleAdapter): Promise; /** * Get all applied migration records from the database. * * @remarks * Queries the `nextly_migrations` table and returns all records * sorted by applied_at descending (most recent first). * * @param adapter - Database adapter to use * @returns Promise with array of applied migration records */ getAppliedMigrations(adapter: DrizzleAdapter): Promise; /** * Record a migration as applied in the database. * * @remarks * Inserts a new record into the `nextly_migrations` table with: * - Migration ID, name * - Current timestamp for applied_at * - Calculated checksum * * Should be called within a transaction during migration execution. * * @param adapter - Database adapter to use * @param migration - Migration that was applied * @returns Promise that resolves when record is inserted */ recordMigration(adapter: DrizzleAdapter, migration: Migration): Promise; /** * Remove a migration record from the database. * * @remarks * Deletes a record from the `nextly_migrations` table. * Used during migration rollback operations. * * Should be called within a transaction during rollback. * * @param adapter - Database adapter to use * @param migrationId - ID of the migration to remove * @returns Promise that resolves when record is deleted */ removeMigrationRecord(adapter: DrizzleAdapter, migrationId: string): Promise; /** * Check if migrations table exists. * * @remarks * Queries database metadata to check if the `nextly_migrations` table exists. * Useful for determining if initialization is needed. * * @param adapter - Database adapter to use * @returns Promise with boolean indicating if table exists */ migrationsTableExists(adapter: DrizzleAdapter): Promise; } /** * Implementation of migration helpers. * * @remarks * Provides default implementations using the adapter's query methods. * These work across all dialects but adapters can optimize for their * specific database if needed. * * @public */ declare const migrationHelpers: MigrationHelpers; export { Migration, type MigrationHelpers, MigrationOptions, MigrationRecord, MigrationStatus, type MigrationValidationResult, calculateChecksum, detectModified, filterApplied, filterPending, getMigrationStatus, migrationHelpers, sortMigrations, validateChecksum, validateMigrations };