Skip to content

Commit 7a23ee5

Browse files
committed
Fix problems with local mysql connections
- Sync is aborting after deletion of application database - Remove ping with mysql command SELECT 1 - Read LOCAL.mysql and set it as local database connection Fixes #112
1 parent ecb728b commit 7a23ee5

File tree

6 files changed

+54
-19
lines changed

6 files changed

+54
-19
lines changed

src/app/CliTools/Console/Command/AbstractCommand.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,15 @@ protected function mysqlQuote($value) {
353353
* @param string $sql
354354
* @return array|null
355355
*/
356-
protected function execSqlCommand($sql)
356+
protected function execSqlCommand($sql, $database = null)
357357
{
358-
return $this->createMysqlCommand('-e', $sql)->execute()->getOutput();
358+
$command = $this->createMysqlCommand('-e', $sql);
359+
360+
if ($database !== null) {
361+
$command->addArgumentTemplate('-D %s', $database);
362+
}
363+
364+
$command->execute()->getOutput();
359365
}
360366

361367

src/app/CliTools/Console/Command/AbstractDockerCommand.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,6 @@ protected function mysqlQuote($value) {
8787
return '\'' . addslashes($value) . '\'';
8888
}
8989

90-
/**
91-
* Execute sql command (using local mysql command)
92-
*
93-
* @param string $sql
94-
* @return array|null
95-
*/
96-
protected function execSqlCommand($sql)
97-
{
98-
return $this->createMysqlCommand('-e', $sql)->execute()->getOutput();
99-
}
100-
101-
10290
/**
10391
* Execute sql command (using local mysql command)
10492
*

src/app/CliTools/Console/Command/Mysql/RestoreCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ public function execute(InputInterface $input, OutputInterface $output)
7575
$output->writeln('<h2>Restoring dump "' . $dumpFile . '" into database "' . $database . '"</h2>');
7676

7777
$output->writeln('<p>Creating database</p>');
78-
$this->execSqlCommand('DROP DATABASE IF EXISTS ' . addslashes($database));
79-
$this->execSqlCommand('CREATE DATABASE ' . addslashes($database));
78+
$this->execSqlCommand('DROP DATABASE IF EXISTS ' . addslashes($database), 'mysql');
79+
$this->execSqlCommand('CREATE DATABASE ' . addslashes($database),'mysql');
8080

8181
$commandMysql = $this->createMysqlCommand($database, '--one-database');
8282

src/app/CliTools/Console/Command/Sync/AbstractCommand.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,34 @@ protected function initializeConfiguration()
149149
// Read configuration
150150
$this->readConfiguration();
151151

152+
$this->initDatabaseConfiguration();
152153
$this->initDockerContainer();
153154
}
154155

156+
/**
157+
* Init database configuration (for local one)
158+
*/
159+
protected function initDatabaseConfiguration()
160+
{
161+
$hostname = DatabaseConnection::getDbHostname();
162+
$username = DatabaseConnection::getDbUsername();
163+
$password = DatabaseConnection::getDbPassword();
164+
165+
if ($this->config->exists('LOCAL.mysql.hostname')) {
166+
$hostname = $this->config->get('LOCAL.mysql.hostname');
167+
}
168+
169+
if ($this->config->exists('LOCAL.mysql.username')) {
170+
$username = $this->config->get('LOCAL.mysql.username');
171+
}
172+
173+
if ($this->config->exists('LOCAL.mysql.password')) {
174+
$password = $this->config->get('LOCAL.mysql.password');
175+
}
176+
177+
DatabaseConnection::setDsn('mysql:host=' . $hostname, $username, $password);
178+
}
179+
155180
/**
156181
* Init docker container setting
157182
*/
@@ -883,6 +908,10 @@ protected function createMysqlRestoreCommand($database, $dumpFile)
883908
$command->addArgumentTemplate('--password %s', $this->config->get('LOCAL.mysql.password'));
884909
}
885910

911+
if ($this->output->isVerbose()) {
912+
$command->addArgument('-v');
913+
}
914+
886915
return $command;
887916
}
888917

@@ -923,6 +952,10 @@ protected function createMysqlBackupCommand($database, $dumpFile, $filterNameBla
923952
$command->addArgumentTemplate('--whitelist=%s', $filterNameWhitelist);
924953
}
925954

955+
if ($this->output->isVerbose()) {
956+
$command->addArgument('-v');
957+
}
958+
926959
return $command;
927960
}
928961

@@ -985,6 +1018,14 @@ protected function createRemoteMySqlCommand($database = null)
9851018
return $command;
9861019
}
9871020

1021+
protected function localMysqlPing()
1022+
{
1023+
$echoCommand = new CommandBuilder('echo', 'SELECT 1');
1024+
$mysqlCommand = $this->createLocalMySqlCommand();
1025+
$mysqlCommand->setOutputRedirect('> /dev/null');
1026+
$echoCommand->addPipeCommand($mysqlCommand);
1027+
$echoCommand->executeInteractive();
1028+
}
9881029

9891030
/**
9901031
* Create new mysql command
@@ -1013,7 +1054,7 @@ protected function createLocalMySqlCommand($database = null)
10131054
$command->addArgumentTemplate('-h%s', DatabaseConnection::getDbHostname());
10141055
}
10151056

1016-
// Add hostname
1057+
// Add port
10171058
if (DatabaseConnection::getDbPort()) {
10181059
$command->addArgumentTemplate('-P%s', DatabaseConnection::getDbPort());
10191060
}

src/app/CliTools/Console/Command/Sync/DeployCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected function runMain()
7575

7676
// Check database connection
7777
if ($runMysql && $this->contextConfig->exists('mysql')) {
78-
DatabaseConnection::ping();
78+
$this->localMysqlPing();
7979
}
8080

8181
// Sync files with rsync to local storage

src/app/CliTools/Console/Command/Sync/ServerCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ protected function runMain()
101101
if ($runMysql && $this->contextConfig->exists('mysql')) {
102102
// ping only on mysql connection, not dockerized exec calling
103103
if (!$this->config->exists('LOCAL.mysql.docker') && !$this->config->exists('LOCAL.mysql.docker-compose')) {
104-
DatabaseConnection::ping();
104+
$this->localMysqlPing();
105105
}
106106
}
107107

0 commit comments

Comments
 (0)