diff --git a/creator-node/src/index.ts b/creator-node/src/index.ts index 4cefbb923ff..f0967ef1063 100644 --- a/creator-node/src/index.ts +++ b/creator-node/src/index.ts @@ -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') @@ -38,6 +38,7 @@ const runDBMigrations = async () => { const connectToDBAndRunMigrations = async () => { await verifyDBConnection() + await clearRunningQueries() await runDBMigrations() } diff --git a/creator-node/src/migrationManager.js b/creator-node/src/migrationManager.js index 8d143c2cc72..507baa98644 100644 --- a/creator-node/src/migrationManager.js +++ b/creator-node/src/migrationManager.js @@ -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({ @@ -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 }