From 1d2f6485fcbb413ae3884ca17ace0103db9cdc56 Mon Sep 17 00:00:00 2001 From: Lito Date: Tue, 3 Nov 2020 22:39:19 +0100 Subject: [PATCH 1/4] [8.x] Added suport to MySQL dump and import using socket --- .../Database/Schema/MySqlSchemaState.php | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Database/Schema/MySqlSchemaState.php b/src/Illuminate/Database/Schema/MySqlSchemaState.php index b8bf3072fc1b..0b8a20e90669 100644 --- a/src/Illuminate/Database/Schema/MySqlSchemaState.php +++ b/src/Illuminate/Database/Schema/MySqlSchemaState.php @@ -69,9 +69,17 @@ protected function appendMigrationData(string $path) */ public function load($path) { - $process = $this->makeProcess('mysql --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}" --user="${:LARAVEL_LOAD_USER}" --password="${:LARAVEL_LOAD_PASSWORD}" --database="${:LARAVEL_LOAD_DATABASE}" < "${:LARAVEL_LOAD_PATH}"'); + $config = $this->connection->getConfig(); - $process->mustRun(null, array_merge($this->baseVariables($this->connection->getConfig()), [ + if ($config['unix_socket']) { + $cmd = 'mysql --socket="${:LARAVEL_LOAD_SOCKET}"'; + } else { + $cmd = 'mysql --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}"'; + } + + $cmd .= ' --user="${:LARAVEL_LOAD_USER}" --password="${:LARAVEL_LOAD_PASSWORD}" --database="${:LARAVEL_LOAD_DATABASE}" < "${:LARAVEL_LOAD_PATH}"'; + + $this->makeProcess($cmd)->mustRun(null, array_merge($this->baseVariables($config), [ 'LARAVEL_LOAD_PATH' => $path, ])); } @@ -84,10 +92,17 @@ public function load($path) protected function baseDumpCommand() { $columnStatistics = $this->connection->isMaria() ? '' : '--column-statistics=0'; - $gtidPurged = $this->connection->isMaria() ? '' : '--set-gtid-purged=OFF'; - return 'mysqldump '.$gtidPurged.' '.$columnStatistics.' --skip-add-drop-table --skip-add-locks --skip-comments --skip-set-charset --tz-utc --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}" --user="${:LARAVEL_LOAD_USER}" --password="${:LARAVEL_LOAD_PASSWORD}" "${:LARAVEL_LOAD_DATABASE}"'; + $cmd = 'mysqldump '.$gtidPurged.' '.$columnStatistics.' --skip-add-drop-table --skip-add-locks --skip-comments --skip-set-charset --tz-utc'; + + if ($this->connection->getConfig()['unix_socket']) { + $cmd .= ' --socket="${:LARAVEL_LOAD_SOCKET}"'; + } else { + $cmd .= ' --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}"'; + } + + return $cmd.' --user="${:LARAVEL_LOAD_USER}" --password="${:LARAVEL_LOAD_PASSWORD}" "${:LARAVEL_LOAD_DATABASE}"'; } /** @@ -99,6 +114,7 @@ protected function baseDumpCommand() protected function baseVariables(array $config) { return [ + 'LARAVEL_LOAD_SOCKET' => $config['unix_socket'], 'LARAVEL_LOAD_HOST' => is_array($config['host']) ? $config['host'][0] : $config['host'], 'LARAVEL_LOAD_PORT' => $config['port'], 'LARAVEL_LOAD_USER' => $config['username'], From 2b8fbaf830c5101a28d67ded5b9444db2cad26b3 Mon Sep 17 00:00:00 2001 From: Lito Date: Wed, 4 Nov 2020 17:58:46 +0100 Subject: [PATCH 2/4] Avoid schema:dump errors when database host, port or unix_socket is not defined --- .../Database/Schema/MySqlSchemaState.php | 49 ++++++++++--------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/src/Illuminate/Database/Schema/MySqlSchemaState.php b/src/Illuminate/Database/Schema/MySqlSchemaState.php index 0b8a20e90669..02cb4e6744b4 100644 --- a/src/Illuminate/Database/Schema/MySqlSchemaState.php +++ b/src/Illuminate/Database/Schema/MySqlSchemaState.php @@ -69,17 +69,9 @@ protected function appendMigrationData(string $path) */ public function load($path) { - $config = $this->connection->getConfig(); + $cmd = 'mysql '.$this->connectionString().' --database="${:LARAVEL_LOAD_DATABASE}" < "${:LARAVEL_LOAD_PATH}"'; - if ($config['unix_socket']) { - $cmd = 'mysql --socket="${:LARAVEL_LOAD_SOCKET}"'; - } else { - $cmd = 'mysql --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}"'; - } - - $cmd .= ' --user="${:LARAVEL_LOAD_USER}" --password="${:LARAVEL_LOAD_PASSWORD}" --database="${:LARAVEL_LOAD_DATABASE}" < "${:LARAVEL_LOAD_PATH}"'; - - $this->makeProcess($cmd)->mustRun(null, array_merge($this->baseVariables($config), [ + $this->makeProcess($cmd)->mustRun(null, array_merge($this->baseVariables($this->connection->getConfig()), [ 'LARAVEL_LOAD_PATH' => $path, ])); } @@ -91,18 +83,7 @@ public function load($path) */ protected function baseDumpCommand() { - $columnStatistics = $this->connection->isMaria() ? '' : '--column-statistics=0'; - $gtidPurged = $this->connection->isMaria() ? '' : '--set-gtid-purged=OFF'; - - $cmd = 'mysqldump '.$gtidPurged.' '.$columnStatistics.' --skip-add-drop-table --skip-add-locks --skip-comments --skip-set-charset --tz-utc'; - - if ($this->connection->getConfig()['unix_socket']) { - $cmd .= ' --socket="${:LARAVEL_LOAD_SOCKET}"'; - } else { - $cmd .= ' --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}"'; - } - - return $cmd.' --user="${:LARAVEL_LOAD_USER}" --password="${:LARAVEL_LOAD_PASSWORD}" "${:LARAVEL_LOAD_DATABASE}"'; + return 'mysqldump '.$this->connectionString().' --skip-add-drop-table --skip-add-locks --skip-comments --skip-set-charset --tz-utc "${:LARAVEL_LOAD_DATABASE}"'; } /** @@ -113,10 +94,12 @@ protected function baseDumpCommand() */ protected function baseVariables(array $config) { + $config['host'] = $config['host'] ?? ''; + return [ - 'LARAVEL_LOAD_SOCKET' => $config['unix_socket'], + 'LARAVEL_LOAD_SOCKET' => $config['unix_socket'] ?? '', 'LARAVEL_LOAD_HOST' => is_array($config['host']) ? $config['host'][0] : $config['host'], - 'LARAVEL_LOAD_PORT' => $config['port'], + 'LARAVEL_LOAD_PORT' => $config['port'] ?? '', 'LARAVEL_LOAD_USER' => $config['username'], 'LARAVEL_LOAD_PASSWORD' => $config['password'], 'LARAVEL_LOAD_DATABASE' => $config['database'], @@ -147,4 +130,22 @@ protected function executeDumpProcess(Process $process, $output, array $variable return $process; } + + /** + * Generate a common connection string (--socket, --host, --port, --user, --password) + * + * @return string + */ + protected function connectionString() + { + $cmd = ' --user="${:LARAVEL_LOAD_USER}" --password="${:LARAVEL_LOAD_PASSWORD}"'; + + if ($this->connection->getConfig()['unix_socket'] ?? false) { + $cmd .= ' --socket="${:LARAVEL_LOAD_SOCKET}"'; + } else { + $cmd .= ' --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}"'; + } + + return $cmd; + } } From a0536eed8aaf40adef402f3afde732332c65ad6f Mon Sep 17 00:00:00 2001 From: Lito Date: Wed, 4 Nov 2020 18:04:11 +0100 Subject: [PATCH 3/4] Fixed isMaria checks on baseDumpCommand --- src/Illuminate/Database/Schema/MySqlSchemaState.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Schema/MySqlSchemaState.php b/src/Illuminate/Database/Schema/MySqlSchemaState.php index 02cb4e6744b4..46782b95f5f0 100644 --- a/src/Illuminate/Database/Schema/MySqlSchemaState.php +++ b/src/Illuminate/Database/Schema/MySqlSchemaState.php @@ -83,7 +83,13 @@ public function load($path) */ protected function baseDumpCommand() { - return 'mysqldump '.$this->connectionString().' --skip-add-drop-table --skip-add-locks --skip-comments --skip-set-charset --tz-utc "${:LARAVEL_LOAD_DATABASE}"'; + $cmd = 'mysqldump '.$this->connectionString().' --skip-add-locks --skip-comments --skip-set-charset --tz-utc'; + + if ($this->connection->isMaria()) { + $cmd .= ' --column-statistics=0 --set-gtid-purged=OFF'; + } + + return $cmd.' "${:LARAVEL_LOAD_DATABASE}"'; } /** From 5c1d55a4f230d394324b525a472c6e734c3b04c9 Mon Sep 17 00:00:00 2001 From: Lito Date: Wed, 4 Nov 2020 18:07:58 +0100 Subject: [PATCH 4/4] Fixed comment typo --- src/Illuminate/Database/Schema/MySqlSchemaState.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Schema/MySqlSchemaState.php b/src/Illuminate/Database/Schema/MySqlSchemaState.php index 46782b95f5f0..642799fc3b75 100644 --- a/src/Illuminate/Database/Schema/MySqlSchemaState.php +++ b/src/Illuminate/Database/Schema/MySqlSchemaState.php @@ -138,7 +138,7 @@ protected function executeDumpProcess(Process $process, $output, array $variable } /** - * Generate a common connection string (--socket, --host, --port, --user, --password) + * Generate a common connection string (--socket, --host, --port, --user, --password). * * @return string */