Skip to content

Commit

Permalink
v1.145.0
Browse files Browse the repository at this point in the history
  • Loading branch information
daneryl committed Dec 4, 2023
2 parents 6c91cbb + c5686b2 commit a718f2e
Show file tree
Hide file tree
Showing 27 changed files with 2,612 additions and 1,806 deletions.
42 changes: 42 additions & 0 deletions app/api/migrations/migrations/150-per_namespace_lastSyncs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Db } from 'mongodb';

export default {
delta: 150,

name: 'per_namespace_lastSyncs',

description: 'Note the last synced timestamp on the syncs separately for each namespace.',

reindex: false,

async up(db: Db) {
process.stdout.write(`${this.name}...\r\n`);
const syncs = await db.collection('syncs').find().toArray();
const updateOperations = syncs.map((sync: any) => {
const timestamp = sync.lastSync || 0;
return {
updateOne: {
filter: { _id: sync._id },
update: {
$set: {
lastSyncs: {
settings: timestamp,
translationsV2: timestamp,
dictionaries: timestamp,
relationtypes: timestamp,
templates: timestamp,
files: timestamp,
connections: timestamp,
entities: timestamp,
},
},
$unset: { lastSync: '' },
},
},
};
});
if (updateOperations.length) {
await db.collection('syncs').bulkWrite(updateOperations);
}
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Db, ObjectId } from 'mongodb';

import testingDB from 'api/utils/testing_db';
import migration from '../index';
import { fixtures } from './fixtures';

let db: Db | null;

beforeAll(async () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
jest.spyOn(process.stdout, 'write').mockImplementation((str: string | Uint8Array) => true);
await testingDB.setupFixturesAndContext(fixtures);
db = testingDB.mongodb!;
await migration.up(db);
});

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

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

it('should add timestamps per collection to the sync objects', async () => {
const syncs = await db!.collection('syncs').find().toArray();
expect(syncs).toEqual([
{
_id: expect.any(ObjectId),
name: 'sync1',
lastSyncs: {
settings: 0,
translationsV2: 0,
dictionaries: 0,
relationtypes: 0,
templates: 0,
files: 0,
connections: 0,
entities: 0,
},
},
{
_id: expect.any(ObjectId),
name: 'sync2',
lastSyncs: {
settings: 1700127956,
translationsV2: 1700127956,
dictionaries: 1700127956,
relationtypes: 1700127956,
templates: 1700127956,
files: 1700127956,
connections: 1700127956,
entities: 1700127956,
},
},
{
_id: expect.any(ObjectId),
name: 'sync3',
lastSyncs: {
settings: 0,
translationsV2: 0,
dictionaries: 0,
relationtypes: 0,
templates: 0,
files: 0,
connections: 0,
entities: 0,
},
},
]);
});

it('should not reindex', async () => {
expect(migration.reindex).toBe(false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { DBFixture } from '../types';

export const fixtures: DBFixture = {
syncs: [
{
name: 'sync1',
lastSync: 0,
},
{
name: 'sync2',
lastSync: 1700127956,
},
{
name: 'sync3',
},
],
};
23 changes: 23 additions & 0 deletions app/api/migrations/migrations/150-per_namespace_lastSyncs/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
type OldSyncs = {
name: string;
lastSync: number;
};

type FaultyOldSync = {
name: string;
};

type NewSyncs = {
names: string;
lastSyncs: {
[name: string]: number;
};
};

type Syncs = OldSyncs | NewSyncs | FaultyOldSync;

type DBFixture = {
syncs: Syncs[];
};

export type { DBFixture, Syncs, OldSyncs, NewSyncs };
2 changes: 1 addition & 1 deletion app/api/suggestions/suggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ const updateExtractedMetadata = async (suggestions: IXSuggestionType[]) => {
};
}

return files.save(file);
await files.save(file);
});
};

Expand Down
193 changes: 193 additions & 0 deletions app/api/sync/specs/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,197 @@ const host2Fixtures: DBFixture = {
],
};

