Repository navigation
Expand file tree
/
Copy pathCreateDatabase.php
More file actions
143 lines (112 loc) · 4.43 KB
/
Copy pathCreateDatabase.php
File metadata and controls
143 lines (112 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Commands\Database;
use CodeIgniter\CLI\AbstractCommand;
use CodeIgniter\CLI\Attributes\Command;
use CodeIgniter\CLI\Input\Argument;
use CodeIgniter\CLI\Input\Option;
use CodeIgniter\Config\Factories;
use CodeIgniter\Database\SQLite3\Connection as SQLite3Connection;
use Config\Database;
use Throwable;
/**
* Creates a new database.
*/
#[Command(name: 'db:create', description: 'Create a new database schema.', group: 'Database')]
class CreateDatabase extends AbstractCommand
{
/**
* @var list<string>
*/
private const VALID_EXTENSIONS = ['db', 'sqlite'];
protected function configure(): void
{
$this
->addArgument(new Argument(
name: 'db_name',
description: 'The database name to use.',
required: true,
))
->addOption(new Option(
name: 'ext',
description: 'File extension of the database file for SQLite3. Can be `db` or `sqlite`.',
requiresValue: true,
default: 'db',
));
}
protected function interact(array &$arguments, array &$options): void
{
if ($arguments === []) {
$arguments[] = $this->getPrompt()->ask('Database name', null, 'required');
}
$ext = $this->getUnboundOption('ext', $options);
if (is_string($ext) && ! in_array($ext, self::VALID_EXTENSIONS, true)) {
$options['ext'] = $this->getPrompt()->select(
'Please choose a valid file extension',
array_combine(self::VALID_EXTENSIONS, self::VALID_EXTENSIONS),
);
}
}
protected function execute(array $arguments, array $options): int
{
$name = $arguments['db_name'];
assert(is_string($name));
$output = $this->getOutput();
try {
$config = config(Database::class);
// Set to an empty database to prevent connection errors.
$group = service('environment')->isTesting() ? 'tests' : $config->defaultGroup;
$config->{$group}['database'] = '';
$db = Database::connect();
if ($db instanceof SQLite3Connection) {
$ext = $options['ext'];
assert(is_string($ext));
if (! in_array($ext, self::VALID_EXTENSIONS, true)) {
$output->error(sprintf('Invalid file extension "%s". Use either `db` or `sqlite`.', $ext), 'light_gray', 'red');
return EXIT_ERROR;
}
if ($name !== ':memory:') {
$name = str_replace(['.db', '.sqlite'], '', $name) . ".{$ext}";
}
$config->{$group}['DBDriver'] = 'SQLite3';
$config->{$group}['database'] = $name;
if ($name !== ':memory:') {
$dbName = str_contains($name, DIRECTORY_SEPARATOR) ? $name : WRITEPATH . $name;
if (is_file($dbName)) {
$output->error("Database \"{$dbName}\" already exists.", 'light_gray', 'red');
return EXIT_ERROR;
}
unset($dbName);
}
// Connect to new SQLite3 to create new database
$db = Database::connect(null, false);
$db->connect();
if (! is_file($db->getDatabase()) && $name !== ':memory:') {
// @codeCoverageIgnoreStart
$output->error('Database creation failed.', 'light_gray', 'red');
return EXIT_ERROR;
// @codeCoverageIgnoreEnd
}
} elseif (! Database::forge()->createDatabase($name)) {
$output->error('Database creation failed.', 'light_gray', 'red');
return EXIT_ERROR;
}
$output->write("Database \"{$name}\" successfully created.", 'green');
return EXIT_SUCCESS;
} catch (Throwable $e) {
$this->renderThrowable($e);
return EXIT_ERROR;
} finally {
Factories::reset('config');
Database::connect(null, false);
}
}
}