整理 迁移

This commit is contained in:
iVampireSP.com 2023-02-02 20:11:41 +08:00
parent 6c508d0660
commit 4b5ba18b29
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
57 changed files with 152 additions and 1722 deletions

View File

@ -15,16 +15,23 @@ public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->uuid()->nullable()->unique();
$table->string('name')->index();
$table->string('real_name')->nullable();
$table->string('email')->unique();
$table->string('email_md5')->nullable()->comment('邮箱 MD5');
$table->string('id_card')->nullable()->comment('身份证号');
$table->timestamp('email_verified_at')->nullable();
$table->timestamp('real_name_verified_at')->nullable()->index()->comment('实名认证时间');
$table->date('birthday_at')->nullable()->index();
$table->string('password')->nullable();
// 积分(8位小数点)
$table->double('drops', 60, 8)->default(0);
$table->decimal('balance', 20, 4)->default(0);
$table->dateTime('banned_at')->nullable()->index()->comment('封禁时间');
$table->string('banned_reason')->nullable();
$table->unsignedBigInteger('user_group_id')->nullable()->index()->comment('用户组');
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
}

View File

@ -1,36 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::create('failed_jobs', function (Blueprint $table) {
$table->id();
$table->string('uuid')->unique();
$table->text('connection');
$table->text('queue');
$table->longText('payload');
$table->longText('exception');
$table->timestamp('failed_at')->useCurrent();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::dropIfExists('failed_jobs');
}
};

View File

@ -8,8 +8,6 @@
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
@ -27,8 +25,6 @@ public function up(): void
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{

View File

@ -1,34 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('hosts', function (Blueprint $table) {
//
$table->double('managed_price', 60, 8)->index()->nullable()->after('price');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('hosts', function (Blueprint $table) {
// drop
$table->dropColumn('managed_price');
});
}
};

View File

@ -1,33 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('work_order_replies', function (Blueprint $table) {
//
$table->unsignedBigInteger('user_id')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('work_order_replies', function (Blueprint $table) {
//
});
}
};

View File

@ -1,34 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('modules', function (Blueprint $table) {
//
$table->string('url')->nullable()->after('api_token')->index();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('modules', function (Blueprint $table) {
//
$table->dropColumn('url');
});
}
};

View File

@ -1,54 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::dropIfExists('tasks');
Schema::create(
'tasks',
function (Blueprint $table) {
$table->uuid('id')->primary()->unique();
$table->string('title');
// progress (max 100)
$table->integer('progress')->default(0);
// status
$table->enum('status', ['pending', 'done', 'success', 'failed', 'error', 'cancelled', 'processing', 'need_operation'])->index();
// user id
$table->unsignedBigInteger('user_id')->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
// host id
$table->unsignedBigInteger('host_id')->index();
$table->foreign('host_id')->references('id')->on('hosts')->onDelete('cascade');
$table->timestamps();
}
);
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
//
Schema::dropIfExists('tasks');
}
};

View File

@ -1,33 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('hosts', function (Blueprint $table) {
//
$table->timestamp('suspended_at')->nullable()->index()->after('status');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('hosts', function (Blueprint $table) {
//
});
}
};

View File

@ -1,39 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
//
$table->decimal('balance')->default(0)->after('password');
// drop column if exists
if (Schema::hasColumn('users', 'drops')) {
$table->dropColumn('drops');
}
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
//
$table->dropColumn('balance');
});
}
};

View File

@ -1,33 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('hosts', function (Blueprint $table) {
$table->foreign('module_id')->references('id')->on('modules')->cascadeOnUpdate();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('hosts', function (Blueprint $table) {
// rollback
$table->dropForeign(['module_id']);
});
}
};

View File

@ -1,35 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
//
$table->dateTime('banned_at')->nullable()->comment('封禁时间')->after('balance');
// reason
$table->string('banned_reason')->nullable()->after('banned_at');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
//
});
}
};

View File

@ -1,47 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
// if transaction collection exists
if (Schema::connection('mongodb')->hasTable('transactions')) {
return;
}
Schema::connection('mongodb')->create('transactions', function (Blueprint $collection) {
$collection->unsignedBigInteger('user_id')->index();
$collection->unsignedBigInteger(
'type'
)->index();
$collection->unsignedBigInteger('payment')->index();
$collection->unsignedBigInteger(
'module_id'
)->index()->nullable();
$collection->unsignedBigInteger('host_id')->index()->nullable();
// a year
$year = 365 * 24 * 60 * 60;
$collection->expire('created_at', $year);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::connection('mongodb')->dropIfExists('transactions');
}
};