const orderedHostIds = {
settings: db.id(),
translationsV2: db.id(),
dictionaries: db.id(),
relationtypes: db.id(),
templates: db.id(),
prop: db.id(),
files: db.id(),
connection1: db.id(),
connection2: db.id(),
hub: db.id(),
entity1: db.id(),
entity2: db.id(),
syncs: db.id(),
updatelogs: db.id(),
};
const orderedHostFixtures: DBFixture = {
settings: [
{
_id: orderedHostIds.settings,
languages: [{ key: 'en' as 'en', default: true, label: 'en' }],
sync: [
{
url: 'http://localhost:6667',
name: 'target1',
active: true,
username: 'user',
password: 'password',
config: {
templates: {
[orderedHostIds.templates.toString()]: {
properties: [orderedHostIds.prop.toString()],
attachments: true,
},
},
relationtypes: [orderedHostIds.relationtypes.toString()],
},
},
],
},
],
translationsV2: [
{
_id: orderedHostIds.translationsV2,
context: {
type: 'Uwazi UI' as 'Uwazi UI',
label: 'User Interface',
id: 'System',
},
key: 'Search',
language: 'en' as 'en',
value: 'Search',
},
],
dictionaries: [
{
_id: orderedHostIds.dictionaries,
name: 'dict',
values: [
{
label: 'a',
},
],
},
],
relationtypes: [{ _id: orderedHostIds.relationtypes, name: 'reltype' }],
templates: [
{
_id: orderedHostIds.templates,
name: 'template1',
properties: [
{
_id: orderedHostIds.prop,
name: 'prop',
label: 'prop',
type: 'select' as 'select',
content: orderedHostIds.dictionaries.toString(),
},
],
},
],
files: [
{
_id: orderedHostIds.files,
entity: 'entity1',
type: 'attachment' as 'attachment',
filename: 'test.txt',
},
],
connections: [
{
_id: orderedHostIds.connection1,
entity: 'entity1',
hub: orderedHostIds.hub,
template: orderedHostIds.relationtypes,
},
{
_id: orderedHostIds.connection2,
entity: 'entity2',
hub: orderedHostIds.hub,
template: orderedHostIds.relationtypes,
},
],
entities: [
{
_id: orderedHostIds.entity1,
language: 'en' as 'en',
sharedId: 'entity1',
title: '1',
template: orderedHostIds.templates,
metadata: {},
},
{
_id: orderedHostIds.entity2,
language: 'en' as 'en',
sharedId: 'entity2',
title: '2',
template: orderedHostIds.templates,
metadata: {},
},
],
syncs: [
{
lastSync: 0,
name: 'target1',
},
],
updatelogs: [
{
timestamp: 1000,
namespace: 'settings',
mongoId: orderedHostIds.settings,
deleted: false,
},
{
timestamp: 700,
namespace: 'translationsV2',
mongoId: orderedHostIds.translationsV2,
deleted: false,
},
{
timestamp: 600,
namespace: 'dictionaries',
mongoId: orderedHostIds.dictionaries,
deleted: false,
},
{
timestamp: 500,
namespace: 'relationtypes',
mongoId: orderedHostIds.relationtypes,
deleted: false,
},
{
timestamp: 40,
namespace: 'templates',
mongoId: orderedHostIds.templates,
deleted: false,
},
{
timestamp: 30,
namespace: 'files',
mongoId: orderedHostIds.files,
deleted: false,
},
{
timestamp: 20,
namespace: 'connections',
mongoId: orderedHostIds.connection1,
deleted: false,
},
{
timestamp: 20,
namespace: 'connections',
mongoId: orderedHostIds.connection2,
deleted: false,
},
{
timestamp: 1,
namespace: 'entities',
mongoId: orderedHostIds.entity1,
deleted: false,
},
{
timestamp: 1,
namespace: 'entities',
mongoId: orderedHostIds.entity2,
deleted: false,
},
],
};

export {
host1Fixtures,
host2Fixtures,
Expand All @@ -887,6 +1078,8 @@ export {
thesauri1Value2,
newDoc1,
newDoc3,
orderedHostFixtures,
orderedHostIds,
relationtype4,
relationship9,
hub3,
Expand Down
Loading

0 comments on commit a718f2e

Please sign in to comment.