Skip to content

Commit

Permalink
improve connect-to-mysql, add creation of magento database if it does…
Browse files Browse the repository at this point in the history
… not exist
  • Loading branch information
ejnshtein committed Jun 6, 2022
1 parent 7ace410 commit 2f1f156
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 17 deletions.
78 changes: 61 additions & 17 deletions build-packages/magento-scripts/lib/tasks/mysql/connect-to-mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,57 @@ const mysql = require('mysql2/promise');
const UnknownError = require('../../errors/unknown-error');
const { execAsyncSpawn } = require('../../util/exec-async-command');
const sleep = require('../../util/sleep');
const { createMagentoDatabase } = require('./create-magento-database');

/**
* @type {() => import('listr2').ListrTask<import('../../../typings/context').ListrContext>}
* @returns {import('listr2').ListrTask<import('../../../typings/context').ListrContext>}
*/
const connectToMySQL = () => ({
title: 'Connecting to MySQL server...',
skip: (ctx) => ctx.skipSetup,
const waitForMySQLInitialization = () => ({
title: 'Waiting for MySQL to initialize',
task: async (ctx, task) => {
const { mysql: { name } } = ctx.config.docker.getContainers();
let mysqlReadyForConnections = false;
while (!mysqlReadyForConnections) {
const mysqlOutput = await execAsyncSpawn(`docker logs ${name}`);
if (mysqlOutput.includes('ready for connections')) {
mysqlReadyForConnections = true;
break;
} else if (mysqlOutput.includes('Initializing database files')) {
task.output = `MySQL is initializing database files!
Please wait, this will take some time and do not restart the MySQL container until initialization is finished!`;

let mysqlFinishedInitialization = false;
while (!mysqlFinishedInitialization) {
const mysqlOutput = await execAsyncSpawn(`docker logs ${name}`);
if (mysqlOutput.includes('MySQL init process done.') && !mysqlFinishedInitialization) {
mysqlFinishedInitialization = true;
break;
}
await sleep(2000);
}
}

await sleep(2000);
}
},
options: {
bottomBar: 10
}
});

/**
* @returns {import('listr2').ListrTask<import('../../../typings/context').ListrContext>}
*/
const gettingMySQLConnection = () => ({
title: 'Getting MySQL connection',
task: async (ctx, task) => {
const { config: { docker }, ports } = ctx;
const { mysql: { env, name } } = docker.getContainers();
const { mysql: { env } } = docker.getContainers();
let tries = 0;
let maxTries = 20;
const maxTries = 20;
const errors = [];
while (tries < maxTries) {
tries++;

if (maxTries !== 120) {
const mysqlOutput = await execAsyncSpawn(`docker logs ${name}`);
if (mysqlOutput.includes('Initializing database files')) {
maxTries = 120;
task.output = `MySQL is initializing database files!
Please wait, this will take some time and do not restart the MySQL container until initialization is finished!`;
}
await sleep(2000);
}
try {
const connection = await mysql.createConnection({
host: '127.0.0.1',
Expand All @@ -49,7 +75,25 @@ Please wait, this will take some time and do not restart the MySQL container unt
}

task.title = 'MySQL server connected!';
},
}
});

/**
* @returns {import('listr2').ListrTask<import('../../../typings/context').ListrContext>}
*/
const connectToMySQL = () => ({
title: 'Connecting to MySQL server',
skip: (ctx) => ctx.skipSetup,
task: (ctx, task) => task.newListr([
waitForMySQLInitialization(),
createMagentoDatabase(),
gettingMySQLConnection()
], {
concurrent: false,
rendererOptions: {
collapse: true
}
}),
options: {
bottomBar: 10
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { execAsyncSpawn } = require('../../util/exec-async-command');

/**
* Will create database 'magento' in MySQL if it does not exist for some reason
* @returns {import('listr2').ListrTask<import('../../../typings/context').ListrContext>}
*/
const createMagentoDatabase = () => ({
title: 'Creating Magento database in MySQL',
task: async (ctx) => {
const { mysql } = ctx.config.docker.getContainers();

await execAsyncSpawn(`docker exec ${mysql.name} mysql -umagento -pmagento -h 127.0.0.1 -e "CREATE DATABASE IF NOT EXISTS magento;"`);
}
});

module.exports = {
createMagentoDatabase
};

0 comments on commit 2f1f156

Please sign in to comment.