Skip to content

Commit

Permalink
add new command db:create to simplify database setup processus (see #107
Browse files Browse the repository at this point in the history
)
  • Loading branch information
llaville committed Jan 8, 2022
1 parent 87cf538 commit eef8d14
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 25 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/php-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ jobs:
- # Build Database
name: Build Database
run: |
mkdir -p "${{ github.workspace }}/.cache/" && touch "${{ github.workspace }}/.cache/compatinfo-db.sqlite"
vendor/bin/doctrine orm:schema-tool:create --ansi
bin/compatinfo-db db:init --ansi
bin/compatinfo-db db:create --ansi
- # Check Database
name: Check Database
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG-3.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ using the [Keep a CHANGELOG](http://keepachangelog.com) principles.

- new environment variable `APP_VENDOR_DIR` to specify `vendor` directory (auto-detection, if not declared)
- `bin/compatinfo-db` is made available into the `bin-dir` (from composer config)
- new command `db:create` to setup database and load its contents (replaces composer script initialization and `db:init` command)

### Changed

Expand Down
17 changes: 0 additions & 17 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,6 @@
"Bartlett\\CompatInfoDb\\Tests\\": "tests/"
}
},
"scripts": {
"setup-database": [
"mkdir -p ${HOME}/.cache/bartlett/ && touch ${HOME}/.cache/bartlett/compatinfo-db.sqlite",
"export DATABASE_URL=sqlite:///${HOME}/.cache/bartlett/compatinfo-db.sqlite"
],
"post-install-cmd": [
"@setup-database",
"vendor/bin/doctrine orm:schema-tool:create",
"bin/compatinfo-db db:init"
],
"post-update-cmd": [
"@setup-database"
]
},
"scripts-descriptions": {
"setup-database": "Initializes SQLite database from JSON files in data directory."
},
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
Expand Down
15 changes: 14 additions & 1 deletion config/set/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,20 @@

$dbUrl = getenv('DATABASE_URL');
if (false === $dbUrl) {
$dbUrl = 'sqlite:///${HOME}/.cache/bartlett/compatinfo-db.sqlite';
if (PATH_SEPARATOR === ';') {
// windows
$userHome = getenv('USERPROFILE');
} else {
// unix
$userHome = getenv('HOME');
}
$cacheDir = implode(DIRECTORY_SEPARATOR, [$userHome, '.cache', 'bartlett']);
$targetFile = 'compatinfo-db.sqlite';
if (!file_exists($cacheDir)) {
mkdir($cacheDir, 0755, true);
}
$dbUrl = sprintf('sqlite:///%s/%s', $cacheDir, $targetFile);
touch($cacheDir . DIRECTORY_SEPARATOR . $targetFile);
putenv('DATABASE_URL=' . $dbUrl);
}
$connectionParams = ['url' => $dbUrl];
Expand Down
15 changes: 11 additions & 4 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,16 @@ DATABASE_URL="sqlite:///${HOME}/.cache/bartlett/compatinfo-db.sqlite"
DATABASE_URL="postgresql://db_user:db_password@127.0.0.1:5432/db_name?serverVersion=11&charset=utf8"
```

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

* `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`

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

At dependencies installation, Composer use the sqlite back-end. You need to set up in your environment the `DATABASE_URL` variable.
106 changes: 106 additions & 0 deletions src/Presentation/Console/Command/CreateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php declare(strict_types=1);
/**
* This file is part of the PHP_CompatInfoDB package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Bartlett\CompatInfoDb\Presentation\Console\Command;

use Bartlett\CompatInfoDb\Application\Command\CommandBusInterface;
use Bartlett\CompatInfoDb\Application\Query\Init\InitQuery;
use Bartlett\CompatInfoDb\Application\Query\QueryBusInterface;
use Bartlett\CompatInfoDb\Presentation\Console\ApplicationInterface;
use Bartlett\CompatInfoDb\Presentation\Console\Style;
use Bartlett\CompatInfoDb\Presentation\Console\StyleInterface;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\ORM\Tools\ToolsException;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Create the database schema and load its contents from JSON files
*
* @since Release 3.18.0
* @author Laurent Laville
*/
class CreateCommand extends AbstractCommand implements CommandInterface
{
public const NAME = 'db:create';

protected EntityManagerInterface $entityManager;

public function __construct(CommandBusInterface $commandBus, QueryBusInterface $queryBus, EntityManagerInterface $em)
{
parent::__construct($commandBus, $queryBus);
$this->entityManager = $em;
}

protected function configure(): void
{
$this->setName(self::NAME)
->setDescription('Create the database schema and load its contents from JSON files')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new Style($input, $output);
if (getenv('APP_ENV') === 'prod') {
$io->caution('This operation should not be executed in a production environment!');
}
if (!$this->createDatabase($io)) {
return self::FAILURE;
}

if (!$this->loadDatabase($io)) {
return self::FAILURE;
}

return self::SUCCESS;
}

private function createDatabase(StyleInterface $io): bool
{
$metadatas = $this->entityManager->getMetadataFactory()->getAllMetadata();

if (empty($metadatas)) {
$io->error('No Metadata Classes found to create Database.');
return false;
}

$schemaTool = new SchemaTool($this->entityManager);

$io->writeln('Creating database schema...');

try {
$schemaTool->createSchema($metadatas);
} catch (ToolsException $e) {
$io->error($e->getMessage());
return false;
}

$io->success('Database schema created successfully!');
return true;
}

private function loadDatabase(StyleInterface $io): bool
{
/** @var ApplicationInterface $app */
$app = $this->getApplication();

$appVersion = $app->getInstalledVersion(true, 'bartlett/php-compatinfo-db');

$io->writeln('Loading database contents...');

$initQuery = new InitQuery($appVersion, $io, false, false);

$exitCode = $this->queryBus->query($initQuery);

$io->success('Database loaded successfully!');
return ($exitCode === self::SUCCESS);
}
}

0 comments on commit eef8d14

Please sign in to comment.