Skip to content

Commit e6e42ae

Browse files
committed
chore: es-db-compare script code structure enhancements
- added verification scripts and guides to the repo - moved README to the script folder - README and Verification guide improvements - ignore paths configured for production use
1 parent dc05e6c commit e6e42ae

15 files changed

+171238
-55
lines changed

docs/es-db-compare.md

Lines changed: 0 additions & 21 deletions
This file was deleted.

scripts/es-db-compare/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Script to find mismatches between data in DB and ES
2+
3+
We keep all the data in two places in DB (Database) and in ES (Elasticsearch Index). Every time we make any changes to the data in the DB all the changes are also reflected in ElasticSearch. Due to some circumstances data in ES and DB can become inconsistent.
4+
5+
This script may be run to find all the inconsistencies between data we have in ES and DB and create a report.
6+
7+
## Configuration
8+
9+
The following properties can be set from env variables:
10+
11+
- `PROJECT_START_ID`: if set, only projects with id that large than or equal to the value are compared.
12+
- `PROJECT_END_ID`: if set, only projects with id that less than or equal to the value are compared.
13+
- `PROJECT_LAST_ACTIVITY_AT`: if set, only projects with property lastActivityAt that large than or equal to the value are compared.
14+
15+
There could be some fields that always mismatch in ES and DB.
16+
The variable named `ignoredPaths` at `scripts/es-db-compare/constants.js` maintains a list of json paths which will be ignored
17+
during the comparation. You may need to modify/add/delete items in the list.
18+
19+
### Required
20+
21+
- `PROJECT_START_ID` and `PROJECT_END_ID` must exist together.
22+
- At least one of `PROJECT_START_ID` with `PROJECT_END_ID` or `PROJECT_LAST_ACTIVITY_AT` needs be set before running the script.
23+
24+
## Usage
25+
26+
Set up configuration and execute command `npm run es-db-compare` on the command line.
27+
It will then generate a HTML report with name `report.html` under the current directory.
28+
29+
Example commands:
30+
31+
- Generate a report comparing ALL the projects:
32+
33+
```bash
34+
PROJECT_LAST_ACTIVITY_AT=0 npm run es-db-compare
35+
```
36+
37+
- Generate a report comparing projects that have been updated on **26 December 2019** or later:
38+
39+
```bash
40+
PROJECT_LAST_ACTIVITY_AT="2019-12-26" npm run es-db-compare
41+
```
42+
43+
- Generate a report comparing projects with ID range:
44+
45+
```bash
46+
PROJECT_START_ID=5000 PROJECT_END_ID=6000 npm run es-db-compare
47+
```

scripts/es-db-compare/compareProjects.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,7 @@
1111

1212
const lodash = require('lodash');
1313
const scriptUtil = require('./util');
14-
15-
const associations = {
16-
phases: 'Phase',
17-
members: 'Member',
18-
invites: 'Invite',
19-
attachments: 'Attachment',
20-
timelines: 'Timeline',
21-
};
14+
const scriptConstants = require('./constants');
2215

