添加 数据库版本判断来规避类型错误

This commit is contained in:
兔姬桑
2022-09-20 19:48:20 +08:00
parent 87be2cfe4b
commit 779a059d36
9 changed files with 44 additions and 306 deletions

View File

@@ -0,0 +1,17 @@
<?php
namespace App\Components;
use DB;
class MigrationToolBox
{
public function versionCheck(): bool
{
$dbVersion = DB::select('select version()')[0]->{'version()'};
$dbType = strpos($dbVersion, 'Maria');
$dbVersion = mb_substr($dbVersion, 0, 6);
return ($dbType !== false && version_compare($dbVersion, '10.2.7', '>=')) || ($dbType === false && version_compare($dbVersion, '5.7.8', '>='));
}
}

View File

@@ -1,5 +1,6 @@
<?php
use App\Components\MigrationToolBox;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
@@ -19,7 +20,11 @@ class CreateReferralApplyTable extends Migration
$table->unsignedInteger('before')->default(0)->comment('操作前可提现金额,单位分');
$table->unsignedInteger('after')->default(0)->comment('操作后可提现金额,单位分');
$table->unsignedInteger('amount')->default(0)->comment('本次提现金额,单位分');
$table->json('link_logs')->comment('关联返利日志ID例如1,3,4');
if ((new MigrationToolBox())->versionCheck()) {
$table->json('link_logs')->comment('关联返利日志ID例如1,3,4');
} else {
$table->text('link_logs')->comment('关联返利日志ID例如1,3,4');
}
$table->boolean('status')->default(0)->comment('状态:-1-驳回、0-待审核、1-审核通过待打款、2-已打款');
$table->dateTime('created_at')->comment('创建时间');
$table->dateTime('updated_at')->comment('最后更新时间');

View File

@@ -1,5 +1,6 @@
<?php
use App\Components\MigrationToolBox;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
@@ -17,8 +18,14 @@ class CreateRuleGroupTable extends Migration
$table->increments('id');
$table->boolean('type')->default(1)->comment('模式1-阻断、0-放行');
$table->string('name')->comment('分组名称');
$table->json('rules')->nullable()->comment('关联的规则ID多个用,号分隔');
$table->json('nodes')->nullable()->comment('关联的节点ID多个用,号分隔');
if ((new MigrationToolBox())->versionCheck()) {
$table->json('rules')->nullable()->comment('关联的规则ID多个用,号分隔');
$table->json('nodes')->nullable()->comment('关联的节点ID多个用,号分隔');
} else {
$table->text('rules')->nullable()->comment('关联的规则ID多个用,号分隔');
$table->text('nodes')->nullable()->comment('关联的节点ID多个用,号分隔');
}
$table->dateTime('created_at')->comment('创建时间');
$table->dateTime('updated_at')->comment('最后更新时间');
});

View File

@@ -1,5 +1,6 @@
<?php
use App\Components\MigrationToolBox;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
@@ -16,7 +17,11 @@ class CreateUserGroupTable extends Migration
Schema::create('user_group', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->comment('分组名称');
$table->json('nodes')->nullable()->comment('关联的节点ID多个用,号分隔');
if ((new MigrationToolBox())->versionCheck()) {
$table->json('nodes')->nullable()->comment('关联的节点ID多个用,号分隔');
} else {
$table->text('nodes')->nullable()->comment('关联的节点ID多个用,号分隔');
}
});
}

View File

@@ -1,5 +1,6 @@
<?php
use App\Components\MigrationToolBox;
use App\Models\Config;
use App\Models\Node;
use Illuminate\Database\Migrations\Migration;
@@ -25,7 +26,11 @@ class ImproveNodeTable extends Migration
// 插入新字段
Schema::table('node', function (Blueprint $table) {
$table->json('profile')->comment('节点设置选项')->after('description');
if ((new MigrationToolBox())->versionCheck()) {
$table->json('profile')->comment('节点设置选项')->after('description');
} else {
$table->text('profile')->comment('节点设置选项')->after('description');
}
$table->unsignedInteger('relay_node_id')->nullable()->comment('中转节点对接母节点, 默认NULL')->after('is_relay');
});

