-
Notifications
You must be signed in to change notification settings - Fork 23
/
phinx.php
98 lines (87 loc) · 3.19 KB
/
phinx.php
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
<?php
declare(strict_types=1);
use App\Infra\Module\Phinx\PhinxLoad;
use Leevel\Di\Container;
use Leevel\Kernel\App;
use Symfony\Component\Console\Input\ArgvInput;
// 加载 Composer
require __DIR__.'/vendor/autoload.php';
// 创建应用
$container = Container::singletons();
$app = new App($container, (string) realpath(__DIR__));
// 载入环境
$input = new ArgvInput();
if ($input->hasParameterOption('--env')) {
$env = $input->getParameterOption('--env');
} else {
$env = 'env';
}
putenv('RUNTIME_ENVIRONMENT='.$env);
// 选择数据库
if ($input->hasParameterOption('--database')) {
$development = $input->getParameterOption('--database');
} else {
$development = 'development';
}
if ($input->hasParameterOption('--database-index')) {
$databaseIndex = (string) $input->getParameterOption('--database-index');
} else {
$databaseIndex = '';
}
if (!in_array($development, [
'production',
'production_common',
'development',
'development_common',
], true)) {
throw new \Exception('Database must be one of `production,production_common,development,development_common`.');
}
// 读取配置
(new PhinxLoad())->handle($app);
$migrationPath = str_contains($development, '_common') ? 'common' : 'data';
return [
'paths' => [
'migrations' => "database/{$migrationPath}/migrations",
'seeds' => "database/{$migrationPath}/seeds",
],
'environments' => [
'default_migration_table' => 'phinx_log',
'default_environment' => $development,
'production' => [
'adapter' => 'mysql',
'host' => Leevel::env('DATABASE_HOST', 'localhost'),
'name' => Leevel::env('DATABASE_NAME', '').$databaseIndex,
'user' => Leevel::env('DATABASE_USER', 'root'),
'pass' => Leevel::env('DATABASE_PASSWORD', ''),
'port' => Leevel::env('DATABASE_PORT', 3306),
'charset' => 'utf8',
],
'production_common' => [
'adapter' => 'mysql',
'host' => Leevel::env('DATABASE_COMMON_HOST', 'localhost'),
'name' => Leevel::env('DATABASE_COMMON_NAME', ''),
'user' => Leevel::env('DATABASE_COMMON_USER', 'root'),
'pass' => Leevel::env('DATABASE_COMMON_PASSWORD', ''),
'port' => Leevel::env('DATABASE_COMMON_PORT', 3306),
'charset' => 'utf8',
],
'development' => [
'adapter' => 'mysql',
'host' => Leevel::env('DATABASE_HOST', 'localhost'),
'name' => Leevel::env('DATABASE_NAME', '').$databaseIndex,
'user' => Leevel::env('DATABASE_USER', 'root'),
'pass' => Leevel::env('DATABASE_PASSWORD', ''),
'port' => Leevel::env('DATABASE_PORT', 3306),
'charset' => 'utf8',
],
'development_common' => [
'adapter' => 'mysql',
'host' => Leevel::env('DATABASE_COMMON_HOST', 'localhost'),
'name' => Leevel::env('DATABASE_COMMON_NAME', ''),
'user' => Leevel::env('DATABASE_COMMON_USER', 'root'),
'pass' => Leevel::env('DATABASE_COMMON_PASSWORD', ''),
'port' => Leevel::env('DATABASE_COMMON_PORT', 3306),
'charset' => 'utf8',
],
],
];