-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/development'
- Loading branch information
Showing
11 changed files
with
239 additions
and
22 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
app/api/migrations/migrations/22-files-to-updatelogs/index.js
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,41 @@ | ||
/* eslint-disable no-await-in-loop, max-statements */ | ||
export default { | ||
delta: 22, | ||
|
||
name: 'files-to-updatelogs', | ||
|
||
description: 'update previously migrated files to updatelogs', | ||
|
||
async up(db) { | ||
process.stdout.write(`${this.name}...\r\n`); | ||
const cursor = db.collection('files').find({}); | ||
|
||
let index = 1; | ||
|
||
const [sync] = await db | ||
.collection('syncs') | ||
.find() | ||
.toArray(); | ||
|
||
while (await cursor.hasNext()) { | ||
const file = await cursor.next(); | ||
const logExists = ( | ||
await db | ||
.collection('updatelogs') | ||
.find({ mongoId: file._id }) | ||
.toArray() | ||
).length; | ||
|
||
if (!logExists) { | ||
await db.collection('updatelogs').insertOne({ | ||
timestamp: sync ? sync.lastSync : 0, | ||
namespace: 'files', | ||
mongoId: file._id, | ||
deleted: false, | ||
}); | ||
} | ||
process.stdout.write(` -> processed: ${index} \r`); | ||
index += 1; | ||
} | ||
}, | ||
}; |
78 changes: 78 additions & 0 deletions
78
app/api/migrations/migrations/22-files-to-updatelogs/specs/22-files-to-updatelogs.spec.js
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,78 @@ | ||
import testingDB from 'api/utils/testing_db'; | ||
import migration from '../index.js'; | ||
import fixtures, { file1, file2, file3 } from './fixtures.js'; | ||
|
||
const query = (collectionName, queryObject = {}, select = {}) => | ||
testingDB.mongodb | ||
.collection(collectionName) | ||
.find(queryObject, select) | ||
.toArray(); | ||
|
||
describe('migration files-to-updatelogs', () => { | ||
beforeEach(async () => { | ||
spyOn(process.stdout, 'write'); | ||
await testingDB.clearAllAndLoad(fixtures); | ||
}); | ||
|
||
afterAll(async () => { | ||
await testingDB.disconnect(); | ||
}); | ||
|
||
it('should have a delta number', () => { | ||
expect(migration.delta).toBe(22); | ||
}); | ||
|
||
it('should create entries on updatelogs for all files', async () => { | ||
await migration.up(testingDB.mongodb); | ||
const fileLogs = await query('updatelogs'); | ||
|
||
expect(fileLogs.length).toBe(3); | ||
expect(fileLogs).toEqual([ | ||
expect.objectContaining({ | ||
timestamp: 50, | ||
namespace: 'files', | ||
mongoId: file3, | ||
deleted: false, | ||
}), | ||
expect.objectContaining({ | ||
timestamp: 0, | ||
namespace: 'files', | ||
mongoId: file1, | ||
deleted: false, | ||
}), | ||
expect.objectContaining({ | ||
timestamp: 0, | ||
namespace: 'files', | ||
mongoId: file2, | ||
deleted: false, | ||
}), | ||
]); | ||
}); | ||
|
||
describe('when it has lastSync', () => { | ||
it('should use lastSync as timestamp', async () => { | ||
await testingDB.mongodb.collection('syncs').insertOne({ | ||
lastSync: 20, | ||
}); | ||
|
||
await migration.up(testingDB.mongodb); | ||
const [file3log, file1log, file2log] = await query('updatelogs'); | ||
|
||
expect(file3log).toEqual( | ||
expect.objectContaining({ | ||
timestamp: 50, | ||
}) | ||
); | ||
expect(file1log).toEqual( | ||
expect.objectContaining({ | ||
timestamp: 20, | ||
}) | ||
); | ||
expect(file2log).toEqual( | ||
expect.objectContaining({ | ||
timestamp: 20, | ||
}) | ||
); | ||
}); | ||
}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
app/api/migrations/migrations/22-files-to-updatelogs/specs/fixtures.js
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,28 @@ | ||
import db from 'api/utils/testing_db'; | ||
|
||
const [file1, file2, file3] = [db.id(), db.id(), db.id()]; | ||
|
||
export default { | ||
files: [ | ||
{ | ||
_id: file1, | ||
}, | ||
{ | ||
_id: file2, | ||
}, | ||
{ | ||
_id: file3, | ||
}, | ||
], | ||
updatelogs: [ | ||
{ | ||
_id: db.id(), | ||
timestamp: 50, | ||
namespace: 'files', | ||
mongoId: file3, | ||
deleted: false, | ||
}, | ||
], | ||
}; | ||
|
||
export { file1, file2, file3 }; |
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,12 @@ | ||
export default { | ||
delta: 23, | ||
|
||
name: 'fix_udaptelogs', | ||
|
||
description: 'delete update logs without mongoId', | ||
|
||
async up(db) { | ||
process.stdout.write(`${this.name}...\r\n`); | ||
await db.collection('updatelogs').removeMany({ mongoId: { $exists: false } }); | ||
}, | ||
}; |
37 changes: 37 additions & 0 deletions
37
app/api/migrations/migrations/23-fix_udaptelogs/specs/23-fix_udaptelogs.spec.js
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,37 @@ | ||
import testingDB from 'api/utils/testing_db'; | ||
import migration from '../index.js'; | ||
import fixtures from './fixtures.js'; | ||
|
||
describe('migration fix_udaptelogs', () => { | ||
beforeEach(async () => { | ||
spyOn(process.stdout, 'write'); | ||
await testingDB.clearAllAndLoad(fixtures); | ||
}); | ||
|
||
afterAll(async () => { | ||
await testingDB.disconnect(); | ||
}); | ||
|
||
it('should have a delta number', () => { | ||
expect(migration.delta).toBe(23); | ||
}); | ||
|
||
it('should remove all updatelogs without mongoId', async () => { | ||
await migration.up(testingDB.mongodb); | ||
const updatelogs = await testingDB.mongodb | ||
.collection('updatelogs') | ||
.find() | ||
.toArray(); | ||
|
||
expect(updatelogs).toEqual([ | ||
expect.objectContaining({ | ||
mongoId: expect.anything(), | ||
namespace: 'entities', | ||
}), | ||
expect.objectContaining({ | ||
mongoId: expect.anything(), | ||
namespace: 'entities', | ||
}), | ||
]); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
app/api/migrations/migrations/23-fix_udaptelogs/specs/fixtures.js
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,20 @@ | ||
import db from 'api/utils/testing_db'; | ||
|
||
export default { | ||
updatelogs: [ | ||
{ | ||
mongoId: db.id(), | ||
namespace: 'entities', | ||
}, | ||
{ | ||
mongoId: db.id(), | ||
namespace: 'entities', | ||
}, | ||
{ | ||
namespace: 'entities', | ||
}, | ||
{ | ||
namespace: 'entities', | ||
}, | ||
], | ||
}; |
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 |
---|---|---|
@@ -1,23 +1,22 @@ | ||
import { catchErrors } from 'api/utils/jasmineHelpers'; | ||
import testingDB from 'api/utils/testing_db'; | ||
import migration from '../index.js'; | ||
import fixtures from './fixtures.js'; | ||
|
||
describe('migration {{ name }}', () => { | ||
beforeEach((done) => { | ||
beforeEach(async () => { | ||
spyOn(process.stdout, 'write'); | ||
testingDB.clearAllAndLoad(fixtures).then(done).catch(catchErrors(done)); | ||
await testingDB.clearAllAndLoad(fixtures); | ||
}); | ||
|
||
afterAll((done) => { | ||
testingDB.disconnect().then(done); | ||
afterAll(async () => { | ||
await testingDB.disconnect(); | ||
}); | ||
|
||
it('should have a delta number', () => { | ||
expect(migration.delta).toBe({{nextMigrationDelta}}); | ||
}); | ||
|
||
it('should fail', (done) => { | ||
migration.up().catch(catchErrors(done)); | ||
it('should fail', async () => { | ||
await migration.up(); | ||
}); | ||
}); |
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
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
/** @format */ | ||
|
||
import mongoose from 'mongoose'; | ||
|
||
const updateLogSchema = new mongoose.Schema({ | ||
|
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