View File

@ -1,38 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('modules', function (Blueprint $table) {
//
$table->string('wecom_key')->nullable()->comment('企业微信机器人 key');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('modules', function (Blueprint $table) {
//
// drop if exists
if (Schema::hasColumn('modules', 'wecom_key')) {
$table->dropColumn('wecom_key');
}
});
}
};

View File

@ -1,33 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('balances', function (Blueprint $table) {
//
$table->decimal('remaining_amount', 10)->default(0)->after('amount');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('balances', function (Blueprint $table) {
//
});
}
};

View File

@ -1,45 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('hosts', function (Blueprint $table) {
//
$table->tinyInteger('hour_at')->index()->nullable()->after('status');
});
echo PHP_EOL.'将开始刷新主机的小时数...';
(new App\Models\Host)->chunk(100, function ($hosts) {
foreach ($hosts as $host) {
$host->hour_at = $host->created_at->hour;
$host->save();
}
});
echo ' 完成!'.PHP_EOL;
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('hosts', function (Blueprint $table) {
//
$table->dropColumn('hour_at');
});
}
};

View File

@ -1,35 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('tasks', function (Blueprint $table) {
//
$table->string('module_id')->nullable()->after('host_id')->index();
$table->foreign('module_id')->references('id')->on('modules')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('tasks', function (Blueprint $table) {
//
});
}
};

View File

@ -1,43 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
// Drop foreign key
Schema::table('tasks', function (Blueprint $table) {
$table->dropForeign('tasks_host_id_foreign');
$table->dropForeign('tasks_user_id_foreign');
$table->dropForeign('tasks_module_id_foreign');
});
// run RAW SQL
DB::statement('ALTER TABLE tasks ENGINE=MEMORY;');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
//
DB::statement('ALTER TABLE tasks ENGINE=InnoDB;');
Schema::table('tasks', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('host_id')->references('id')->on('hosts')->onDelete('cascade');
$table->foreign('module_id')->references('id')->on('modules')->onDelete('cascade');
});
}
};

View File

@ -1,46 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('hosts', function (Blueprint $table) {
//
$table->tinyInteger('minute_at')->index()->nullable()->after('hour_at');
});
echo PHP_EOL.'将开始刷新主机的分钟数...';
(new App\Models\Host)->chunk(100, function ($hosts) {
foreach ($hosts as $host) {
echo '刷新: '.$host->id.PHP_EOL;
$host->minute_at = $host->created_at->minute;
$host->save();
}
});
echo ' 完成!'.PHP_EOL;
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('hosts', function (Blueprint $table) {
//
$table->dropColumn('minute_at');
});
}
};

View File

@ -1,35 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('modules', function (Blueprint $table) {
//
$table->enum('status', ['up', 'down', 'maintenance'])->index()->default('down')->after('url');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('modules', function (Blueprint $table) {
//
$table->dropColumn('status');
});
}
};

View File

@ -1,73 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::create('user_groups', function (Blueprint $table) {
$table->id();
// 名称
$table->string('name')->comment('名称')->index();
// 颜色
$table->string('color')->comment('颜色')->nullable();
// 优惠百分比
$table->integer('discount')->comment('优惠百分比')->default(100);
// 暂停/终止豁免权
$table->boolean('exempt')->comment('暂停/终止豁免权')->default(false);
$table->timestamps();
});
// Schema::table('users', function (Blueprint $table) {
// $table->unsignedBigInteger('user_group_id')->nullable()->comment('用户组')->index()->after('banned_reason');
// $table->foreign('user_group_id')->references('id')->on('user_groups')->onDelete('set null');
// });
Schema::table('users', function (Blueprint $table) {
$table->unsignedBigInteger('user_group_id')->nullable()->comment('用户组')->index()->after('banned_reason');
$table->foreign('user_group_id')->references('id')->on('user_groups')->onDelete('set null');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
// Schema::table('users', function (Blueprint $table) {
// // drop column if exists
// if (Schema::hasColumn('users', 'user_group_id')) {
// $table->dropForeign('users_user_group_id_foreign');
//
// $table->dropColumn('user_group_id');
// }
// });
//
Schema::table('users', function (Blueprint $table) {
// drop column if exists
if (Schema::hasColumn('users', 'user_group_id')) {
$table->dropForeign('users_user_group_id_foreign');
$table->dropColumn('user_group_id');
}
});
Schema::dropIfExists('user_groups');
}
};

