firefly-iii/app/Console/Commands/CreateDatabase.php

106 lines
3.4 KiB
PHP
Raw Normal View History

<?php
2020-01-23 12:38:39 -06:00
/**
* CreateDatabase.php
2020-01-23 13:35:02 -06:00
* Copyright (c) 2020 james@firefly-iii.org
2020-01-23 12:38:39 -06: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 12:05:35 -05:00
declare(strict_types=1);
namespace FireflyIII\Console\Commands;
use Illuminate\Console\Command;
use PDO;
use PDOException;
/**
* Class CreateDatabase
*/
class CreateDatabase extends Command
{
/**
* The console command description.
*
* @var string
*/
protected $description = 'Tries to create the database if it doesn\'t exist yet.';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'firefly-iii:create-database';
/**
* Execute the console command.
*
2020-06-06 15:25:52 -05:00
* @return int
*/
2020-06-06 15:25:52 -05:00
public function handle(): int
{
2020-09-18 02:46:22 -05:00
if ('mysql' !== env('DB_CONNECTION', 'mysql')) {
2020-01-29 21:43:54 -06:00
$this->info(sprintf('CreateDB does not apply to "%s", skipped.', env('DB_CONNECTION')));
2020-03-21 09:43:41 -05:00
2020-01-29 21:43:54 -06:00
return 0;
}
// try to set up a raw connection:
2020-11-01 23:20:49 -06:00
$pdo = false;
$exists = false;
$checked = false; // checked for existence of DB?
2020-02-07 13:50:46 -06:00
$dsn = sprintf('mysql:host=%s;port=%d;charset=utf8mb4', env('DB_HOST', 'localhost'), env('DB_PORT', '3306'));
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
2020-11-01 23:20:49 -06:00
// when it fails, display error
try {
$pdo = new PDO($dsn, env('DB_USERNAME'), env('DB_PASSWORD'), $options);
} catch (PDOException $e) {
$this->error(sprintf('Error when connecting to DB: %s', $e->getMessage()));
}
2020-11-01 23:20:49 -06:00
// only continue when no error.
if (false !== $pdo) {
// with PDO, try to list DB's (
$stmt = $pdo->query('SHOW DATABASES;');
$checked = true;
// slightly more complex but less error prone.
foreach ($stmt as $row) {
$name = $row['Database'] ?? false;
if ($name === env('DB_DATABASE')) {
$exists = true;
}
2019-11-17 06:34:33 -06:00
}
}
2020-11-01 23:20:49 -06:00
if (false === $exists && true === $checked) {
$this->error(sprintf('Database "%s" does not exist.', env('DB_DATABASE')));
// try to create it.
2019-11-17 06:34:33 -06:00
$pdo->exec(sprintf('CREATE DATABASE `%s` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;', env('DB_DATABASE')));
$this->info(sprintf('Created database "%s"', env('DB_DATABASE')));
}
2020-11-01 23:20:49 -06:00
if (true === $exists && true === $checked) {
$this->info(sprintf('Database "%s" exists.', env('DB_DATABASE')));
}
return 0;
}
}