Skip to content

Commit 1fc329c

Browse files
committed
bug #628 Workaround to use DATABASE_URL with %kernel.project_dir% parameter (yceruto)
This PR was merged into the master branch. Discussion ---------- Workaround to use DATABASE_URL with %kernel.project_dir% parameter Setting `DATABASE_URL=sqlite:///var/data/blog.sqlite` as default, causes this known error through `index.php`: ``` An exception occurred in driver: SQLSTATE[HY000] [14] unable to open database file ``` So the right relative path in this case should be `sqlite:///../var/data/blog.sqlite` BUT then it wouldn't work for `bin/console` entries. Then, relative paths shouldn't be the right way but absolute paths. The right thing is that soon (in 3.4 & 4.0) we can use environment variables with configuration parameters symfony/symfony#23901, so the final configuration would be the one proposed. For now a workaround to make both scenarios work `index.php` and `bin/console`. Commits ------- e45b5d5 Workaround to use DATABASE_URL with %kernel.project_dir% parameter
2 parents 5358558 + e45b5d5 commit 1fc329c

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

.env.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ APP_SECRET=67d829bf61dc5f87a73fd814e2c9f629
1212
# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
1313
# For a sqlite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
1414
# Set "serverVersion" to your server version to avoid edge-case exceptions and extra database calls
15-
DATABASE_URL=sqlite:///var/data/blog.sqlite
15+
DATABASE_URL=sqlite:///%kernel.project_dir%/var/data/blog.sqlite
1616
###< doctrine/doctrine-bundle ###
1717

1818
###> symfony/swiftmailer-bundle ###

src/Kernel.php

+19
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
1515
use Symfony\Component\Config\Loader\LoaderInterface;
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\Dotenv\Dotenv;
1718
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
1819
use Symfony\Component\Routing\RouteCollectionBuilder;
1920

@@ -26,6 +27,24 @@ final class Kernel extends BaseKernel
2627

2728
private const CONFIG_EXTS = '.{php,xml,yaml,yml}';
2829

30+
public function __construct($environment, $debug)
31+
{
32+
/*
33+
* Workaround to avoid: An exception occurred in driver: SQLSTATE[HY000] [14] unable to open database file
34+
* As environment variables is not supported yet to be used with configuration parameters.
35+
*
36+
* @TODO remove in 3.4
37+
*/
38+
39+
if (isset($_ENV['DATABASE_URL']) && false !== mb_strpos($_ENV['DATABASE_URL'], '%kernel.project_dir%')) {
40+
(new Dotenv())->populate([
41+
'DATABASE_URL' => str_replace('%kernel.project_dir%', $this->getProjectDir(), $_ENV['DATABASE_URL']),
42+
]);
43+
}
44+
45+
parent::__construct($environment, $debug);
46+
}
47+
2948
public function getCacheDir(): string
3049
{
3150
return dirname(__DIR__).'/var/cache/'.$this->environment;

0 commit comments

Comments
 (0)