(db: Database, input: Migration[])
| 41 | } |
| 42 | |
| 43 | export function applyOnly(db: Database, input: Migration[]) { |
| 44 | return Effect.gen(function* () { |
| 45 | yield* db.run( |
| 46 | sql`CREATE TABLE IF NOT EXISTS ${sql.identifier("migration")} (id TEXT PRIMARY KEY, time_completed INTEGER NOT NULL)`, |
| 47 | ) |
| 48 | let completed = new Set( |
| 49 | (yield* db.all<{ id: string }>(sql`SELECT id FROM ${sql.identifier("migration")}`)).map((row) => row.id), |
| 50 | ) |
| 51 | if (completed.size === 0) { |
| 52 | // Existing installs used Drizzle's migration journal. Seed the new |
| 53 | // journal once so TypeScript migrations don't replay old SQL. |
| 54 | if ( |
| 55 | yield* db.get(sql`SELECT name FROM sqlite_master WHERE type = 'table' AND name = ${"__drizzle_migrations"}`) |
| 56 | ) { |
| 57 | yield* db.run(sql` |
| 58 | INSERT OR IGNORE INTO ${sql.identifier("migration")} (id, time_completed) |
| 59 | SELECT name, ${Date.now()} |
| 60 | FROM ${sql.identifier("__drizzle_migrations")} |
| 61 | WHERE name IS NOT NULL |
| 62 | `) |
| 63 | completed = new Set( |
| 64 | (yield* db.all<{ id: string }>(sql`SELECT id FROM ${sql.identifier("migration")}`)).map((row) => row.id), |
| 65 | ) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | for (const migration of input) { |
| 70 | if (completed.has(migration.id)) continue |
| 71 | yield* db.transaction((tx) => |
| 72 | Effect.gen(function* () { |
| 73 | yield* migration.up(tx) |
| 74 | yield* tx.run( |
| 75 | sql`INSERT INTO ${sql.identifier("migration")} (id, time_completed) VALUES (${migration.id}, ${Date.now()})`, |
| 76 | ) |
| 77 | }), |
| 78 | ) |
| 79 | } |
| 80 | }) |
| 81 | } |
no test coverage detected