Skip to content

Commit

Permalink
fix migration mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
dumpstate committed Jul 15, 2023
1 parent 92c12f1 commit 4153bf2
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
12 changes: 7 additions & 5 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -83,7 +83,7 @@ async function currentRevision(pg: PGPoolClient): Promise<number | null> {
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(
Expand Down Expand Up @@ -178,7 +178,9 @@ export async function migrateDown(logger: Logger, pg: PGPool): Promise<void> {
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")
Expand Down
65 changes: 65 additions & 0 deletions test/schema.test.ts
Original file line number Diff line number Diff line change
@@ -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<string[]> {
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"))
})
})

0 comments on commit 4153bf2

Please sign in to comment.