View File

@ -1,37 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('work_orders', function (Blueprint $table) {
//
// 删除外键
$table->dropForeign('work_orders_module_id_foreign');
$table->foreign('module_id')->references('id')->on('modules')->cascadeOnDelete()->cascadeOnUpdate();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('work_orders', function (Blueprint $table) {
//
});
}
};

View File

@ -1,36 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->date('birthday_at')->nullable()->index()->after('email_verified_at');
// 真实姓名
$table->string('real_name')->nullable()->after('name');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('birthday_at');
$table->dropColumn('real_name');
});
}
};

View File

@ -1,39 +0,0 @@
<?php
use App\Models\WorkOrder\WorkOrder;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('work_orders', function (Blueprint $table) {
// uuid
$table->uuid()->nullable()->after('id')->index()->unique();
});
// 为每个工单生成一个 uuid 安静更改
WorkOrder::query()->chunk(100, function ($workOrders) {
foreach ($workOrders as $workOrder) {
$workOrder->uuid = Str::uuid();
$workOrder->saveQuietly();
}
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
}
};

View File

@ -1,48 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('work_order_replies', function (Blueprint $table) {
// name
$table->string('name')->nullable()->after('user_id');
// module_id
$table->string('module_id')->nullable()->after('name')->index();
$table->foreign('module_id')->references('id')->on('modules')->cascadeOnDelete();
});
// 为每个工单回复生成一个 module_id 安静更改
(new App\Models\WorkOrder\Reply)->whereNull('module_id')->with('workOrder')->chunk(100, function ($replies) {
foreach ($replies as $reply) {
$reply->module_id = $reply->workOrder->module_id;
$reply->saveQuietly();
}
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('work_order_replies', function (Blueprint $table) {
Schema::hasColumn('work_order_replies', 'name') && $table->dropColumn('name');
$table->dropForeign(['module_id']);
$table->dropColumn('module_id');
});
}
};

View File

@ -1,32 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('work_order_replies', function (Blueprint $table) {
$table->string('role')->default('user')->comment('回复角色')->after('module_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('work_order_replies', function (Blueprint $table) {
$table->dropColumn('role');
});
}
};

View File

@ -1,32 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('work_orders', function (Blueprint $table) {
$table->boolean('notify')->default(true)->comment('是否通知')->after('status');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('work_orders', function (Blueprint $table) {
$table->dropColumn('notify');
});
}
};

View File

@ -1,44 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->string('email_md5')->after('email')->nullable()->comment('邮箱 MD5');
});
$count = (new App\Models\User)->count();
$i = 0;
(new App\Models\User)->chunk(100, function ($users) use (&$i, $count) {
foreach ($users as $user) {
echo sprintf('Updating %d/%d', ++$i, $count).PHP_EOL;
$user->email_md5 = md5($user->email);
$user->saveQuietly();
}
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('email_md5');
});
}
};

View File

@ -1,46 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->uuid()->unique()->after('id')->nullable();
});
$count = (new App\Models\User)->count();
$i = 0;
(new App\Models\User)->chunk(100, function ($users) use (&$i, $count) {
foreach ($users as $user) {
echo sprintf('Updating %d/%d', ++$i, $count).PHP_EOL;
$user->email_md5 = md5($user->email);
$user->uuid = Str::uuid();
$user->saveQuietly();
}
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('uuid');
});
}
};

View File

@ -1,34 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->string('name')->index()->change();
$table->dateTime('banned_at')->index()->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropIndex('users_name_index');
$table->dropIndex('users_banned_at_index');
});
}
};

View File

@ -1,39 +0,0 @@
<?php
use App\Models\Admin;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('admins', function (Blueprint $table) {
$table->string('name')->after('id')->nullable();
});
$admins = Admin::all();
foreach ($admins as $admin) {
$admin->name = $admin->id;
$admin->save();
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('admins', function (Blueprint $table) {
$table->dropColumn('name');
});
}
};

View File

@ -1,36 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('hosts', function (Blueprint $table) {
// 将 price 和 managed_price 改成 decimal
$table->decimal('price', 10)->change();
$table->decimal('managed_price', 10)->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('hosts', function (Blueprint $table) {
// 回滚
$table->unsignedDouble('price', 10)->change();
$table->unsignedDouble('managed_price', 10)->change();
});
}
};

View File

@ -1,32 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('work_orders', function (Blueprint $table) {
$table->string('ip')->nullable()->after('status');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('work_orders', function (Blueprint $table) {
$table->dropColumn('ip');
});
}
};

