diff --git a/.github/workflows/php-tests.yaml b/.github/workflows/php-tests.yaml index ba6ad34c..264b10bd 100644 --- a/.github/workflows/php-tests.yaml +++ b/.github/workflows/php-tests.yaml @@ -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 diff --git a/CHANGELOG-3.x.md b/CHANGELOG-3.x.md index 5015f6a3..310472f7 100644 --- a/CHANGELOG-3.x.md +++ b/CHANGELOG-3.x.md @@ -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 diff --git a/composer.json b/composer.json index f734e423..dbbe88cc 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/config/set/default.php b/config/set/default.php index 4a27e79b..ed48601d 100644 --- a/config/set/default.php +++ b/config/set/default.php @@ -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]; diff --git a/docs/getting-started.md b/docs/getting-started.md index c425599f..1d5cb01e 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -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. diff --git a/src/Presentation/Console/Command/CreateCommand.php b/src/Presentation/Console/Command/CreateCommand.php new file mode 100644 index 00000000..c471688d --- /dev/null +++ b/src/Presentation/Console/Command/CreateCommand.php @@ -0,0 +1,106 @@ +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); + } +}