npm package diff

Package: @forge/sql

Versions: 2.0.1-next.1 - 2.1.0-next.2

File: package/out/migration.js

Index: package/out/migration.js
===================================================================
--- package/out/migration.js
+++ package/out/migration.js
@@ -0,0 +1,64 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.migrationRunner = exports.MigrationRunner = void 0;
+const sql_1 = require("./sql");
+const response_handler_1 = require("./utils/response-handler");
+const SCHEMA_VERSION_TABLE_CREATE_QUERY = 'CREATE TABLE IF NOT EXISTS __migrations (id BIGINT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255) NOT NULL, migratedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP);';
+const INSERT_SCHEMA_VERSION_QUERY = 'INSERT INTO __migrations (name) VALUES (?);';
+const LIST_MIGRATIONS_QUERY = 'SELECT id, name, migratedAt FROM __migrations;';
+class MigrationRunner {
+    sqlClient;
+    migrations;
+    constructor(sqlClient) {
+        this.sqlClient = sqlClient;
+        this.migrations = [];
+    }
+    async initialize() {
+        await this.sqlClient.executeRaw(SCHEMA_VERSION_TABLE_CREATE_QUERY);
+    }
+    enqueue(name, statement) {
+        if (this.migrations.some((migration) => migration.name === name)) {
+            return this;
+        }
+        this.migrations.push({ name, statement });
+        return this;
+    }
+    getEnqueued() {
+        return this.migrations;
+    }
+    async list() {
+        const result = await this.sqlClient.executeRaw(LIST_MIGRATIONS_QUERY);
+        const migrations = new Array();
+        for (const row of result.rows) {
+            const migratedAt = new Date(row.migratedAt);
+            const id = row.id;
+            const name = row.name;
+            migrations.push({ id, name, migratedAt });
+        }
+        return migrations;
+    }
+    async run() {
+        await this.initialize();
+        const previousMigrations = await this.list();
+        const migrationsToRun = this.migrations.filter((migration) => !previousMigrations.some((prev) => prev.name === migration.name));
+        const migrationsSuccessfullyRun = [];
+        for (const migration of migrationsToRun) {
+            try {
+                await this.sqlClient.executeRaw(migration.statement);
+            }
+            catch (error) {
+                throw new response_handler_1.MigrationExecutionError(migration.name, migrationsToRun.map((m) => m.name));
+            }
+            try {
+                await this.sqlClient.prepare(INSERT_SCHEMA_VERSION_QUERY).bindParams(migration.name).execute();
+            }
+            catch (error) {
+                throw new response_handler_1.MigrationCheckPointError(migration.name, migrationsToRun.map((m) => m.name));
+            }
+            migrationsSuccessfullyRun.push(migration.name);
+        }
+        return migrationsSuccessfullyRun;
+    }
+}
+exports.MigrationRunner = MigrationRunner;
+exports.migrationRunner = new MigrationRunner(sql_1.sql);