Skip to content

Commit

Permalink
feat: automatic migration to new sync method
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Oct 16, 2023
1 parent 6f5d407 commit 7fab62e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"esversion": 11
}
16 changes: 16 additions & 0 deletions src/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/models/units/units.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,6 @@ class Unit extends Model {
sql = `${sql} AND orgUid = :orgUid`;
}

console.log('searchTerm', userSearchInput);

const replacements = { search: userSearchInput, orgUid };

const count = (
Expand Down
51 changes: 45 additions & 6 deletions src/tasks/sync-audit-table.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -12,6 +12,7 @@ import {
assertDataLayerAvailable,
assertWalletIsSynced,
} from '../utils/data-assertions';
import { mirrorDBEnabled } from '../database';

dotenv.config();

Expand All @@ -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}`);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand All @@ -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);

Expand Down

0 comments on commit 7fab62e

Please sign in to comment.