-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathDatabase.php
executable file
·130 lines (120 loc) · 3.67 KB
/
Database.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
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
<?php
/*
* This file is part of the phpems/phpems.
*
* (c) oiuv <[email protected]>
*
* 项目维护:oiuv(QQ:7300637) | 定制服务:火眼(QQ:278768688)
*
* This source file is subject to the MIT license that is bundled.
*/
require __DIR__.'/../vendor/autoload.php';
use Illuminate\Database\Capsule\Manager as DB;
class Database
{
// 运行迁移
public static function migrate()
{
$table = 'test_table';
// 判断数据表是否存在
if (DB::schema()->hasTable($table)) {
// 在已有数据表上创建字段
DB::schema()->table($table, function ($table) {
$table->string('mobile')->after('email');
});
} else {
// 创建数据表
DB::schema()->create($table, function ($table) {
$table->increments('id');
$table->string('email')->unique();
$table->timestamps();
});
}
}
// 回滚迁移
public static function rollback()
{
$table = 'test_table';
// 删除数据表
DB::schema()->dropIfExists($table);
// 删除数据表中的字段
// if (DB::schema()->hasTable($table)) {
// DB::schema()->table($table, function ($table) {
// $table->dropColumn(['mobile']);
// });
// }
}
// 重命名数据表
public static function rename()
{
$table = 'test_table';
if (DB::schema()->hasTable($table)) {
DB::schema()->rename($table, 'test_demo');
}
}
// 升级数据库
public static function up()
{
// v6.0 to v6.1
$table = 'certificate';
// 判断数据表是否存在
if (DB::schema()->hasTable($table)) {
// 在已有数据表上创建字段
DB::schema()->table($table, function ($table) {
$table->integer('cedays')->after('cetime')->nullable();
});
} else {
// 数据表不是v6.0版本?
exit('数据库版本不对?请手动校验后升级。');
}
$table = 'content';
// 判断数据表是否存在
if (DB::schema()->hasTable($table)) {
// 在已有数据表上创建字段
DB::schema()->table($table, function ($table) {
$table->integer('contentview')->default(0);
$table->dropColumn('news_title');
});
} else {
// 数据表不是v6.0版本?
exit('数据库版本不对?请手动校验后升级。');
}
}
// 降级数据库
public static function down()
{
// todo
}
}
if (isset($_GET['action'])) {
switch ($_GET['action']) {
case 'migrate':
try {
Database::migrate();
echo '数据库迁移成功';
} catch (PDOException $exception) {
echo $exception->getMessage();
}
break;
case 'rollback':
try {
Database::rollback();
echo '数据库滚回成功';
} catch (PDOException $exception) {
echo $exception->getMessage();
}
break;
case 'update':
try {
Database::up();
echo '数据库升级成功';
} catch (PDOException $exception) {
echo $exception->getMessage();
}
break;
default:
echo '没有数据库迁移操作';
}
} else {
echo "<a href='https://learnku.com/docs/laravel/6.x/migrations/5173#tables' target='_blank'>数据库迁移操作指南</a>";
}