Skip to content

Commit

Permalink
Created migration to resync translations
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaPolit committed Mar 26, 2021
1 parent 0cdb26f commit 187fcd1
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
17 changes: 17 additions & 0 deletions app/api/migrations/migrations/37-resync-translations/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default {
delta: 37,

name: 'resync-tranlsations',

description: 'Moves timestamp of current translation update logs forward to force resync',

async up(db) {
process.stdout.write('Updating updatelogs of translations...\r\n');

await db
.collection('updatelogs')
.updateMany({ namespace: 'translations' }, { $set: { timestamp: Date.now() } });

process.stdout.write('Updatelogs updated\r\n');
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import testingDB from 'api/utils/testing_db';
import migration from '../index.js';
import fixtures, { translation1, translation2, translation3, entity1 } from './fixtures';

describe('migration resync translations', () => {
let updatelogs;

beforeEach(async () => {
await testingDB.clearAllAndLoad(fixtures);
spyOn(process.stdout, 'write');
spyOn(Date, 'now').and.returnValue(1000);
await migration.up(testingDB.mongodb);
});

afterAll(async () => {
await testingDB.disconnect();
});

const getUpdatelog = mongoId => updatelogs.find(l => l.mongoId.toString() === mongoId.toString());

const expectLog = (logEntry, [namespace, deleted, timestamp]) => {
expect(logEntry).toEqual(expect.objectContaining({ namespace, deleted, timestamp }));
};

it('should have a delta number', () => {
expect(migration.delta).toBe(37);
});

it('should update the translation updatelogs to current timestamp and not affect others', async () => {
updatelogs = await testingDB.mongodb
.collection('updatelogs')
.find({})
.toArray();

expect(updatelogs.length).toBe(4);

const logTranslation1 = getUpdatelog(translation1);
const logTranslation2 = getUpdatelog(translation2);
const logTranslation3 = getUpdatelog(translation3);
const logEntity1 = getUpdatelog(entity1);

expectLog(logTranslation1, ['translations', false, 1000]);
expectLog(logTranslation2, ['translations', false, 1000]);
expectLog(logTranslation3, ['translations', true, 1000]);
expectLog(logEntity1, ['entities', true, 8]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import testingDB from 'api/utils/testing_db';

const translation1 = testingDB.id();
const translation2 = testingDB.id();
const translation3 = testingDB.id();
const entity1 = testingDB.id();

export default {
updatelogs: [
{
mongoId: translation1,
namespace: 'translations',
deleted: false,
timestamp: 6,
},
{
mongoId: entity1,
namespace: 'entities',
deleted: true,
timestamp: 8,
},
{
mongoId: translation2,
namespace: 'translations',
deleted: false,
timestamp: 10,
},
{
mongoId: translation3,
namespace: 'translations',
deleted: true,
timestamp: 12,
},
],
};

export { translation1, translation2, translation3, entity1 };

0 comments on commit 187fcd1

Please sign in to comment.