( db: SqliteRemoteDatabase<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: SqliteRemoteDatabase<TSchema>, |
| 10 | callback: ProxyMigrator, |
| 11 | config: MigrationConfig, |
| 12 | ) { |
| 13 | const migrations = readMigrationFiles(config); |
| 14 | |
| 15 | const migrationsTable = typeof config === 'string' |
| 16 | ? '__drizzle_migrations' |
| 17 | : config.migrationsTable ?? '__drizzle_migrations'; |
| 18 | |
| 19 | const migrationTableCreate = sql` |
| 20 | CREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsTable)} ( |
| 21 | id SERIAL PRIMARY KEY, |
| 22 | hash text NOT NULL, |
| 23 | created_at numeric |
| 24 | ) |
| 25 | `; |
| 26 | |
| 27 | await db.run(migrationTableCreate); |
| 28 | |
| 29 | const dbMigrations = await db.values<[number, string, string]>( |
| 30 | sql`SELECT id, hash, created_at FROM ${sql.identifier(migrationsTable)} ORDER BY created_at DESC LIMIT 1`, |
| 31 | ); |
| 32 | |
| 33 | const lastDbMigration = dbMigrations[0] ?? undefined; |
| 34 | |
| 35 | const queriesToRun: string[] = []; |
| 36 | for (const migration of migrations) { |
| 37 | if ( |
| 38 | !lastDbMigration |
| 39 | || Number(lastDbMigration[2])! < migration.folderMillis |
| 40 | ) { |
| 41 | queriesToRun.push( |
| 42 | ...migration.sql, |
| 43 | `INSERT INTO \`${migrationsTable}\` ("hash", "created_at") VALUES('${migration.hash}', '${migration.folderMillis}')`, |
| 44 | ); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | await callback(queriesToRun); |
| 49 | } |
nothing calls this directly
no test coverage detected