-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
2,612 additions
and
1,806 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
app/api/migrations/migrations/150-per_namespace_lastSyncs/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}, | ||
}; |
77 changes: 77 additions & 0 deletions
77
...grations/migrations/150-per_namespace_lastSyncs/specs/150-per_namespace_lastSyncs.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
17 changes: 17 additions & 0 deletions
17
app/api/migrations/migrations/150-per_namespace_lastSyncs/specs/fixtures.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
app/api/migrations/migrations/150-per_namespace_lastSyncs/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.