Skip to content
This repository was archived by the owner on May 28, 2023. It is now read-only.

Commit 08ad0d7

Browse files
author
tkostuch
committed
merge db with db7 script
1 parent 205e171 commit 08ad0d7

File tree

3 files changed

+146
-148
lines changed

3 files changed

+146
-148
lines changed

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
1616
"build:copy:schema": "cpx 'src/**/*.schema*.json' dist/src",
1717
"start": "pm2 start ecosystem.json $PM2_ARGS",
1818
"db": "node scripts/db.js",
19-
"db7": "node scripts/db7.js",
2019
"cache": "node ./scripts/cache",
2120
"mage2vs": "node scripts/mage2vs.js",
2221
"restore": "node scripts/elastic.js restore && npm run db rebuild",
23-
"restore7": "node scripts/elastic7.js restore --output-index=vue_storefront_catalog && npm run db7 rebuild --indexName=vue_storefront_catalog",
24-
"restore7_it": "npm run restore7 -- --input-file=var/catalog_it.json --output-index=vue_storefront_catalog_it && npm run db7 rebuild -- --indexName=vue_storefront_catalog_it",
25-
"restore7_de": "npm run restore7 -- --input-file=var/catalog_de.json --output-index=vue_storefront_catalog_de && npm run db7 rebuild -- --indexName=vue_storefront_catalog_de",
22+
"restore7": "node scripts/elastic7.js restore --output-index=vue_storefront_catalog && npm run db rebuild --indexName=vue_storefront_catalog",
23+
"restore7_it": "npm run restore7 -- --input-file=var/catalog_it.json --output-index=vue_storefront_catalog_it && npm run db rebuild -- --indexName=vue_storefront_catalog_it",
24+
"restore7_de": "npm run restore7 -- --input-file=var/catalog_de.json --output-index=vue_storefront_catalog_de && npm run db rebuild -- --indexName=vue_storefront_catalog_de",
2625
"restore_it": "npm run restore -- --input-file=var/catalog_it.json --output-index=vue_storefront_catalog_it && npm run db rebuild -- --indexName=vue_storefront_catalog_it",
2726
"restore_de": "npm run restore -- --input-file=var/catalog_de.json --output-index=vue_storefront_catalog_de && npm run db rebuild -- --indexName=vue_storefront_catalog_de",
2827
"restore2main": "npm run restore -- --output-index=vue_storefront_catalog",

scripts/db.js

Lines changed: 143 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,75 +3,178 @@ const config = require('config')
33
const common = require('../migrations/.common')
44
const es = require('../src/lib/elastic')
55