View File

@ -1,32 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('work_order_replies', function (Blueprint $table) {
$table->string('ip')->nullable()->after('content');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('work_order_replies', function (Blueprint $table) {
$table->dropColumn('ip');
});
}
};

View File

@ -1,32 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('modules', function (Blueprint $table) {
$table->decimal('balance', 20, 2)->default(0)->after('name');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('modules', function (Blueprint $table) {
$table->dropColumn('balance');
});
}
};

View File

@ -1,34 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->timestamp('real_name_verified_at')->nullable()->comment('实名认证时间')->after('email_verified_at');
$table->string('id_card')->nullable()->comment('身份证号')->after('email_md5');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('real_name_verified_at');
$table->dropColumn('id_card');
});
}
};

View File

@ -1,40 +0,0 @@
<?php
use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\Crypt;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
User::chunk(100, function ($users) {
foreach ($users as $user) {
if (! $user->id_card) {
continue;
}
echo "Encrypting user {$user->id}...".PHP_EOL;
// 设置值(不走模型的 mutator
$user->setAttribute('id_card', Crypt::encryptString($user->id_card));
$user->save();
}
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
echo PHP_EOL.'无法解密用户数据,因为此操作是不可逆的。'.PHP_EOL;
}
};

View File

@ -1,32 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->index('real_name_verified_at');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropIndex('users_real_name_verified_at_index');
});
}
};

View File

@ -1,40 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->decimal('balance', 20, 4)->change();
});
Schema::table('modules', function (Blueprint $table) {
$table->decimal('balance', 20, 4)->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->decimal('balance', 10)->change();
});
Schema::table('modules', function (Blueprint $table) {
$table->decimal('balance', 10)->change();
});
}
};

View File

@ -1,41 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
// 坏,为什么得这么写
Schema::table('tasks', function (Blueprint $table) {
$table->dropColumn('progress');
});
Schema::table('tasks', function (Blueprint $table) {
$table->unsignedTinyInteger('progress')->nullable()->after('title');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('tasks', function (Blueprint $table) {
$table->dropColumn('progress');
});
Schema::table('tasks', function (Blueprint $table) {
$table->integer('progress')->after('title');
});
}
};

View File

@ -1,32 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('work_orders', function (Blueprint $table) {
$table->string('module_id')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('work_orders', function (Blueprint $table) {
$table->string('module_id')->nullable(false)->change();
});
}
};

View File

@ -1,39 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('modules', function (Blueprint $table) {
$table->timestamps();
});
// 刷新时间戳
DB::table('modules')->update([
'created_at' => DB::raw('NOW()'),
'updated_at' => DB::raw('NOW()'),
]);
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('modules', function (Blueprint $table) {
$table->dropColumn('created_at');
$table->dropColumn('updated_at');
});
}
};

View File

@ -1,37 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
\Illuminate\Support\Facades\DB::statement("ALTER TABLE `hosts` CHANGE `status` `status` ENUM('running','stopped','error','suspended','pending','unavailable', 'locked') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'pending';");
\Illuminate\Support\Facades\Schema::table('hosts', function (Blueprint $table) {
$table->timestamp('unavailable_at')->nullable()->comment('不可用时间')->after('suspended_at');
$table->timestamp('locked_at')->nullable()->comment('锁定时间')->after('unavailable_at');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
\Illuminate\Support\Facades\DB::statement("ALTER TABLE `hosts` CHANGE `status` `status` ENUM('running','stopped','error','suspended','pending') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'pending';");
\Illuminate\Support\Facades\Schema::table('hosts', function (Blueprint $table) {
$table->dropColumn('unavailable_at');
$table->dropColumn('locked_at');
});
}
};

View File

