Skip to content

Commit

Permalink
Fix get version error when schemaversion table does not exist (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
rickbergfalk committed Feb 19, 2022
1 parent 5bb63e5 commit 04263a6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ class Client {
return config.execQuery(query);
}

async hasVersionTable() {
const sql = this.getColumnsSql();
const results = await this.runQuery(sql);
const { rows } = results;
return rows.length > 0;
}

async ensureTable() {
const { config } = this;
const sql = this.getColumnsSql();
Expand Down
6 changes: 6 additions & 0 deletions postgrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ class Postgrator extends EventEmitter {
*/
async getDatabaseVersion() {
const versionSql = this.commonClient.getDatabaseVersionSql();

const initialized = await this.commonClient.hasVersionTable();
if (!initialized) {
return undefined;
}

const result = await this.commonClient.runQuery(versionSql);
const version = result.rows.length > 0 ? result.rows[0].version : 0;
return parseInt(version);
Expand Down
5 changes: 5 additions & 0 deletions test/drivers.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ function driverExecQuery(factoryFunction, label) {
await end();
});

it("Returns undefined for database version before init", async function () {
const result = await postgrator.getDatabaseVersion();
assert.strictEqual(result, undefined);
});

it("Migrates multiple versions up (000 -> 002)", function () {
return postgrator
.migrate("002")
Expand Down

0 comments on commit 04263a6

Please sign in to comment.