Files
firefly-iii/app/Console/Commands/System/CreatesDatabase.php

95 lines
3.4 KiB
PHP
Raw Normal View History

<?php
2020-01-23 19:38:39 +01:00
2023-04-16 07:33:12 +02:00
/*
2020-01-23 19:38:39 +01:00
* CreateDatabase.php
2023-04-16 07:33:12 +02:00
* Copyright (c) 2023 james@firefly-iii.org
2020-01-23 19:38:39 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-06-30 19:05:35 +02:00
declare(strict_types=1);
2023-04-16 07:33:12 +02:00
namespace FireflyIII\Console\Commands\System;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
use Illuminate\Console\Command;
use PDO;
2024-12-27 09:30:41 +01:00
class CreatesDatabase extends Command
{
use ShowsFriendlyMessages;
protected $description = 'Tries to create the database if it doesn\'t exist yet.';
2023-11-05 09:54:53 +01:00
protected $signature = 'firefly-iii:create-database';
2020-06-06 22:25:52 +02:00
public function handle(): int
{
2025-01-03 15:04:15 +01:00
if ('mysql' !== config('database.default')) {
$this->friendlyInfo(sprintf('CreateDB does not apply to "%s", skipped.', config('database.default')));
2020-03-21 15:43:41 +01:00
2020-01-30 04:43:54 +01:00
return 0;
}
// try to set up a raw connection:
$exists = false;
2025-01-03 15:04:15 +01:00
$dsn = sprintf('mysql:host=%s;port=%d;charset=utf8mb4', config('database.mysql.host'), config('database.mysql.port'));
2022-01-28 20:58:08 +01:00
2025-01-03 15:07:30 +01:00
if ('' !== config('database.mysql.unix_socket')) {
2025-01-03 15:04:15 +01:00
$dsn = sprintf('mysql:unix_socket=%s;charset=utf8mb4', config('database.mysql.unix_socket'));
2022-01-28 20:58:08 +01:00
}
$this->friendlyLine(sprintf('DSN is %s', $dsn));
2022-01-28 20:58:08 +01:00
$options = [
2023-12-20 19:35:52 +01:00
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::ATTR_EMULATE_PREPARES => false,
];
2020-11-02 06:20:49 +01:00
// when it fails, display error
try {
2025-01-03 15:04:15 +01:00
$pdo = new \PDO($dsn, config('database.mysql.username'), config('database.mysql.password'), $options);
2023-12-20 19:35:52 +01:00
} catch (\PDOException $e) {
$this->friendlyError(sprintf('Error when connecting to DB: %s', $e->getMessage()));
2023-12-20 19:35:52 +01:00
return 1;
}
2020-11-02 06:20:49 +01:00
// only continue when no error.
2023-11-05 16:11:09 +01:00
// with PDO, try to list DB's (
2023-11-26 12:10:42 +01:00
/** @var array $stmt */
$stmt = $pdo->query('SHOW DATABASES;');
2023-11-05 16:11:09 +01:00
// slightly more complex but less error-prone.
foreach ($stmt as $row) {
$name = $row['Database'] ?? false;
2025-01-03 15:04:15 +01:00
if ($name === config('database.mysql.database')) {
2023-11-05 16:11:09 +01:00
$exists = true;
2019-11-17 13:34:33 +01:00
}
}
2023-11-05 16:11:09 +01:00
if (false === $exists) {
2025-01-03 15:04:15 +01:00
$this->friendlyError(sprintf('Database "%s" does not exist.', config('database.mysql.database')));
// try to create it.
2025-01-03 15:04:15 +01:00
$pdo->exec(sprintf('CREATE DATABASE `%s` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;', config('database.mysql.database')));
$this->friendlyInfo(sprintf('Created database "%s"', config('database.mysql.database')));
}
2023-11-05 16:11:09 +01:00
if (true === $exists) {
2025-01-03 15:04:15 +01:00
$this->friendlyInfo(sprintf('Database "%s" exists.', config('database.mysql.database')));
2020-11-02 06:20:49 +01:00
}
return 0;
}
}