Skip to content

Commit

Permalink
[131] update mongodb validation for the case schema to allow search (#…
Browse files Browse the repository at this point in the history
…136)

* Update libraries batch-2 first part

* Move some testing to TODO

* Update Yup when

* update set field values

* Update breaking changes

* Update comment

* Remove unused

* update datepicker

* Update libraries batch-2 second part

* Update return values of the functions for countries

* update typescript

* Update mismatched versions

* WIP update router and eslint + vite instead of cra

* Update tests for new vitest library

* Update command for test and coverage

* Skip tests that were blocking flow

* Update Location.test.tsx

* Update varaible name

* Fix merging issues

* Update index.tsx

* Update index.tsx

* WIP tests for location and landing page

* Fixed all landing page tests

* Unskip and fix verification status indicator tests

* Fix automated source form tests

* WIP unmocking EditCase tests

* Update EditCase.test.tsx

* Fix viewcase tests

* Resolve warnings regarding act

* Update Dockerfile

* Update Location.test.tsx

* Fix tests failing at the first day of the month

* Cleanup sourcetable

* Update Users Uploads and Sources tables

* Add backfill back

It was removed after splitting one component into multiple

* Rename collection from cases to day0cases

* Update day0cases.schema.json

* Update day0cases.schema.json

* Initial schema work

* initial schema work

* Fix location lat/long update issue

* Add missing schema element - isGovernmentSource for Additional Sources

* Update tests for searchbar and data guide

* Update README.md
  • Loading branch information
stanislaw-zakrzewski authored Oct 17, 2024
1 parent 25a86d5 commit 929ea2a
Show file tree
Hide file tree
Showing 11 changed files with 348 additions and 91 deletions.
7 changes: 7 additions & 0 deletions data-serving/data-service/src/controllers/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,13 @@ export class CasesController {
res.status(201).json(result);
} catch (e) {
const err = e as Error;
if (err.name === 'MongoServerError') {
logger.error((e as any).errInfo);
res.status(422).json({
message: (err as any).errInfo,
});
return;
}
if (err instanceof GeocodeNotFoundError) {
res.status(404).json({
message: err.message,
Expand Down
99 changes: 51 additions & 48 deletions data-serving/scripts/setup-db/migrations/20210902121948-initial.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,60 @@
const fs = require('fs');

module.exports = {
async up(db, client) {
await createCollectionValidationAndIndexes(
db,
'cases',
'schemas/cases.schema.json',
'schemas/cases.indexes.json'
);

await createCollectionValidationAndIndexes(
db,
'sources',
'schemas/sources.schema.json',
'schemas/sources.indexes.json'
);
},

async down(db, client) {
// initial migration has no rollback
}
async up(db, client) {
await createCollectionValidationAndIndexes(
db,
'day0cases',
'schemas/day0cases.schema.json',
'schemas/day0cases.indexes.json',
);

await createCollectionValidationAndIndexes(
db,
'sources',
'schemas/sources.schema.json',
'schemas/sources.indexes.json',
);
},

async down(db, client) {
// initial migration has no rollback
},
};

async function createCollectionValidationAndIndexes(db, collectionName, schemaPath, indexPath) {
const schemaFile = await fs.promises.readFile(schemaPath);
const schema = JSON.parse(schemaFile);
async function createCollectionValidationAndIndexes(
db,
collectionName,
schemaPath,
indexPath,
) {
const schemaFile = await fs.promises.readFile(schemaPath);
const schema = JSON.parse(schemaFile);

const indexFile = await fs.promises.readFile(indexPath);
const indexes = JSON.parse(indexFile);
/*
* because this migration might run against a DB from before we had the migrations infra,
* check whether the collection already exists. If it does, then modify its validation schema.
* If it doesn't, then create it.
*/
try {
await db.collection(collectionName);
await db.command({
collMod: collectionName,
validator: schema,
});
} catch {
await db.createCollection(collectionName, {
validator: schema,
});
}

const collection = db.collection(collectionName);
await collection.dropIndexes();

const indexFile = await fs.promises.readFile(indexPath);
const indexes = JSON.parse(indexFile);
/*
* because this migration might run against a DB from before we had the migrations infra,
* check whether the collection already exists. If it does, then modify its validation schema.
* If it doesn't, then create it.
*/
try {
await db.collection(collectionName);
await db.command({
collMod: collectionName,
validator: schema,
});
}
catch {
await db.createCollection(collectionName, {
validator: schema,
createIndexes: collectionName,
indexes: indexes,
});
}

const collection = db.collection(collectionName);
await collection.dropIndexes();

await db.command({
createIndexes: collectionName,
indexes: indexes,
});
}

Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
const indexes = [
{
name: 'byGenderAndCountry',
key: {
'demographics.gender': -1,
'location.countryISO3': -1
{
name: 'byGenderAndCountry',
key: {
'demographics.gender': -1,
'location.countryISO3': -1,
},
collation: {
locale: 'en_US',
strength: 2,
},
},
collation: {
locale: 'en_US',
strength: 2,
},
}
];

module.exports = {
async up(db, client) {
await db.command({
createIndexes: 'cases',
indexes: indexes,
});
},
async up(db, client) {
await db.command({
createIndexes: 'day0cases',
indexes: indexes,
});
},

async down(db, client) {
await db.command({
dropIndexes: 'cases',
index: ['byGenderAndCountry']
});
}
async down(db, client) {
await db.command({
dropIndexes: 'day0cases',
index: ['byGenderAndCountry'],
});
},
};
1 change: 1 addition & 0 deletions data-serving/scripts/setup-db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"lint": "tsc --noEmit && eslint '*/**/*.{js,ts,tsx}' --quiet --fix",
"import-sample-data": "python3 ./import-sample-data.py",
"migrate": "npm ci && migrate-mongo up",
"migrate-down": "npm ci && migrate-mongo down",
"delete-all-cases": "mongosh $CONN --eval 'db.cases.deleteMany({})'"
},
"repository": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"key": {
"demographics.occupation": "text",
"location.country": "text",
"location.city": "text",
"location.admin1": "text",
"location.admin2": "text",
"location.admin3": "text",
"caseReference.sourceUrl": "text",
"caseStatus": "text"
}
Expand Down Expand Up @@ -74,9 +76,29 @@
}
},
{
"name": "locationCityIdx",
"name": "locationAdmin1Idx",
"key": {
"location.city": -1
"location.admin1": -1
},
"collation": {
"locale": "en_US",
"strength": 2
}
},
{
"name": "locationAdmin2Idx",
"key": {
"location.admin2": -1
},
"collation": {
"locale": "en_US",
"strength": 2
}
},
{
"name": "locationAdmin3Idx",
"key": {
"location.admin3": -1
},
"collation": {
"locale": "en_US",
Expand Down
Loading

0 comments on commit 929ea2a

Please sign in to comment.