clean peers + snatched table unique

This commit is contained in:
xiaomlove
2023-04-02 02:42:45 +08:00
parent caaf140beb
commit 4703cf7b28
7 changed files with 133 additions and 16 deletions

View File

@@ -27,10 +27,10 @@ return new class extends Migration
DB::statement($sql);
}
$sql = "alter table peers add index idx_torrent_peer(`torrent`, `peer_id`(20))";
$sql = "alter table $tableName add index idx_torrent_peer(`torrent`, `peer_id`(20))";
DB::statement($sql);
$sql = "alter table peers add index idx_peer(`peer_id`(20))";
$sql = "alter table $tableName add index idx_peer(`peer_id`(20))";
DB::statement($sql);
}

View File

@@ -0,0 +1,49 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$tableName = 'snatched';
$result = DB::select('show index from ' . $tableName);
$indexToDrop = [];
foreach ($result as $item) {
if (in_array($item->Column_name, ['torrentid', 'userid'])) {
if ($item->Non_unique == 0) {
return;
}
$indexToDrop[$item->Key_name] = "drop index " . $item->Key_name;
}
}
if (!empty($indexToDrop)) {
$sql = sprintf("alter table %s %s", $tableName, implode(', ', $indexToDrop));
DB::statement($sql);
}
$sql = "alter table $tableName add unique unique_torrent_user(`torrentid`, `userid`)";
DB::statement($sql);
$sql = "alter table $tableName add index idx_user(`userid`)";
DB::statement($sql);
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
};

View File

@@ -0,0 +1,41 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$tableName = 'peers';
$result = DB::select('show index from ' . $tableName);
$toDropIndex = 'idx_torrent_peer';
foreach ($result as $item) {
if ($item->Key_name == $toDropIndex) {
DB::statement("alter table $tableName drop index $toDropIndex");
break;
}
}
DB::statement("alter table $tableName add unique unique_torrent_peer_user(`torrent`, `peer_id`, `userid`)");
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('peers', function (Blueprint $table) {
//
});
}
};