Skip to content

Commit

Permalink
Clear running migrations on server start (#3640)
Browse files Browse the repository at this point in the history
* Clear running migrations on server start

* Cleanup
  • Loading branch information
dmanjunath authored Aug 4, 2022
1 parent 2528cab commit 37ad770
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion creator-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { Keypair } = require('@solana/web3.js')
const initializeApp = require('./app')
const config = require('./config')
const { sequelize } = require('./models')
const { runMigrations } = require('./migrationManager')
const { runMigrations, clearRunningQueries } = require('./migrationManager')
const { logger } = require('./logging')
const { serviceRegistry } = require('./serviceRegistry')

Expand Down Expand Up @@ -38,6 +38,7 @@ const runDBMigrations = async () => {

const connectToDBAndRunMigrations = async () => {
await verifyDBConnection()
await clearRunningQueries()
await runDBMigrations()
}

Expand Down
26 changes: 25 additions & 1 deletion creator-node/src/migrationManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const Umzug = require('umzug')
const path = require('path')

const { sequelize } = require('./models')
const { logger } = require('./logging')

async function runMigrations() {
const umzug = new Umzug({
Expand All @@ -26,4 +27,27 @@ async function clearDatabase() {
await sequelize.query('CREATE SCHEMA public')
}

module.exports = { runMigrations, clearDatabase }
async function clearRunningQueries() {
logger.info(`Clearing running db queries...`)
try {
await sequelize.query(`
BEGIN;
SELECT
pg_cancel_backend(pid),
pid,
state,
age(clock_timestamp(), query_start),
substring(trim(regexp_replace(query, '\\s+', ' ', 'g')) from 1 for 200)
FROM pg_stat_activity
WHERE state != 'idle' AND query NOT ILIKE '%pg_stat_activity%'
ORDER BY query_start DESC;
COMMIT;
`)
} catch (e) {
logger.error(
`Error running clearRunningQueries: ${e.message}. Continuing with content node setup`
)
}
}

module.exports = { runMigrations, clearDatabase, clearRunningQueries }

0 comments on commit 37ad770

Please sign in to comment.