6-
program
7-
.command('rebuild')
8-
.option('-i|--indexName <indexName>', 'name of the Elasticsearch index', config.elasticsearch.indices[0])
9-
.action((cmd) => { // TODO: add parallel processing
10-
if (!cmd.indexName) {
11-
console.error('error: indexName must be specified');
12-
process.exit(1);
13-
}
6+
/**
7+
* REBUILD DB
8+
*/
9+
const es7RebuildCommand = (cmd) => { // TODO: add parallel processing
10+
if (!cmd.indexName) {
11+
console.error('error: indexName must be specified');
12+
process.exit(1);
13+
}
1414

15+
let waitingCounter = 0
16+
for (var indexTypeIterator in config.elasticsearch.indexTypes) {
17+
var collectionName = config.elasticsearch.indexTypes[indexTypeIterator]
18+
console.log(config.elasticsearch.indexTypes);
1519
console.log('** Hello! I am going to rebuild EXISTING ES index to fix the schema')
16-
const originalIndex = cmd.indexName
20+
const originalIndex = cmd.indexName + '_' + collectionName;
1721
const tempIndex = originalIndex + '_' + Math.round(+new Date() / 1000)
1822

1923
console.log(`** Creating temporary index ${tempIndex}`)
20-
es.createIndex(common.db, tempIndex, '', (err) => {
24+
es.createIndex(common.db, tempIndex, collectionName, (err) => {
2125
if (err) {
2226
console.log(err)
2327
}
2428

25-
console.log(`** Putting the mappings on top of ${tempIndex}`)
26-
es.putMappings(common.db, tempIndex, (err) => {
29+
console.log(`** We will reindex ${originalIndex} with the current schema`)
30+
es.reIndex(common.db, originalIndex, tempIndex, (err) => {
2731
if (err) {
28-
console.error(err.meta ? err.meta : err)
32+
console.log(err)
2933
}
3034

31-
console.log(`** We will reindex ${originalIndex} with the current schema`)
32-
es.reIndex(common.db, originalIndex, tempIndex, (err) => {
35+
console.log('** Removing the original index')
36+
es.deleteIndex(common.db, originalIndex, (err) => {
3337
if (err) {
3438
console.log(err)
3539
}
3640

37-
console.log('** Removing the original index')
38-
es.deleteIndex(common.db, originalIndex, (err) => {
39-
if (err) {
40-
console.log(err)
41-
}
42-
43-
console.log('** Creating alias')
44-
es.putAlias(common.db, tempIndex, originalIndex, (err) => {
45-
console.log('Done! Bye!')
46-
process.exit(0)
47-
})
41+
console.log('** Creating alias')
42+
es.putAlias(common.db, tempIndex, originalIndex, (err) => {
43+
waitingCounter++
4844
})
4945
})
5046
})
5147
})
52-
})
48+
}
49+
setInterval(() => {
50+
if (waitingCounter === config.elasticsearch.indexTypes.length) process.exit(0)
51+
}, 1000)
52+
}
5353

54-
program
55-
.command('new')
56-
.option('-i|--indexName <indexName>', 'name of the Elasticsearch index', config.elasticsearch.indices[0])
57-
.action((cmd) => { // TODO: add parallel processing
58-
if (!cmd.indexName) {
59-
console.error('error: indexName must be specified');
60-
process.exit(1);
54+
const es5RebuildCommand = (cmd) => { // TODO: add parallel processing
55+
if (!cmd.indexName) {
56+
console.error('error: indexName must be specified');
57+
process.exit(1);
58+
}
59+
60+
console.log('** Hello! I am going to rebuild EXISTING ES index to fix the schema')
61+
const originalIndex = cmd.indexName
62+
const tempIndex = originalIndex + '_' + Math.round(+new Date() / 1000)
63+
64+
console.log(`** Creating temporary index ${tempIndex}`)
65+
es.createIndex(common.db, tempIndex, '', (err) => {
66+
if (err) {
67+
console.log(err)
6168
}
6269

63-
console.log('** Hello! I am going to create NEW ES index')
64-
const indexName = cmd.indexName
65-
es.createIndex(common.db, indexName, '', (err) => {
70+
console.log(`** Putting the mappings on top of ${tempIndex}`)
71+
es.putMappings(common.db, tempIndex, (err) => {
6672
if (err) {
67-
console.log(err)
73+
console.error(err.meta ? err.meta : err)
6874
}
6975

70-
console.log('Done! Bye!')
71-
process.exit(0)
76+
console.log(`** We will reindex ${originalIndex} with the current schema`)
77+
es.reIndex(common.db, originalIndex, tempIndex, (err) => {
78+
if (err) {
79+
console.log(err)
80+
}
81+
82+
console.log('** Removing the original index')
83+
es.deleteIndex(common.db, originalIndex, (err) => {
84+
if (err) {
85+
console.log(err)
86+
}
87+
88+
console.log('** Creating alias')
89+
es.putAlias(common.db, tempIndex, originalIndex, (err) => {
90+
console.log('Done! Bye!')
91+
process.exit(0)
92+
})
93+
})
94+
})
7295
})
7396
})
97+
}
98+
99+
const rebuildCommandFactory = (cmd) => {
100+
if (parseInt(config.elasticsearch.apiVersion) < 6) {
101+
return es5RebuildCommand(cmd)
102+
} else {
103+
return es7RebuildCommand(cmd)
104+
}
105+
}
106+
107+
program
108+
.command('rebuild')
109+
.option('-i|--indexName <indexName>', 'name of the Elasticsearch index', config.elasticsearch.indices[0])
110+
.action(rebuildCommandFactory)
111+
112+
/**
113+
* CREATE INDEX
114+
*/
115+
const asyncCreateIndex = (es, common, indexName, collectionName) => new Promise((resolve, reject) => {
116+
es.createIndex(common.db, indexName + '_' + collectionName, collectionName, (err) => {
117+
if (err) {
118+
reject(err)
119+
} else {
120+
resolve()
121+
}
122+
})
123+
})
124+
125+
const es7NewCommand = async (cmd) => { // TODO: add parallel processing
126+
if (!cmd.indexName) {
127+
console.error('error: indexName must be specified');
128+
process.exit(1);
129+
}
130+
131+
console.log('** Hello! I am going to create NEW ES index')
132+
const indexName = cmd.indexName
133+
134+
for (let collectionName of config.elasticsearch.indexTypes) {
135+
try {
136+
await asyncCreateIndex(es, common, indexName, collectionName)
137+
} catch (err) {
138+
console.log(JSON.stringify(err, null, 2))
139+
}
140+
}
141+
process.exit(0)
142+
}
143+
144+
const es5NewCommand = (cmd) => { // TODO: add parallel processing
145+
if (!cmd.indexName) {
146+
console.error('error: indexName must be specified');
147+
process.exit(1);
148+
}
149+
150+
console.log('** Hello! I am going to create NEW ES index')
151+
const indexName = cmd.indexName
152+
es.createIndex(common.db, indexName, '', (err) => {
153+
if (err) {
154+
console.log(err)
155+
}
156+
157+
console.log('Done! Bye!')
158+
process.exit(0)
159+
})
160+
}
161+
162+
const newCommandFactory = (cmd) => {
163+
if (parseInt(config.elasticsearch.apiVersion) < 6) {
164+
return es5NewCommand(cmd)
165+
} else {
166+
return es7NewCommand(cmd)
167+
}
168+
}
169+
170+
program
171+
.command('new')
172+
.option('-i|--indexName <indexName>', 'name of the Elasticsearch index', config.elasticsearch.indices[0])
173+
.action(newCommandFactory)
74174

175+
/**
176+
* OTHERS COMMANDS
177+
*/
75178
program
76179
.on('command:*', () => {
77180
console.error('Invalid command: %s\nSee --help for a list of available commands.', program.args.join(' '));

scripts/db7.js

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

0 commit comments

Comments
 (0)