( db: PgRemoteDatabase<TSchema>, callback: ProxyMigrator, config: MigrationConfig, )
| 6 | export type ProxyMigrator = (migrationQueries: string[]) => Promise<void>; |
| 7 | |
| 8 | export async function migrate<TSchema extends Record<string, unknown>>( |
| 9 | db: PgRemoteDatabase<TSchema>, |
| 10 | callback: ProxyMigrator, |
| 11 | config: MigrationConfig, |
| 12 | ) { |
| 13 | const migrations = readMigrationFiles(config); |
| 14 | |
| 15 | const migrationTableCreate = sql` |
| 16 | CREATE TABLE IF NOT EXISTS "drizzle"."__drizzle_migrations" ( |
| 17 | id SERIAL PRIMARY KEY, |
| 18 | hash text NOT NULL, |
| 19 | created_at numeric |
| 20 | ) |
| 21 | `; |
| 22 | |
| 23 | await db.execute(sql`CREATE SCHEMA IF NOT EXISTS "drizzle"`); |
| 24 | await db.execute(migrationTableCreate); |
| 25 | |
| 26 | const dbMigrations = await db.execute<{ |
| 27 | id: number; |
| 28 | hash: string; |
| 29 | created_at: string; |
| 30 | }>( |
| 31 | sql`SELECT id, hash, created_at FROM "drizzle"."__drizzle_migrations" ORDER BY created_at DESC LIMIT 1`, |
| 32 | ); |
| 33 | |
| 34 | const lastDbMigration = dbMigrations[0] ?? undefined; |
| 35 | |
| 36 | const queriesToRun: string[] = []; |
| 37 | |
| 38 | for (const migration of migrations) { |
| 39 | if ( |
| 40 | !lastDbMigration |
| 41 | || Number(lastDbMigration.created_at)! < migration.folderMillis |
| 42 | ) { |
| 43 | queriesToRun.push( |
| 44 | ...migration.sql, |
| 45 | `INSERT INTO "drizzle"."__drizzle_migrations" ("hash", "created_at") VALUES('${migration.hash}', '${migration.folderMillis}')`, |
| 46 | ); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | await callback(queriesToRun); |
| 51 | } |
nothing calls this directly
no test coverage detected