View File

@@ -1,38 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateReferralApplyTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('referral_apply', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id')->comment('用户ID');
$table->unsignedInteger('before')->default(0)->comment('操作前可提现金额,单位分');
$table->unsignedInteger('after')->default(0)->comment('操作后可提现金额,单位分');
$table->unsignedInteger('amount')->default(0)->comment('本次提现金额,单位分');
$table->text('link_logs')->comment('关联返利日志ID例如1,3,4');
$table->boolean('status')->default(0)->comment('状态:-1-驳回、0-待审核、1-审核通过待打款、2-已打款');
$table->dateTime('created_at')->comment('创建时间');
$table->dateTime('updated_at')->comment('最后更新时间');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('referral_apply');
}
}

View File

@@ -1,36 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateRuleGroupTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('rule_group', function (Blueprint $table) {
$table->increments('id');
$table->boolean('type')->default(1)->comment('模式1-阻断、0-放行');
$table->string('name')->comment('分组名称');
$table->text('rules')->nullable()->comment('关联的规则ID多个用,号分隔');
$table->text('nodes')->nullable()->comment('关联的节点ID多个用,号分隔');
$table->dateTime('created_at')->comment('创建时间');
$table->dateTime('updated_at')->comment('最后更新时间');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('rule_group');
}
}

View File

@@ -1,32 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUserGroupTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_group', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->comment('分组名称');
$table->text('nodes')->nullable()->comment('关联的节点ID多个用,号分隔');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('user_group');
}
}

View File