@ -15,15 +15,10 @@ public function up(): void
{
Schema::create('admins', function (Blueprint $table) {
$table->id();
// email
$table->string('name')->nullable();
$table->string('email')->unique();
// password
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}

View File

@ -4,8 +4,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
return new class extends Migration {
/**
* Run the migrations.
*
@ -15,13 +14,9 @@ public function up(): void
{
Schema::create('applications', function (Blueprint $table) {
$table->id();
$table->string('name')->index();
$table->string('description')->nullable();
$table->string('api_token')->index()->unique();
$table->string('api_token')->unique();
$table->timestamps();
});
}

View File

@ -15,27 +15,14 @@ public function up(): void
{
Schema::create('balances', function (Blueprint $table) {
$table->id();
// order id
$table->string('order_id')->nullable()->index();
// trade id
$table->string('trade_id')->nullable()->index();
// payment
$table->string('payment')->nullable()->index();
// amount
$table->decimal('amount', 10)->default(0);
// paid_at
$table->decimal('remaining_amount', 10)->default(0);
$table->timestamp('paid_at')->nullable();
// user id
$table->unsignedBigInteger('user_id')->nullable()->index();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
$table->foreign(['user_id'])->references(['id'])->on('users')->onUpdate('NO ACTION')->onDelete('NO ACTION');
});
}

View File

@ -14,9 +14,10 @@
public function up(): void
{
Schema::create('notifications', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->uuid()->primary();
$table->string('type');
$table->morphs('notifiable');
$table->string('notifiable_type')->index();
$table->unsignedBigInteger('notifiable_id')->index();
$table->text('data');
$table->timestamp('read_at')->nullable();
$table->timestamps();

View File

@ -14,24 +14,15 @@
public function up(): void
{
Schema::create('modules', function (Blueprint $table) {
$table->string('id')->index()->primary()->unique();
$table->string('id')->primary();
$table->string('name')->index();
// api token
$table->string('api_token')->nullable()->unique()->index();
$table->decimal('balance', 20, 4)->default(0)->index();
$table->string('api_token')->nullable()->unique();
$table->string('url')->nullable()->index();
$table->enum('status', ['up', 'down', 'maintenance'])->default('down')->index();
$table->timestamps();
$table->string('wecom_key')->nullable()->comment('企业微信机器人 key');
});
// if env is local
if (config('app.env') == 'local') {
$module = [
'id' => 'test',
'name' => 'Example Module',
'api_token' => '123456',
];
(new App\Models\Module)->create($module);
}
}
/**

View File

@ -4,8 +4,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
return new class extends Migration {
/**
* Run the migrations.
*
@ -17,12 +16,11 @@ public function up(): void
$table->id();
$table->string('module_id')->index();
$table->foreign('module_id')->references('id')->on('modules')->cascadeOnDelete()->cascadeOnUpdate();
$table->string('allowed_module_id')->index();
$table->foreign('allowed_module_id')->references('id')->on('modules')->cascadeOnDelete()->cascadeOnUpdate();
$table->timestamps();
$table->foreign(['allowed_module_id'])->references(['id'])->on('modules')->onUpdate('CASCADE')->onDelete('CASCADE');
$table->foreign(['module_id'])->references(['id'])->on('modules')->onUpdate('CASCADE')->onDelete('CASCADE');
});
}

View File

@ -15,31 +15,23 @@ public function up(): void
{
Schema::create('hosts', function (Blueprint $table) {
$table->id();
// name
$table->string('name')->index();
// provider id
$table->string('module_id')->index();
// $table->foreign('module_id')->references('id')->on('modules')->onDelete('set null');
// user_id
$table->unsignedBigInteger('user_id')->index();
$table->foreign('user_id')->references('id')->on('users');
// price
$table->double('price', 60, 8)->index();
// config
$table->decimal('price', 10)->index();
$table->decimal('managed_price', 10)->nullable()->index();
$table->json('configuration')->nullable();
// status
$table->enum('status', ['running', 'stopped', 'error', 'suspended', 'pending'])->default('pending')->index();
// soft delete
$table->enum('status', ['running', 'stopped', 'error', 'suspended', 'pending', 'unavailable', 'locked'])->default('pending')->index();
$table->tinyInteger('hour_at')->nullable()->index();
$table->tinyInteger('minute_at')->nullable()->index();
$table->timestamp('suspended_at')->nullable()->index();
$table->timestamp('unavailable_at')->nullable()->comment('不可用时间');
$table->timestamp('locked_at')->nullable()->comment('锁定时间');
$table->softDeletes();
$table->timestamps();
$table->foreign(['module_id'])->references(['id'])->on('modules')->onUpdate('CASCADE')->onDelete('NO ACTION');
$table->foreign(['user_id'])->references(['id'])->on('users')->onUpdate('NO ACTION')->onDelete('NO ACTION');
});
}

View File

@ -1,38 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
// if it has column `deleted_at`, skip
if (! Schema::hasColumn('users', 'deleted_at')) {
$table->softDeletes();
}
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
// if it has column `deleted_at`, drop it
if (Schema::hasColumn('users', 'deleted_at')) {
$table->dropSoftDeletes();
}
});
}
};

View File

@ -1,32 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('modules', function (Blueprint $table) {
$table->index('balance');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('modules', function (Blueprint $table) {
$table->dropIndex('balance');
});
}
};

View File

@ -0,0 +1,40 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::create('tasks', function (Blueprint $table) {
$table->uuid()->primary();
$table->string('title');
$table->unsignedTinyInteger('progress')->nullable();
$table->enum('status', ['pending', 'done', 'success', 'failed', 'error', 'cancelled', 'processing', 'need_operation'])->index();
$table->unsignedBigInteger('user_id')->index();
$table->unsignedBigInteger('host_id')->index();
$table->string('module_id')->nullable()->index();
$table->timestamps();
});
// 设置存储引擎为 MEMORY
DB::statement('ALTER TABLE tasks ENGINE = MEMORY');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::dropIfExists('tasks');
}
};

View File

@ -0,0 +1,42 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::create('user_groups', function (Blueprint $table) {
$table->id();
$table->string('name')->index()->comment('名称');
$table->string('color')->nullable()->comment('颜色');
$table->integer('discount')->default(100)->comment('优惠百分比');
$table->boolean('exempt')->default(false)->comment('暂停/终止豁免权');
$table->timestamps();
});
Schema::table('users', function (Blueprint $table) {
$table->foreign(['user_group_id'])->references(['id'])->on('user_groups')->onUpdate('NO ACTION')->onDelete('SET NULL');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropForeign('users_user_group_id_foreign');
});
Schema::dropIfExists('user_groups');
}
};

View File

@ -16,28 +16,20 @@ public function up(): void
Schema::create('work_orders', function (Blueprint $table) {
$table->id();
// title
$table->uuid()->nullable()->unique();
$table->string('title')->index();
// content
$table->text('content');
// user id
$table->unsignedBigInteger('user_id')->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
// module id
$table->string('module_id')->index();
$table->foreign('module_id')->references('id')->on('modules')->onDelete('cascade');
// host id
$table->unsignedBigInteger('host_id')->index()->nullable();
$table->foreign('host_id')->references('id')->on('hosts')->onDelete('cascade');
// status
$table->string('module_id')->nullable()->index();
$table->unsignedBigInteger('host_id')->nullable()->index();
$table->enum('status', ['open', 'user_read', 'closed', 'user_replied', 'replied', 'read', 'on_hold', 'in_progress', 'error', 'pending'])->default('pending')->index();
$table->string('ip')->nullable();
$table->boolean('notify')->default(true)->comment('是否通知');
$table->timestamps();
$table->foreign(['host_id'])->references(['id'])->on('hosts')->onUpdate('NO ACTION')->onDelete('CASCADE');
$table->foreign(['module_id'])->references(['id'])->on('modules')->onUpdate('CASCADE')->onDelete('CASCADE');
$table->foreign(['user_id'])->references(['id'])->on('users')->onUpdate('NO ACTION')->onDelete('CASCADE');
});
}

View File

@ -4,8 +4,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
return new class extends Migration {
/**
* Run the migrations.
*
@ -16,20 +15,19 @@ public function up(): void
Schema::create('work_order_replies', function (Blueprint $table) {
$table->id();
// content
$table->text('content');
// work_order id (on delete cascade)
$table->string('ip')->nullable();
$table->unsignedBigInteger('work_order_id')->index();
$table->foreign('work_order_id')->references('id')->on('work_orders')->onDelete('cascade');
// user id
$table->unsignedBigInteger('user_id')->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->unsignedBigInteger('user_id')->nullable()->index();
$table->string('name')->nullable();
$table->string('module_id')->nullable()->index();
$table->string('role')->default('user')->comment('回复角色');
$table->boolean('is_pending')->default(false)->index();
$table->timestamps();
$table->foreign(['module_id'])->references(['id'])->on('modules')->onUpdate('NO ACTION')->onDelete('CASCADE');
$table->foreign(['user_id'])->references(['id'])->on('users')->onUpdate('NO ACTION')->onDelete('CASCADE');
$table->foreign(['work_order_id'])->references(['id'])->on('work_orders')->onUpdate('NO ACTION')->onDelete('CASCADE');
});
}

View File

@ -4,9 +4,9 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTagTables extends Migration
return new class extends Migration
{
public function up()
public function up(): void
{
Schema::create('tags', function (Blueprint $table) {
$table->id();
@ -27,4 +27,10 @@ public function up()
$table->unique(['tag_id', 'taggable_id', 'taggable_type']);
});
}
}
public function down(): void
{
Schema::dropIfExists('taggables');
Schema::dropIfExists('tags');
}
};