Skip to content

Commit

Permalink
Added readBundle
Browse files Browse the repository at this point in the history
  • Loading branch information
viktor-podzigun committed Jun 17, 2024
1 parent 85b8fb7 commit 76c0495
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
3 changes: 3 additions & 0 deletions index.d.mts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { URL } from "node:url";
import Database from "@farjs/better-sqlite3-wrapper";

export interface MigrationBundleItem {
Expand All @@ -7,6 +8,8 @@ export interface MigrationBundleItem {

export type MigrationBundle = MigrationBundleItem[];

export function readBundle(url: URL): Promise<MigrationBundle>;

export function runBundle(db: Database, bundle: MigrationBundle): Promise<void>;

export function createBundle(args: string[]): Promise<void>;
10 changes: 10 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
/**
* @typedef {import("node:url").URL} URL
* @typedef {import("@farjs/better-sqlite3-wrapper").Database} Database
* @typedef {import("./index.mjs").MigrationBundle} MigrationBundle
*/

/**
* @param {URL} url
* @returns {Promise<MigrationBundle>}
*/
export async function readBundle(url) {
const { readBundle } = await import("./src/runner.mjs");
return readBundle(url);
}

/**
* @param {Database} db
* @param {MigrationBundle} bundle
Expand Down
6 changes: 5 additions & 1 deletion src/bundler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ export async function createBundle(args) {
};
});

fs.writeFileSync(migrationsBundle, JSON.stringify(bundleObj, undefined, 2));
fs.writeFileSync(
migrationsBundle,
JSON.stringify(bundleObj, undefined, 2),
{ encoding: "utf8" }
);
console.log(`Generated SQL bundle file: ${migrationsBundle}`);
return;
}
Expand Down
14 changes: 14 additions & 0 deletions src/runner.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/**
* @typedef {import("node:url").URL} URL
* @typedef {import("@farjs/better-sqlite3-wrapper").Database} Database
* @typedef {import("../index.mjs").MigrationBundle} MigrationBundle
*/
import fs from "fs";
import Database from "@farjs/better-sqlite3-wrapper";

/**
Expand All @@ -16,6 +18,18 @@ const dbTable = "schema_versions";
const versionAndNameRegex = /V(\d+)__(.+).sql/i;
const underscoreRegex = /_/g;

/**
* @param {URL} url
* @returns {Promise<MigrationBundle>}
*/
export async function readBundle(url) {
const json = fs.readFileSync(url, { encoding: "utf8" });

/** @type {MigrationBundle} */
const bundle = JSON.parse(json);
return bundle;
}

/**
* @param {Database} db
* @param {MigrationBundle} bundle
Expand Down
38 changes: 37 additions & 1 deletion test/runner.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
* @typedef {import("@farjs/better-sqlite3-wrapper").Database} Database
* @typedef {import("../index.mjs").MigrationBundleItem} MigrationBundleItem
*/
import { URL } from "node:url";
import assert from "node:assert/strict";
import mockFunction from "mock-fn";
import Database from "@farjs/better-sqlite3-wrapper";
import { runBundle } from "../index.mjs";
import { readBundle, runBundle } from "../index.mjs";

const { describe, it } = await (async () => {
// @ts-ignore
Expand Down Expand Up @@ -351,6 +352,41 @@ describe("runner.test.mjs", () => {
assertSchema(db, [{ version: 1, name: "non-transactional migration" }]);
});

it("should run migrations from bundle.json", async () => {
//given
const db = new Database(":memory:");
const logs = /** @type {string[]} */ ([]);
const logMock = mockFunction((msg) => {
logs.push(msg);
});
const savedLog = console.log;
console.log = logMock;

//when & then
const bundleUrl = new URL("./migrations/bundle.json", import.meta.url);
const bundle = await readBundle(bundleUrl);

//when
await runBundle(db, bundle);

//then
console.log = savedLog;
assert.deepEqual(logMock.times, 3);
assert.deepEqual(logs, [
"DB: migrating to version 1 - initial db structure",
"DB: migrating to version 2 - rename db field",
"DB: 2 migration(s) were applied successfully",
]);
assertDb(db, [
{ id: 1, name: "test 1" },
{ id: 2, name: "test 2" },
]);
assertSchema(db, [
{ version: 1, name: "initial db structure" },
{ version: 2, name: "rename db field" },
]);
});

it("should fail if cannot parse migration version and name", async () => {
//given
const db = new Database(":memory:");
Expand Down

0 comments on commit 76c0495

Please sign in to comment.