Skip to content

Commit

Permalink
refactoring and cover tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriizee committed Feb 7, 2025
1 parent e119e47 commit 100e64e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/Illuminate/Database/Schema/MySqlSchemaState.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ protected function baseVariables(array $config)
{
$config['host'] ??= '';

return [
return array_merge([
'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'],
'LARAVEL_LOAD_PASSWORD' => $config['password'] ?? '',
'LARAVEL_LOAD_DATABASE' => $config['database'],
];
], $this->getAdditionalOptionValues($config['options']));
}

/**
Expand Down Expand Up @@ -197,10 +197,34 @@ private function getAdditionalConnectionOptions(array $options): string

foreach ($this->getOptionMap() as $optionKey => $optionValue) {
if (isset($options[$optionKey])) {
$connectionString .= sprintf(' %s=%s', $optionValue, $options[$optionKey]);
$connectionString .= sprintf(
' %s="${:LARAVEL_LOAD_%s}"',
$optionValue,
Str::upper(Str::replace(['--', '-'], [null, '_'], $optionValue))
);
}
}

return $connectionString;
}

/**
* Get mysql options connection values for binding.
*
* @param array $options
* @return array
*/
private function getAdditionalOptionValues(array $options): array
{
$values = [];

foreach ($this->getOptionMap() as $optionKey => $optionValue) {
if (isset($options[$optionKey])) {
$valueKey = 'LARAVEL_LOAD_' . Str::upper(Str::replace(['--', '-'], [null, '_'], $optionValue));
$values[$valueKey] = $options[$optionKey];
}
}

return $values;
}
}
18 changes: 18 additions & 0 deletions tests/Database/DatabaseMySqlSchemaStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ public static function provider(): Generator
],
];

yield 'ssl' => [
' --user="${:LARAVEL_LOAD_USER}" --password="${:LARAVEL_LOAD_PASSWORD}" --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}" --ssl="${:LARAVEL_LOAD_SSL}"', [
'LARAVEL_LOAD_SOCKET' => '',
'LARAVEL_LOAD_HOST' => '',
'LARAVEL_LOAD_PORT' => '',
'LARAVEL_LOAD_USER' => 'root',
'LARAVEL_LOAD_PASSWORD' => '',
'LARAVEL_LOAD_DATABASE' => 'forge',
'LARAVEL_LOAD_SSL' => 'OFF',
], [
'username' => 'root',
'database' => 'forge',
'options' => [
\PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT=> 'OFF',
],
],
];

yield 'unix socket' => [
' --user="${:LARAVEL_LOAD_USER}" --password="${:LARAVEL_LOAD_PASSWORD}" --socket="${:LARAVEL_LOAD_SOCKET}"', [
'LARAVEL_LOAD_SOCKET' => '/tmp/mysql.sock',
Expand Down

0 comments on commit 100e64e

Please sign in to comment.