Skip to content

Commit

Permalink
keep HOME placeholder valid even for pre-defined DATABASE_URL (see #107)
Browse files Browse the repository at this point in the history
  • Loading branch information
llaville committed Jan 8, 2022
1 parent eef8d14 commit b9308f1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
29 changes: 19 additions & 10 deletions config/set/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,32 @@
$services->alias(ConstantRepository::class, InfrastructureConstantRepository::class);
$services->alias(ClassRepository::class, InfrastructureClassRepository::class);

if (PATH_SEPARATOR === ';') {
// windows
$userHome = getenv('USERPROFILE');
} else {
// unix
$userHome = getenv('HOME');
}
$dbUrl = getenv('DATABASE_URL');
if (false === $dbUrl) {
if (PATH_SEPARATOR === ';') {
// windows
$userHome = getenv('USERPROFILE');
} else {
// unix
$userHome = getenv('HOME');
}
$cacheDir = implode(DIRECTORY_SEPARATOR, [$userHome, '.cache', 'bartlett']);
$targetFile = 'compatinfo-db.sqlite';
$dbUrl = sprintf('sqlite:///%s/%s', $cacheDir, $targetFile);
putenv('DATABASE_URL=' . $dbUrl);
} else {
$dbUrl = str_replace(['${HOME}', '%HOME%'], $userHome, $dbUrl);
}

$url = preg_replace('#^((?:pdo_)?sqlite3?):///#', '$1://localhost/', $dbUrl);
$url = parse_url($url);

if ('sqlite' === $url['scheme']) {
$cacheDir = dirname($url['path']);
if (!file_exists($cacheDir)) {
mkdir($cacheDir, 0755, true);
}
$dbUrl = sprintf('sqlite:///%s/%s', $cacheDir, $targetFile);
touch($cacheDir . DIRECTORY_SEPARATOR . $targetFile);
putenv('DATABASE_URL=' . $dbUrl);
touch($url['path']);
}
$connectionParams = ['url' => $dbUrl];

Expand Down
11 changes: 5 additions & 6 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,13 @@ DATABASE_URL="postgresql://db_user:db_password@127.0.0.1:5432/db_name?serverVers

If you change database connection, you have to run following command(s):

- If you have CompatInfoDB 3.17 or lower
- `vendor/bin/doctrine orm:schema-tool:create`
- `bin/compatinfo-db db:init`
* If you have CompatInfoDB 3.17 or lower
* `vendor/bin/doctrine orm:schema-tool:create`
* `bin/compatinfo-db db:init`

At dependencies installation, Composer use the sqlite back-end. You need to set up in your environment the `DATABASE_URL` variable.

- If you have CompatInfoDB 3.18 or greater
- `bin/compatinfo-db db:create`
* If you have CompatInfoDB 3.18 or greater
* `bin/compatinfo-db db:create`

At first run of CompatInfoDB, `DATABASE_URL` will be set to use default SQLite connection

0 comments on commit b9308f1

Please sign in to comment.