Skip to content

Commit de598c4

Browse files
committed
remove php configuration from install php task, move it to start task itself; introduce import-db command and import-db option in start command;
1 parent 8381229 commit de598c4

File tree

13 files changed

+196
-10
lines changed

13 files changed

+196
-10
lines changed

build-packages/magento-scripts/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const commands = [
1414
require('./lib/commands/stop'),
1515
require('./lib/commands/cleanup'),
1616
require('./lib/commands/status'),
17-
require('./lib/commands/execute')
17+
require('./lib/commands/execute'),
18+
require('./lib/commands/import-db')
1819
];
1920

2021
process.title = 'magento-scripts';
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* eslint-disable no-param-reassign */
2+
// const path = require('path');
3+
const logger = require('@scandipwa/scandipwa-dev-utils/logger');
4+
const { Listr } = require('listr2');
5+
const importDump = require('../tasks/import-dump');
6+
7+
module.exports = (yargs) => {
8+
yargs.command(
9+
'import-db <importDb>',
10+
'Import database dump to MySQL',
11+
() => {},
12+
async (args = {}) => {
13+
const tasks = new Listr([importDump], {
14+
exitOnError: true,
15+
ctx: args,
16+
concurrent: false,
17+
rendererOptions: { collapse: false }
18+
});
19+
20+
try {
21+
await tasks.run();
22+
23+
logger.logN();
24+
logger.warn(`If you had theme setup you might want to re-link theme by running command ${ logger.style.command('link') }!`);
25+
logger.warn(`Also, it's recommended to run ${ logger.style.command('start') } properly configure setup!`);
26+
process.exit(0);
27+
} catch (e) {
28+
logger.error(e.message || e);
29+
process.exit(1);
30+
}
31+
}
32+
);
33+
};

build-packages/magento-scripts/lib/commands/start.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ module.exports = (yargs) => {
4747
default: false
4848
}
4949
);
50+
51+
yargs.option(
52+
'import-db', {
53+
describe: 'Import database dump to MySQL',
54+
type: 'string'
55+
// normalize: true
56+
}
57+
);
5058
}, async (args = {}) => {
5159
const tasks = new Listr([start], {
5260
exitOnError: true,
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* eslint-disable no-param-reassign */
2+
const getMagentoVersionConfig = require('../config/get-magento-version-config');
3+
const { getCachedPorts } = require('../config/get-port-config');
4+
const { checkRequirements } = require('./requirements');
5+
const { importDumpToMySQL, fixDB } = require('./mysql');
6+
7+
const importDump = {
8+
title: 'Importing Database Dump',
9+
task: (ctx, task) => {
10+
task.title = `Importing database dump '${ctx.importDb}'`;
11+
return task.newListr([
12+
checkRequirements,
13+
getMagentoVersionConfig,
14+
getCachedPorts,
15+
importDumpToMySQL,
16+
fixDB
17+
], {
18+
concurrent: false,
19+
exitOnError: true,
20+
ctx,
21+
rendererOptions: { collapse: false }
22+
});
23+
}
24+
};
25+
26+
module.exports = importDump;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const deleteAdminUsers = {
2+
title: 'Deleting old admin users',
3+
task: async (ctx) => {
4+
const { mysqlConnection } = ctx;
5+
await mysqlConnection.query(`
6+
DELETE FROM admin_user;
7+
`);
8+
}
9+
};
10+
11+
module.exports = deleteAdminUsers;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* eslint-disable no-param-reassign */
2+
const runMagentoCommand = require('../../../util/run-magento');
3+
4+
const indexProducts = {
5+
title: 'Running magento index:reindex command...',
6+
task: async (ctx, task) => {
7+
const { magentoVersion } = ctx;
8+
9+
await runMagentoCommand('index:reindex', {
10+
magentoVersion,
11+
callback: (t) => {
12+
task.output = t;
13+
}
14+
});
15+
},
16+
options: {
17+
bottomBar: 10
18+
}
19+
};
20+
21+
module.exports = indexProducts;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const adjustMagentoConfiguration = require('../magento/setup-magento/adjust-magento-configuration');
2+
const configureElasticsearch = require('../magento/setup-magento/configure-elasticsearch');
3+
const deleteAdminUsers = require('../magento/setup-magento/delete-admin-users');
4+
const indexProducts = require('../magento/setup-magento/index-products');
5+
6+
const fixDB = {
7+
title: 'Fixing database',
8+
task: async (ctx, task) => task.newListr([
9+
adjustMagentoConfiguration,
10+
configureElasticsearch,
11+
deleteAdminUsers,
12+
indexProducts
13+
], {
14+
concurrent: false,
15+
exitOnError: true,
16+
ctx,
17+
rendererOptions: { collapse: false }
18+
})
19+
};
20+
21+
module.exports = fixDB;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* eslint-disable no-param-reassign */
2+
const { execAsyncSpawn } = require('../../util/exec-async-command');
3+
const pathExists = require('../../util/path-exists');
4+
5+
const importDumpToMySQL = {
6+
title: 'Importing Database Dump To MySQL',
7+
task: async (ctx, task) => {
8+
if (!await pathExists(ctx.importDb)) {
9+
throw new Error(`Dump file at ${ctx.importDb} does not exist. Please provide correct relative path to the file`);
10+
}
11+
12+
const { config: { docker }, ports } = ctx;
13+
14+
const { mysql } = docker.getContainers(ports);
15+
16+
const startImportTime = Date.now();
17+
const tickInterval = setInterval(() => {
18+
task.title = `Importing Database Dump To MySQL, ${Math.floor((Date.now() - startImportTime) / 1000)}s in progress...`;
19+
}, 1000);
20+
21+
try {
22+
await execAsyncSpawn(
23+
`docker cp ${ctx.importDb} ${mysql.name}:/dump.sql`
24+
);
25+
26+
await execAsyncSpawn(
27+
`docker exec ${mysql.name} bash -c "cat dump.sql | mysql -umagento -pmagento magento"`,
28+
{
29+
callback: (t) => {
30+
task.output = t;
31+
}
32+
}
33+
);
34+
} catch (e) {
35+
throw new Error(`Unexpected error during dump import.\n\n${e}`);
36+
}
37+
38+
clearInterval(tickInterval);
39+
40+
task.title = 'Database imported!';
41+
},
42+
options: {
43+
bottomBar: 10
44+
}
45+
};
46+
47+
module.exports = importDumpToMySQL;
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module.exports = {
2-
connectToMySQL: require('./connect-to-mysql')
2+
connectToMySQL: require('./connect-to-mysql'),
3+
importDumpToMySQL: require('./import-dump-to-mysql'),
4+
fixDB: require('./fix-db')
35
};

build-packages/magento-scripts/lib/tasks/php/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ const installPhp = {
5050
// eslint-disable-next-line consistent-return
5151
return task.newListr([
5252
updatePhpBrew,
53-
compile,
54-
configure
53+
compile
5554
], {
5655
concurrent: false,
5756
exitOnError: true,
@@ -63,4 +62,4 @@ const installPhp = {
6362
}
6463
};
6564

66-
module.exports = { installPhp };
65+
module.exports = { installPhp, compilePhp: compile, configurePhp: configure };

0 commit comments

Comments
 (0)