"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.migrate = migrate;
const sql_1 = require("drizzle-orm/sql");
const index_js_1 = __importDefault(require("./migrations/index.js"));
async function migrate(db) {
    const migrationsTable = "__drizzle_migrations";
    const migrationTableCreate = (0, sql_1.sql) `
    CREATE TABLE IF NOT EXISTS ${sql_1.sql.identifier(migrationsTable)} (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      hash TEXT NOT NULL
    )
  `;
    await db.run(migrationTableCreate).execute();
    const executedMigrations = await db.all((0, sql_1.sql) `SELECT hash FROM ${sql_1.sql.identifier(migrationsTable)} ORDER BY id`);
    const executedHashes = new Set(executedMigrations.map((m) => m.hash));
    for (const migration of index_js_1.default) {
        if (!executedHashes.has(migration.hash)) {
            await db.run(migration.sql).execute();
            await db
                .run((0, sql_1.sql) `INSERT INTO ${sql_1.sql.identifier(migrationsTable)} (hash) VALUES (${migration.hash})`)
                .execute();
            console.log(`Migration ${migration.hash} executed`);
        }
    }
}
