From 4f64b8910e1295207a42c757cb81c9b0e9ee3be2 Mon Sep 17 00:00:00 2001 From: Justin Wind Date: Thu, 5 Aug 2021 09:18:19 -0700 Subject: [PATCH] renamed database schemaCheck method to initialize As more functionality had been heaped onto this method, it's name was updated to be more fitting. --- server.js | 2 +- src/db/base.js | 16 ++++++++-------- src/db/postgres/index.js | 6 +++--- test/src/db/base.js | 8 ++++---- test/src/db/integration.js | 2 +- test/src/db/postgres.js | 12 ++++++------ test/stub-db.js | 2 +- 7 files changed, 24 insertions(+), 24 deletions(-) diff --git a/server.js b/server.js index 985e575..7658097 100644 --- a/server.js +++ b/server.js @@ -19,7 +19,7 @@ const ADDR = process.env.LISTEN_ADDR || '127.0.0.1'; config = new Config(process.env.NODE_ENV); logger = new Logger(config); db = new DB(logger, config); - await db.schemaCheck(); + await db.initialize(); service = new Service(logger, db, config); http.createServer((req, res) => { diff --git a/src/db/base.js b/src/db/base.js index 8a1df74..8d72d0f 100644 --- a/src/db/base.js +++ b/src/db/base.js @@ -83,14 +83,14 @@ class Database { /** - * Validate schema compatibility. - * Ensure this is called immediately after instantiating a DB instance, - * as some engines also finish initialization and validation here, which - * was easier than wrangling async calls in constructor. - * In light of this behavior, this method could be named better. - */ - async schemaCheck() { - const _scope = _fileScope('schemaCheck'); + * Perform tasks needed to prepare database for use. Ensure this is called + * after construction, and before any other database activity. + * At the minimum, this will validate a compatible schema is present and usable. + * Some engines will also perform other initializations or async actions which + * are easier handled outside the constructor. + */ + async initialize() { + const _scope = _fileScope('initialize'); const currentSchema = await this._currentSchema(); const current = svh.schemaVersionObjectToNumber(currentSchema); diff --git a/src/db/postgres/index.js b/src/db/postgres/index.js index f4f690a..d02d981 100644 --- a/src/db/postgres/index.js +++ b/src/db/postgres/index.js @@ -100,13 +100,13 @@ class DatabasePostgres extends Database { } - async schemaCheck(applyMigrations = true) { - const _scope = _fileScope('schemaCheck'); + async initialize(applyMigrations = true) { + const _scope = _fileScope('initialize'); this.logger.debug(_scope, 'called', { applyMigrations }); if (applyMigrations) { await this._initTables(); } - await super.schemaCheck(); + await super.initialize(); } diff --git a/test/src/db/base.js b/test/src/db/base.js index 18871f4..7863657 100644 --- a/test/src/db/base.js +++ b/test/src/db/base.js @@ -108,7 +108,7 @@ describe('DatabaseBase', function () { }); }); // _ensureTypes - describe('schemaCheck', function () { + describe('initialize', function () { let currentSchema; beforeEach(function () { currentSchema = { @@ -123,7 +123,7 @@ describe('DatabaseBase', function () { sinon.stub(db, '_currentSchema').resolves(currentSchema); }); it('covers success', async function () { - await db.schemaCheck(); + await db.initialize(); }); it('covers failure', async function() { db.schemaVersionsSupported = { @@ -139,13 +139,13 @@ describe('DatabaseBase', function () { }, }; try { - await db.schemaCheck(); + await db.initialize(); assert.fail('did not get expected exception'); } catch (e) { assert(e instanceof DBErrors.MigrationNeeded); } }); - }); // schemaCheck + }); // initialize describe('_topicDefaults', function () { let topic; diff --git a/test/src/db/integration.js b/test/src/db/integration.js index 3cb07f9..e6f632c 100644 --- a/test/src/db/integration.js +++ b/test/src/db/integration.js @@ -69,7 +69,7 @@ describe('Database Integration', function () { // eslint-disable-next-line security/detect-non-literal-require DB = require(i.module); db = new DB(stubLogger, i.config); - await db.schemaCheck(); + await db.initialize(); await db._purgeTables(true); }); after(async function () { diff --git a/test/src/db/postgres.js b/test/src/db/postgres.js index 5df49fd..5aff1e8 100644 --- a/test/src/db/postgres.js +++ b/test/src/db/postgres.js @@ -155,17 +155,17 @@ describe('DatabasePostgres', function () { }); }); // _initTables - describe('schemaCheck', function () { + describe('initialize', function () { it('passes supported version', async function () { const version = { major: 1, minor: 0, patch: 0 }; sinon.stub(db.db, 'one').resolves(version); - await db.schemaCheck(false); + await db.initialize(false); }); it('fails low version', async function () { const version = { major: 0, minor: 0, patch: 0 }; sinon.stub(db.db, 'one').resolves(version); try { - await db.schemaCheck(false); + await db.initialize(false); assert.fail(noExpectedException); } catch (e) { assert(e instanceof DBErrors.MigrationNeeded); @@ -175,7 +175,7 @@ describe('DatabasePostgres', function () { const version = { major: 100, minor: 100, patch: 100 }; sinon.stub(db.db, 'one').resolves(version); try { - await db.schemaCheck(false); + await db.initialize(false); assert.fail(noExpectedException); } catch (e) { assert(e instanceof DBErrors.MigrationNeeded); @@ -186,9 +186,9 @@ describe('DatabasePostgres', function () { sinon.stub(db.db, 'multiResult'); sinon.stub(db, '_currentSchema').resolves(db.schemaVersionsSupported.max); sinon.stub(db.db, 'one').resolves(db.schemaVersionsSupported.max); - await db.schemaCheck(); + await db.initialize(); }); - }); // schemaCheck + }); // initialize describe('healthCheck', function () { beforeEach(function () { diff --git a/test/stub-db.js b/test/stub-db.js index 608ea77..5ef2422 100644 --- a/test/stub-db.js +++ b/test/stub-db.js @@ -13,7 +13,7 @@ const stubFns = [ 'authenticationGet', 'authenticationUpsert', 'healthCheck', - 'schemaCheck', + 'initialize', 'subscriptionsByTopicId', 'subscriptionCountByTopicUrl', 'subscriptionDelete', -- 2.45.2