2316
/**
2417
* Process diff delta to extract project-related data.
@@ -241,8 +234,10 @@ function processDelta(delta, dbData, esData, finalData) {
241234
}
242235
};
243236

244-
if (delta.path.length > 2 && associations[delta.path[1]]) {
245-
return processAssociation(delta, { modelName: associations[delta.path[1]], refPath: delta.path[1] });
237+
if (delta.path.length > 2 && scriptConstants.associations.projects[delta.path[1]]) {
238+
return processAssociation(delta, {
239+
modelName: scriptConstants.associations.projects[delta.path[1]], refPath: delta.path[1],
240+
});
246241
}
247242
if (delta.dataType === 'array' && delta.path.length === 1) {
248243
if (delta.type === 'delete') {

scripts/es-db-compare/constants.js

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,16 @@ module.exports = {
66
// currently support only a subset of jsonpath notations
77
// "*" means any index number
88
ignoredPaths: [
9-
'project.projectUrl',
10-
'project.utm',
11-
'metadata.milestoneTemplates.order',
9+
// all project updatedAt (good to set for local verification)
10+
// 'project.updatedAt',
11+
// 'project.phases[*].updatedAt',
12+
// 'project.phases[*].products[*].updatedAt',
13+
// 'project.phases[*].products[*].timeline.updatedAt',
14+
// 'project.phases[*].products[*].timeline.milestones[*].updatedAt',
15+
// 'project.invites[*].updatedAt',
16+
// 'project.members[*].updatedAt',
17+
// 'project.attachments[*].updatedAt',
1218

13-
// all project updatedAt
14-
'project.updatedAt',
15-
'project.phases[*].updatedAt',
16-
'project.phases[*].products[*].updatedAt',
17-
'project.phases[*].products[*].timeline.updatedAt',
18-
'project.phases[*].products[*].timeline.milestones[*].updatedAt',
19-
'project.invites[*].updatedAt',
20-
'project.members[*].updatedAt',
21-
'project.attachments[*].updatedAt',
2219
// all project deletedAt
2320
'project.deletedAt',
2421
'project.phases[*].deletedAt',
@@ -28,6 +25,7 @@ module.exports = {
2825
'project.invites[*].deletedAt',
2926
'project.members[*].deletedAt',
3027
'project.attachments[*].deletedAt',
28+
3129
// all project deletedBy
3230
'project.deletedBy',
3331
'project.phases[*].deletedBy',
@@ -38,17 +36,18 @@ module.exports = {
3836
'project.members[*].deletedBy',
3937
'project.attachments[*].deletedBy',
4038

41-
// all metadata updatedAt
42-
'metadata.projectTemplates.updatedAt',
43-
'metadata.productTemplates.updatedAt',
44-
'metadata.projectTypes.updatedAt',
45-
'metadata.productCategories.updatedAt',
46-
'metadata.milestoneTemplates.updatedAt',
47-
'metadata.orgConfigs.updatedAt',
48-
'metadata.forms.updatedAt',
49-
'metadata.planConfigs.updatedAt',
50-
'metadata.priceConfigs.updatedAt',
51-
'metadata.buildingBlocks.updatedAt',
39+
// all metadata updatedAt (good to set for local verification)
40+
// 'metadata.projectTemplates.updatedAt',
41+
// 'metadata.productTemplates.updatedAt',
42+
// 'metadata.projectTypes.updatedAt',
43+
// 'metadata.productCategories.updatedAt',
44+
// 'metadata.milestoneTemplates.updatedAt',
45+
// 'metadata.orgConfigs.updatedAt',
46+
// 'metadata.forms.updatedAt',
47+
// 'metadata.planConfigs.updatedAt',
48+
// 'metadata.priceConfigs.updatedAt',
49+
// 'metadata.buildingBlocks.updatedAt',
50+
5251
// all metadata deletedAt
5352
'metadata.projectTemplates.deletedAt',
5453
'metadata.productTemplates.deletedAt',
@@ -60,6 +59,7 @@ module.exports = {
6059
'metadata.planConfigs.deletedAt',
6160
'metadata.priceConfigs.deletedAt',
6261
'metadata.buildingBlocks.deletedAt',
62+
6363
// all metadata deletedBy
6464
'metadata.projectTemplates.deletedBy',
6565
'metadata.productTemplates.deletedBy',
@@ -85,5 +85,13 @@ module.exports = {
8585
priceConfigs: 'PriceConfig',
8686
buildingBlocks: 'BuildingBlock',
8787
},
88+
89+
projects: {
90+
phases: 'Phase',
91+
members: 'Member',
92+
invites: 'Invite',
93+
attachments: 'Attachment',
94+
timelines: 'Timeline',
95+
},
8896
},
8997
};

scripts/es-db-compare/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function getTemplate() {
120120
}
121121
return 'unknown';
122122
});
123-
const template = handlebars.compile(fs.readFileSync(path.join(__dirname, 'report.mustache')).toString());
123+
const template = handlebars.compile(fs.readFileSync(path.join(__dirname, 'report.handlebars')).toString());
124124
return template;
125125
}
126126

0 commit comments

Comments
 (0)