allow to configure database version via ENV var (#2055)

This commit is contained in:
Kevin Papst
2020-10-24 21:00:43 +02:00
committed by GitHub
parent 8a8f5cb6ce
commit abd31e3785
2 changed files with 31 additions and 20 deletions

View File

@@ -4,6 +4,7 @@ parameters:
# environment variables are not available yet. # environment variables are not available yet.
# You should not need to change this value. # You should not need to change this value.
env(DATABASE_URL): '' env(DATABASE_URL): ''
env(DATABASE_VERSION): ~
doctrine: doctrine:
dbal: dbal:
@@ -14,7 +15,7 @@ doctrine:
driver: 'pdo_mysql' driver: 'pdo_mysql'
# this setting prevents automatic database detection and finds a lot of false-negatives on doctrine:migrations:diff # this setting prevents automatic database detection and finds a lot of false-negatives on doctrine:migrations:diff
# for null columns with MariaDB. Each migration tries to convert EVERY nullable column. # for null columns with MariaDB. Each migration tries to convert EVERY nullable column.
# server_version: '5.7' server_version: '%env(string:DATABASE_VERSION)%'
charset: utf8mb4 charset: utf8mb4
default_table_options: default_table_options:
charset: utf8mb4 charset: utf8mb4

View File

@@ -26,37 +26,47 @@ class DoctrineCompilerPass implements CompilerPassInterface
'sqlite' 'sqlite'
]; ];
private function getEnvVar(string $name): ?string
{
$envVarValue = null;
if (isset($_ENV[$name])) {
$envVarValue = $_ENV[$name];
}
if ($envVarValue === null && isset($_SERVER[$name])) {
$envVarValue = $_SERVER[$name];
}
if ($envVarValue === null) {
$envVarValue = getenv($name);
}
if ($envVarValue === false || empty($envVarValue)) {
return null;
}
return $envVarValue;
}
/** /**
* @return array|false|null|string * @return string
* @throws \Exception * @throws \Exception
*/ */
protected function findEngine() private function findEngine(): string
{ {
$engine = null; $engine = null;
$databaseUrl = null;
if (null === $databaseUrl && isset($_ENV['DATABASE_URL'])) { if (null !== ($databaseUrl = $this->getEnvVar('DATABASE_URL'))) {
$databaseUrl = $_ENV['DATABASE_URL'];
}
if (null === $databaseUrl && isset($_SERVER['DATABASE_URL'])) {
$databaseUrl = $_SERVER['DATABASE_URL'];
}
if (null === $databaseUrl && (false !== $envDbUrl = getenv('DATABASE_URL'))) {
$databaseUrl = $envDbUrl;
}
if (null !== $databaseUrl) {
$urlParts = explode('://', $databaseUrl); $urlParts = explode('://', $databaseUrl);
$engine = $urlParts[0] ?: null; $engine = $urlParts[0] ?: null;
} }
if (null === $engine) { if ($engine === null) {
$engine = getenv('DATABASE_ENGINE'); $engine = $this->getEnvVar('DATABASE_ENGINE');
} }
if (empty($engine)) { if ($engine === null) {
throw new \Exception( throw new \Exception(
'Could not detect database engine, make sure DATABASE_URL is available from $_SERVER or $_ENV. Check your .env file.' 'Could not detect database engine, make sure DATABASE_URL is available from $_SERVER or $_ENV. Check your .env file.'
); );