diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 00000000..711f4c4f --- /dev/null +++ b/.jshintrc @@ -0,0 +1,3 @@ +{ + "esversion": 11 +} diff --git a/src/database/index.js b/src/database/index.js index 78c68bb4..b3d13c3b 100644 --- a/src/database/index.js +++ b/src/database/index.js @@ -25,7 +25,23 @@ const logDebounce = _.debounce(() => { logger.info('Mirror DB not connected'); }, 120000); +export const mirrorDBEnabled = () => { + const CONFIG = getConfig(); + if ( + !CONFIG?.MIRROR_DB?.DB_HOST || + !CONFIG?.MIRROR_DB?.DB_NAME || + !CONFIG?.MIRROR_DB?.DB_USERNAME || + !CONFIG?.MIRROR_DB?.DB_PASSWORD + ) { + return false; + } +}; + export const safeMirrorDbHandler = (callback) => { + if (!mirrorDBEnabled()) { + return Promise.resolve(); + } + return new Promise((resolve) => { try { sequelizeMirror diff --git a/src/models/units/units.model.js b/src/models/units/units.model.js index 89918625..07c94324 100644 --- a/src/models/units/units.model.js +++ b/src/models/units/units.model.js @@ -191,8 +191,6 @@ class Unit extends Model { sql = `${sql} AND orgUid = :orgUid`; } - console.log('searchTerm', userSearchInput); - const replacements = { search: userSearchInput, orgUid }; const count = ( diff --git a/src/tasks/sync-audit-table.js b/src/tasks/sync-audit-table.js index 9576cedc..db32a033 100644 --- a/src/tasks/sync-audit-table.js +++ b/src/tasks/sync-audit-table.js @@ -1,7 +1,7 @@ import _ from 'lodash'; import { SimpleIntervalJob, Task } from 'toad-scheduler'; -import { Organization, Audit, ModelKeys, Staging } from '../models'; +import { Organization, Audit, ModelKeys, Staging, Meta } from '../models'; import datalayer from '../datalayer'; import { decodeHex } from '../utils/datalayer-utils'; import dotenv from 'dotenv'; @@ -12,6 +12,7 @@ import { assertDataLayerAvailable, assertWalletIsSynced, } from '../utils/data-assertions'; +import { mirrorDBEnabled } from '../database'; dotenv.config(); @@ -23,7 +24,39 @@ const task = new Task('sync-audit', async () => { try { if (!taskIsRunning) { taskIsRunning = true; - await processJob(); + + const hasMigratedToNewSyncMethod = await Meta.findOne({ + where: { metaKey: 'migratedToNewSync' }, + }); + + if (hasMigratedToNewSyncMethod) { + await processJob(); + } else { + logger.info( + 'Initiating migration to the new synchronization method. This will require a complete resynchronization of all data and may take some time.', + ); + + for (const modelKey of Object.keys(ModelKeys)) { + logger.info(`Resetting ${modelKey}`); + await ModelKeys[modelKey].destroy({ + where: {}, + truncate: true, + }); + } + + logger.info(`Resetting Audit Table`); + await Audit.destroy({ + where: {}, + truncate: true, + }); + + logger.info(`Completing Migration`); + await Meta.upsert({ + metaKey: 'migratedToNewSync', + metaValue: 'true', + }); + logger.info(`Migration Complete`); + } } } catch (error) { logger.error(`Error during datasync: ${error.message}`); @@ -52,7 +85,7 @@ const processJob = async () => { await assertDataLayerAvailable(); await assertWalletIsSynced(); - logger.info('Syncing Audit Information'); + logger.info('Syncing Registry Data'); const organizations = await Organization.findAll({ where: { subscribed: true }, raw: true, @@ -86,14 +119,20 @@ async function createTransaction(callback, afterCommitCallbacks) { logger.info('Starting transaction'); // Start a transaction transaction = await sequelize.transaction(); - mirrorTransaction = await sequelizeMirror.transaction(); + + if (mirrorDBEnabled()) { + mirrorTransaction = await sequelizeMirror.transaction(); + } // Execute the provided callback with the transaction result = await callback(transaction, mirrorTransaction); // Commit the transaction if the callback completes without errors await transaction.commit(); - await mirrorTransaction.commit(); + + if (mirrorDBEnabled()) { + await mirrorTransaction.commit(); + } for (const afterCommitCallback of afterCommitCallbacks) { await afterCommitCallback(); @@ -114,7 +153,7 @@ async function createTransaction(callback, afterCommitCallbacks) { const syncOrganizationAudit = async (organization) => { try { - logger.info(`Syncing Audit: ${_.get(organization, 'name')}`); + logger.info(`Syncing Registry: ${_.get(organization, 'name')}`); let afterCommitCallbacks = []; const rootHistory = await datalayer.getRootHistory(organization.registryId);