| 
									
										
										
										
											2019-11-17 13:18:35 +01:00
										 |  |  | <?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; | 
					
						
							| 
									
										
										
										
											2019-11-17 13:18:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-20 07:16:56 +02:00
										 |  |  | use FireflyIII\Console\Commands\ShowsFriendlyMessages; | 
					
						
							| 
									
										
										
										
											2019-11-17 13:18:35 +01:00
										 |  |  | use Illuminate\Console\Command; | 
					
						
							|  |  |  | use PDO; | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  | use PDOException; | 
					
						
							| 
									
										
										
										
											2019-11-17 13:18:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-27 09:30:41 +01:00
										 |  |  | class CreatesDatabase extends Command | 
					
						
							| 
									
										
										
										
											2019-11-17 13:18:35 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-20 07:16:56 +02:00
										 |  |  |     use ShowsFriendlyMessages; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-17 13:18:35 +01:00
										 |  |  |     protected $description = 'Tries to create the database if it doesn\'t exist yet.'; | 
					
						
							| 
									
										
										
										
											2023-11-05 09:54:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |     protected $signature   = 'firefly-iii:create-database'; | 
					
						
							| 
									
										
										
										
											2019-11-17 13:18:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-06 22:25:52 +02:00
										 |  |  |     public function handle(): int | 
					
						
							| 
									
										
										
										
											2019-11-17 13:18:35 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-01-19 19:07:19 +01:00
										 |  |  |         if ('mysql' !== env('DB_CONNECTION')) { // @phpstan-ignore larastan.noEnvCallsOutsideOfConfig */
 | 
					
						
							| 
									
										
										
										
											2025-01-05 14:33:33 +01:00
										 |  |  |             $this->friendlyInfo(sprintf('CreateDB does not apply to "%s", skipped.', env('DB_CONNECTION'))); | 
					
						
							| 
									
										
										
										
											2020-03-21 15:43:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-30 04:43:54 +01:00
										 |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2019-11-17 13:18:35 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         // try to set up a raw connection:
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $exists  = false; | 
					
						
							| 
									
										
										
										
											2025-01-05 14:33:33 +01:00
										 |  |  |         $dsn     = sprintf('mysql:host=%s;port=%d;charset=utf8mb4', env('DB_HOST'), env('DB_PORT')); | 
					
						
							| 
									
										
										
										
											2022-01-28 20:58:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-05 14:33:33 +01:00
										 |  |  |         if ('' !== (string) env('DB_SOCKET')) { | 
					
						
							|  |  |  |             $dsn = sprintf('mysql:unix_socket=%s;charset=utf8mb4', env('DB_SOCKET')); | 
					
						
							| 
									
										
										
										
											2022-01-28 20:58:08 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-20 07:16:56 +02:00
										 |  |  |         $this->friendlyLine(sprintf('DSN is %s', $dsn)); | 
					
						
							| 
									
										
										
										
											2022-01-28 20:58:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-17 13:18:35 +01:00
										 |  |  |         $options = [ | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  |             PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION, | 
					
						
							|  |  |  |             PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, | 
					
						
							|  |  |  |             PDO::ATTR_EMULATE_PREPARES   => false, | 
					
						
							| 
									
										
										
										
											2019-11-17 13:18:35 +01:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2020-11-02 06:20:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // when it fails, display error
 | 
					
						
							| 
									
										
										
										
											2019-11-17 13:18:35 +01:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  |             $pdo = new PDO($dsn, (string) env('DB_USERNAME'), (string) env('DB_PASSWORD'), $options); | 
					
						
							|  |  |  |         } catch (PDOException $e) { | 
					
						
							| 
									
										
										
										
											2023-06-20 07:16:56 +02:00
										 |  |  |             $this->friendlyError(sprintf('Error when connecting to DB: %s', $e->getMessage())); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-20 19:00:45 -03:00
										 |  |  |             return 1; | 
					
						
							| 
									
										
										
										
											2019-11-17 13:18:35 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											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 */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $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-05 14:33:33 +01:00
										 |  |  |             if ($name === env('DB_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-05 14:33:33 +01:00
										 |  |  |             $this->friendlyError(sprintf('Database "%s" does not exist.', env('DB_DATABASE'))); | 
					
						
							| 
									
										
										
										
											2019-11-17 13:18:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // try to create it.
 | 
					
						
							| 
									
										
										
										
											2025-01-05 14:33:33 +01:00
										 |  |  |             $pdo->exec(sprintf('CREATE DATABASE `%s` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;', env('DB_DATABASE'))); | 
					
						
							|  |  |  |             $this->friendlyInfo(sprintf('Created database "%s"', env('DB_DATABASE'))); | 
					
						
							| 
									
										
										
										
											2019-11-17 13:18:35 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-11-05 16:11:09 +01:00
										 |  |  |         if (true === $exists) { | 
					
						
							| 
									
										
										
										
											2025-01-05 14:33:33 +01:00
										 |  |  |             $this->friendlyInfo(sprintf('Database "%s" exists.', env('DB_DATABASE'))); | 
					
						
							| 
									
										
										
										
											2020-11-02 06:20:49 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-11-17 13:18:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |