mirror of
https://github.com/lkddi/Xboard.git
synced 2026-04-15 04:10:51 +08:00
Merge pull request #755 from socksprox/feat/server-id-stat-user
feat: Track user traffic per node (server_id)
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('v2_stat_user', function (Blueprint $table) {
|
||||
// Add server_id column as nullable for backward compatibility
|
||||
$table->integer('server_id')->nullable()->after('user_id')->comment('节点ID (nullable for legacy data)');
|
||||
|
||||
// Add index for per-node queries
|
||||
if (config('database.default') !== 'sqlite') {
|
||||
$table->index(['user_id', 'server_id', 'record_at'], 'user_server_record_idx');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('v2_stat_user', function (Blueprint $table) {
|
||||
if (config('database.default') !== 'sqlite') {
|
||||
$table->dropIndex('user_server_record_idx');
|
||||
}
|
||||
$table->dropColumn('server_id');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* Updates the unique constraint on v2_stat_user to include server_id,
|
||||
* allowing per-node user traffic tracking.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
if (config('database.default') === 'sqlite') {
|
||||
// SQLite uses explicit WHERE queries in code, no constraint changes needed
|
||||
return;
|
||||
}
|
||||
|
||||
Schema::table('v2_stat_user', function (Blueprint $table) {
|
||||
// Drop the old unique constraint that doesn't include server_id
|
||||
$table->dropUnique('server_rate_user_id_record_at');
|
||||
|
||||
// Add new unique constraint including server_id
|
||||
// Note: NULL server_id values (legacy) are treated as distinct in MySQL
|
||||
$table->unique(
|
||||
['user_id', 'server_id', 'server_rate', 'record_at', 'record_type'],
|
||||
'stat_user_unique_idx'
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
if (config('database.default') === 'sqlite') {
|
||||
return;
|
||||
}
|
||||
|
||||
Schema::table('v2_stat_user', function (Blueprint $table) {
|
||||
// Drop new constraint
|
||||
$table->dropUnique('stat_user_unique_idx');
|
||||
|
||||
// Restore original constraint
|
||||
$table->unique(
|
||||
['server_rate', 'user_id', 'record_at'],
|
||||
'server_rate_user_id_record_at'
|
||||
);
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user