@@ -1,195 +0,0 @@
<?php
use App\Models\Config;
use App\Models\Node;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class ImproveNodeTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
protected $configs = [
'stripe_currency',
];
public function up()
{
foreach ($this->configs as $config) {
Config::insertOrIgnore(['name' => $config]);
}
// 插入新字段
Schema::table('node', function (Blueprint $table) {
$table->text('profile')->comment('节点设置选项')->after('description');
$table->unsignedInteger('relay_node_id')->nullable()->comment('中转节点对接母节点, 默认NULL')->after('is_relay');
});
foreach (Node::all() as $node) {
$profile = null;
switch ($node->type) {
case 0:
$profile = [
'method' => $node->method,
];
break;
case 2:
$profile = [
'method' => $node->v2_method,
'v2_alter_id' => $node->v2_alter_id,
'v2_net' => $node->v2_net,
'v2_type' => $node->v2_type,
'v2_host' => $node->v2_host,
'v2_path' => $node->v2_path,
'v2_tls' => $node->v2_tls ? 'tls' : '',
'v2_sni' => $node->v2_sni,
];
break;
case 3:
$profile = [
'allow_insecure' => false,
];
break;
case 1:
case 4:
$profile = [
'method' => $node->method,
'protocol' => $node->protocol,
'obfs' => $node->obfs,
'obfs_param' => $node->obfs_param,
'protocol_param' => $node->protocol_param,
'passwd' => $node->passwd,
];
break;
default:
}
$node->update(['profile' => $profile]);
if ($node->relay_server && $node->relay_port) { // 创建 中转线路
$relayNodeData = [
'type' => 0,
'name' => $node->name.'↔️',
'country_code' => $node->country_code,
'port' => $node->relay_port,
'level' => $node->level,
'rule_group_id' => $node->rule_group_id,
'speed_limit' => $node->speed_limit,
'client_limit' => $node->client_limit,
'description' => $node->description,
'geo' => $node->geo,
'traffic_rate' => $node->traffic_rate,
'relay_node_id' => $node->id,
'is_udp' => $node->is_udp,
'push_port' => $node->push_port,
'detection_type' => $node->detection_type,
'sort' => $node->sort,
'status' => $node->status,
];
if (filter_var($node->relay_server, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$relayNodeData['ip'] = $node->relay_server;
} elseif (filter_var($node->relay_server, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
$relayNodeData['ipv6'] = $node->relay_server;
} else {
$relayNodeData['server'] = $node->relay_server;
$ip = gethostbyname($node->relay_server);
if ($ip) {
$relayNodeData['ip'] = $ip;
}
}
Node::create($relayNodeData);
}
}
// 销毁老字段
Schema::table('node', function (Blueprint $table) {
$table->dropColumn('relay_server', 'relay_port', 'method', 'protocol', 'protocol_param', 'obfs', 'obfs_param', 'compatible', 'single', 'passwd', 'v2_alter_id',
'v2_method', 'v2_net', 'v2_type', 'v2_host', 'v2_path', 'v2_tls', 'v2_sni', 'tls_provider', 'is_relay');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Config::destroy($this->configs);
Schema::table('node', function (Blueprint $table) { // 回滚老字段
$table->string('relay_server')->nullable()->comment('中转地址');
$table->unsignedSmallInteger('relay_port')->nullable()->comment('中转端口');
$table->string('method', 32)->default('aes-256-cfb')->comment('加密方式');
$table->string('protocol', 64)->default('origin')->comment('协议');
$table->string('protocol_param', 128)->nullable()->comment('协议参数');
$table->string('obfs', 64)->default('plain')->comment('混淆');
$table->string('obfs_param')->nullable()->comment('混淆参数');
$table->boolean('compatible')->default(0)->comment('兼容SS');
$table->boolean('single')->default(0)->comment('启用单端口功能0-否、1-是');
$table->string('passwd')->nullable()->comment('单端口的连接密码');
$table->unsignedSmallInteger('v2_alter_id')->default(16)->comment('V2Ray额外ID');
$table->string('v2_method', 32)->default('aes-128-gcm')->comment('V2Ray加密方式');
$table->string('v2_net', 16)->default('tcp')->comment('V2Ray传输协议');
$table->string('v2_type', 32)->default('none')->comment('V2Ray伪装类型');
$table->string('v2_host')->nullable()->comment('V2Ray伪装的域名');
$table->string('v2_path')->nullable()->comment('V2Ray的WS/H2路径');
$table->boolean('v2_tls')->default(0)->comment('V2Ray连接TLS0-未开启、1-开启');
$table->string('v2_sni', 191)->nullable()->comment('V2Ray的SNI配置');
$table->text('tls_provider')->nullable()->comment('V2Ray节点的TLS提供商授权信息');
$table->boolean('is_relay')->default(0)->comment('是否中转节点0-否、1-是');
});
foreach (Node::all() as $node) {
if ($node->relay_node_id) { // 回滚中转节点
$pNode = Node::find($node->relay_node_id);
$pNode->is_relay = 1;
$pNode->relay_server = $node->server ?: $node->ip;
$pNode->relay_port = $node->port;
$pNode->save();
try {
$node->delete();
} catch (Exception $e) {
Log::emergency('中转删除失败,请手动在数据库中删除; '.$e->getMessage());
}
continue;
}
switch ($node->type) { // 回滚节点配置
case 0:
$node->method = $node->profile['method'];
break;
case 2:
$node->v2_method = $node->profile['method'];
$node->v2_alter_id = $node->profile['v2_alter_id'];
$node->v2_net = $node->profile['v2_net'];
$node->v2_type = $node->profile['v2_type'];
$node->v2_host = $node->profile['v2_host'];
$node->v2_path = $node->profile['v2_path'];
$node->v2_tls = $node->profile['v2_tls'] ? 1 : 0;
$node->v2_sni = $node->profile['v2_sni'];
break;
case 1:
case 4:
$node->method = $node->profile['method'];
$node->protocol = $node->profile['protocol'];
$node->obfs = $node->profile['obfs'];
$node->obfs_param = $node->profile['obfs_param'];
$node->protocol_param = $node->profile['protocol_param'];
$node->single = $node->profile['passwd'] ? 1 : 0;
$node->passwd = $node->profile['passwd'];
break;
default:
}
$node->save();
}
// 回滚新字段
Schema::table('node', function (Blueprint $table) {
$table->dropColumn('profile', 'relay_node_id');
});
}
}