Skip to content

Commit

Permalink
Merge branch 'master' into eslint-prevent-application-code-in-migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn authored Jan 22, 2025
2 parents 223795e + f444627 commit 0dfe2e9
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"no-else-return": "off",
"no-multiple-empty-lines": "off",
"no-nested-ternary": "off",
"no-only-tests/no-only-tests": "error",
"no-only-tests/no-only-tests": [ "error", { "block": [ "describe", "it", "describeMigration" ] } ],
"no-restricted-syntax": "off",
"no-underscore-dangle": "off",
"nonblock-statement-body-position": "off",
Expand Down
4 changes: 2 additions & 2 deletions lib/bin/check-migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
// including this file, may be copied, modified, propagated, or distributed
// except according to the terms contained in the LICENSE file.

const { withDatabase, checkMigrations } = require('../model/migrate');
const { withKnex, checkMigrations } = require('../model/migrate');

(async () => {
try {
await withDatabase(require('config').get('default.database'))(checkMigrations);
await withKnex(require('config').get('default.database'))(checkMigrations);
} catch (err) {
console.error('Error:', err.message);
process.exit(1);
Expand Down
4 changes: 2 additions & 2 deletions lib/bin/check-open-db-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
// including this file, may be copied, modified, propagated, or distributed
// except according to the terms contained in the LICENSE file.

const { withDatabase } = require('../model/migrate');
const { withKnex } = require('../model/migrate');

(async () => {
try {
const { rows } = await withDatabase(require('config').get('default.database'))((db) =>
const { rows } = await withKnex(require('config').get('default.database'))((db) =>
db.raw('SELECT COUNT(*) FROM pg_stat_activity WHERE usename=CURRENT_USER'));
const queryCount = rows[0].count - 1; // the count query will appear as one of the open queries

Expand Down
4 changes: 2 additions & 2 deletions lib/bin/run-migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
// including this file, may be copied, modified, propagated, or distributed
// except according to the terms contained in the LICENSE file.

const { withDatabase, migrate } = require('../model/migrate');
const { withKnex, migrate } = require('../model/migrate');

(async () => {
try {
await withDatabase(require('config').get('default.database'))(migrate);
await withKnex(require('config').get('default.database'))(migrate);
} catch (err) {
console.error('Error:', err.message);
process.exit(1);
Expand Down
8 changes: 4 additions & 4 deletions lib/model/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ const knex = require('knex');
const { knexConnection } = require('../util/db');

// Connects to the postgres database specified in configuration and returns it.
const connect = (config) => knex({ client: 'pg', connection: knexConnection(config) });
const knexConnect = (config) => knex({ client: 'pg', connection: knexConnection(config) });

// Connects to a database, passes it to a function for operations, then ensures its closure.
const withDatabase = (config) => (mutator) => {
const db = connect(config);
const withKnex = (config) => (mutator) => {
const db = knexConnect(config);
return mutator(db).finally(() => db.destroy());
};

Expand All @@ -33,5 +33,5 @@ const checkMigrations = (db) => db.migrate.list({ directory: `${__dirname}/migra
process.exitCode = 1;
});

module.exports = { checkMigrations, connect, withDatabase, migrate };
module.exports = { checkMigrations, knexConnect, withKnex, migrate };

2 changes: 1 addition & 1 deletion lib/model/migrations/20191231-02-add-schema-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const up = async (db) => {
// this config hardcoding would be dangerous with tests except that
// tests will never invoke this path.
const config = require('config').get('default.database');
const db2 = require('../migrate').connect(config); // eslint-disable-line no-restricted-modules
const db2 = require('../migrate').knexConnect(config); // eslint-disable-line no-restricted-modules
return db2.select('projectId', 'xmlFormId').from('forms').where({ currentDefId: formDefId })
.then(([{ projectId, xmlFormId }]) => {
process.stderr.write(`\n!!!!\nThe database upgrade to v0.8 has failed because the Form '${xmlFormId}' in Project ${projectId} has an invalid schema. It tries to bind multiple instance nodes at the path ${path}.\n!!!!\n\n`);
Expand Down
4 changes: 2 additions & 2 deletions test/integration/other/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ const { testContainerFullTrx, testServiceFullTrx } = require('../setup');
const { sql } = require('slonik');
const { createReadStream } = require('fs');
const { Actor, Config } = require(appRoot + '/lib/model/frames');
const { withDatabase } = require(appRoot + '/lib/model/migrate');
const { withKnex } = require(appRoot + '/lib/model/migrate');
const { exhaust } = require(appRoot + '/lib/worker/worker');

const testData = require('../../data/xml');
const populateUsers = require('../fixtures/01-users');
const populateForms = require('../fixtures/02-forms');
const { getFormFields } = require('../../../lib/data/schema');

const withTestDatabase = withDatabase(config.get('test.database'));
const withTestDatabase = withKnex(config.get('test.database'));
const migrationsDir = appRoot + '/lib/model/migrations';
const upToMigration = (toName, inclusive = true) => withTestDatabase(async (migrator) => {
await migrator.raw('drop owned by current_user');
Expand Down
4 changes: 2 additions & 2 deletions test/integration/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const testData = require('../data/xml');

// knex things.
const config = require('config');
const { connect } = require(appRoot + '/lib/model/migrate');
const { knexConnect } = require(appRoot + '/lib/model/migrate');

// slonik connection pool
const { slonikPool } = require(appRoot + '/lib/external/slonik');
Expand Down Expand Up @@ -72,7 +72,7 @@ const populate = (container, [ head, ...tail ] = fixtures) =>
// this hook won't run if `test-unit` is called, as this directory is skipped
// in that case.
const initialize = async () => {
const migrator = connect(config.get('test.database'));
const migrator = knexConnect(config.get('test.database'));
const { log } = console;
try {
await migrator.raw('drop owned by current_user');
Expand Down

0 comments on commit 0dfe2e9

Please sign in to comment.