diff --git a/package-lock.json b/package-lock.json index 81a66f2..b23f119 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@dumpstate/bongojs", - "version": "0.24.1", + "version": "0.24.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@dumpstate/bongojs", - "version": "0.24.1", + "version": "0.24.2", "license": "MIT", "dependencies": { "@dumpstate/dbaction": "^0.2.5", diff --git a/package.json b/package.json index 8a344d1..77be259 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dumpstate/bongojs", - "version": "0.24.1", + "version": "0.24.2", "description": "PostgreSQL JSON document modeling tool for node.js", "main": "lib/index.js", "bin": { diff --git a/src/schema.ts b/src/schema.ts index 37204d9..ae3b821 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -10,14 +10,14 @@ interface Revision { readonly down: string } -const REVISIONS: Revision[] = [ +export const REVISIONS: Revision[] = [ { id: 1, up: ` CREATE TABLE IF NOT EXISTS ${REVISION_TABLE} ( ${REVISION_COLUMN} INTEGER PRIMARY KEY )`.trim(), - down: `DROP TABLE ${REVISION_TABLE}`, + down: `DROP TABLE IF EXISTS ${REVISION_TABLE}`, }, { id: 2, @@ -28,7 +28,7 @@ doctype VARCHAR, doc JSONB NOT NULL, PRIMARY KEY (id, doctype) ) PARTITION BY LIST(doctype)`.trim(), - down: `DROP TABLE ${DOCUMENT_TABLE}`, + down: `DROP TABLE IF EXISTS ${DOCUMENT_TABLE}`, }, { id: 3, @@ -83,7 +83,7 @@ async function currentRevision(pg: PGPoolClient): Promise { FROM ${REVISION_TABLE} `) - return res.rowCount === 1 ? res.rows[0]["max"] : null + return res.rowCount === 1 ? res.rows[0][REVISION_COLUMN] : null } async function setCurrentRevision( @@ -178,7 +178,9 @@ export async function migrateDown(logger: Logger, pg: PGPool): Promise { await conn.query("BEGIN") logger.info(`DOWN(${rev.id}) :: ${rev.down}`) await conn.query(rev.down) - await setCurrentRevision(conn, rev) + if (rev.id !== 1) { + await setCurrentRevision(conn, rev) + } await conn.query("COMMIT") } catch (err: any) { await conn.query("ROLLBACK") diff --git a/test/schema.test.ts b/test/schema.test.ts new file mode 100644 index 0000000..ac55294 --- /dev/null +++ b/test/schema.test.ts @@ -0,0 +1,65 @@ +import assert from "node:assert/strict" +import { Pool } from "pg" +import { includes } from "lodash" +import { Bongo } from "../src/Bongo" +import { REVISIONS } from "../src/schema" + +async function getTables(pg: Pool): Promise { + return ( + await pg.query(` + SELECT table_name + FROM information_schema.tables + WHERE table_schema = 'public' + `) + ).rows.map((row) => row.table_name) +} + +describe("migrateUp", () => { + const bongo = new Bongo() + + after(async () => { + await bongo.drop() + await bongo.close() + }) + + it("should create tables", async () => { + await bongo.migrate() + const tables = await getTables(bongo.pg) + + assert.ok(includes(tables, "bongo_revision")) + assert.ok(includes(tables, "bongo_documents")) + }) + + it("should set revision to the latest", async () => { + await bongo.migrate() + + const revision = ( + await bongo.pg.query(` + SELECT revision + FROM bongo_revision + `) + ).rows[0].revision + + assert.equal(revision, Math.max(...REVISIONS.map((r) => r.id))) + }) +}) + +describe("migrateDown", () => { + const bongo = new Bongo() + + before(async () => { + await bongo.migrate() + }) + + after(async () => { + await bongo.close() + }) + + it("should drop tables", async () => { + await bongo.drop() + const tables = await getTables(bongo.pg) + + assert.ok(!includes(tables, "bongo_revision")) + assert.ok(!includes(tables, "bongo_documents")) + }) +})