CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
migrator.cjs85 linesDownload Raw Back to durable-sqlite
1"use strict";2var __defProp = Object.defineProperty;3var __getOwnPropDesc = Object.getOwnPropertyDescriptor;4var __getOwnPropNames = Object.getOwnPropertyNames;5var __hasOwnProp = Object.prototype.hasOwnProperty;6var __export = (target, all) => {7  for (var name in all)8    __defProp(target, name, { get: all[name], enumerable: true });9};10var __copyProps = (to, from, except, desc) => {11  if (from && typeof from === "object" || typeof from === "function") {12    for (let key of __getOwnPropNames(from))13      if (!__hasOwnProp.call(to, key) && key !== except)14        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });15  }16  return to;17};18var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);19var migrator_exports = {};20__export(migrator_exports, {21  migrate: () => migrate22});23module.exports = __toCommonJS(migrator_exports);24var import_sql = require("../sql/index.cjs");25function readMigrationFiles({ journal, migrations }) {26  const migrationQueries = [];27  for (const journalEntry of journal.entries) {28    const query = migrations[`m${journalEntry.idx.toString().padStart(4, "0")}`];29    if (!query) {30      throw new Error(`Missing migration: ${journalEntry.tag}`);31    }32    try {33      const result = query.split("--> statement-breakpoint").map((it) => {34        return it;35      });36      migrationQueries.push({37        sql: result,38        bps: journalEntry.breakpoints,39        folderMillis: journalEntry.when,40        hash: ""41      });42    } catch {43      throw new Error(`Failed to parse migration: ${journalEntry.tag}`);44    }45  }46  return migrationQueries;47}48async function migrate(db, config) {49  const migrations = readMigrationFiles(config);50  db.transaction((tx) => {51    try {52      const migrationsTable = "__drizzle_migrations";53      const migrationTableCreate = import_sql.sql`54				CREATE TABLE IF NOT EXISTS ${import_sql.sql.identifier(migrationsTable)} (55					id SERIAL PRIMARY KEY,56					hash text NOT NULL,57					created_at numeric58				)59			`;60      db.run(migrationTableCreate);61      const dbMigrations = db.values(62        import_sql.sql`SELECT id, hash, created_at FROM ${import_sql.sql.identifier(migrationsTable)} ORDER BY created_at DESC LIMIT 1`63      );64      const lastDbMigration = dbMigrations[0] ?? void 0;65      for (const migration of migrations) {66        if (!lastDbMigration || Number(lastDbMigration[2]) < migration.folderMillis) {67          for (const stmt of migration.sql) {68            db.run(import_sql.sql.raw(stmt));69          }70          db.run(71            import_sql.sql`INSERT INTO ${import_sql.sql.identifier(migrationsTable)} ("hash", "created_at") VALUES(${migration.hash}, ${migration.folderMillis})`72          );73        }74      }75    } catch (error) {76      tx.rollback();77      throw error;78    }79  });80}81// Annotate the CommonJS export names for ESM import in node:820 && (module.exports = {83  migrate84});85//# sourceMappingURL=migrator.cjs.map