-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDatabaseMigrationCommand.php
More file actions
executable file
·105 lines (82 loc) · 3.64 KB
/
DatabaseMigrationCommand.php
File metadata and controls
executable file
·105 lines (82 loc) · 3.64 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
<?php
namespace Leaf\Commands;
use Leaf\Sprout\Command;
class DatabaseMigrationCommand extends Command
{
protected $signature = 'db:migrate
{file? : Migrate a particular file}
{--seed? : Run seeds after migration}';
protected $description = 'Migrate your db schema files';
protected $help = 'Run the migrations defined in the migrations directory';
protected function handle()
{
$fileToMigrate = $this->argument('file');
$migrations = function_exists('AppPaths') ?
glob(getcwd() . DIRECTORY_SEPARATOR . AppPaths('database') . DIRECTORY_SEPARATOR . '*.yml') :
glob(getcwd() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'database' . DIRECTORY_SEPARATOR . '*.yml');
if (empty($migrations)) {
$this->error("No schema files found.");
return 1;
}
foreach ($migrations as $migration) {
$currentFileName = path($migration)->basename();
if ($fileToMigrate && preg_replace('/\.yml$/', '', $currentFileName) !== preg_replace('/\.yml$/', '', $fileToMigrate)) {
continue;
}
$this->writeln("> db migration on <comment>$currentFileName</comment>");
$this->createDatabase($migration);
if (!\Leaf\Schema::migrate($migration)) {
$this->error("Could not migrate $currentFileName");
return 1;
}
if ($this->option('seed')) {
if (!\Leaf\Schema::seed($migration)) {
$this->error("Could not seed $currentFileName");
return 1;
}
$this->writeln("> $currentFileName seeded successfully!");
}
}
$this->info("Database migration completed!\n");
return 0;
}
public function createDatabase(string $migration)
{
$connection = MvcConfig('database')['connections'][
\Leaf\Schema::getConnection($migration) ?? MvcConfig('database')['default']
];
try {
if ($connection['driver'] === 'sqlite') {
if (!file_exists($connection['database'])) {
\Leaf\FS\File::create($connection['database'], null, [
'recursive' => true
]);
}
return 0;
}
if ($connection['host'] !== 'localhost' && $connection['host'] !== '127.0.0.1') {
return 0;
}
db()->addConnections([
'precheck' => [
'dbtype' => $connection['driver'],
'host' => $connection['host'],
'username' => $connection['username'],
'password' => $connection['password'],
'port' => $connection['port'],
]
]);
if ($connection['driver'] === 'pgsql') {
$connectionExists = db('precheck')->query("SELECT 1 FROM pg_database WHERE datname = '{$connection['database']}';")->execute()->fetchColumn();
if (!$connectionExists && db('precheck')->query("CREATE DATABASE {$connection['database']};")->execute()) {
return 0;
}
}
if ($connection['driver'] === 'mysql' && db('precheck')->query("CREATE DATABASE IF NOT EXISTS `{$connection['database']}` CHARACTER SET {$connection['charset']} COLLATE {$connection['collation']};")->execute()) {
return 0;
}
} catch (\Throwable $th) {
$this->error("{$connection['database']} could not be created.\n {$th->getMessage()}");
}
}
}