update migrations to support pg

This commit is contained in:
xiaomlove
2026-04-13 14:17:19 +07:00
parent f271e28b15
commit 4d4af87dc9
13 changed files with 104 additions and 56 deletions
@@ -27,7 +27,6 @@ class CreatePostsTable extends Migration
$table->dateTime('editdate')->nullable(); $table->dateTime('editdate')->nullable();
$table->index(['topicid', 'id'], 'topicid_id'); $table->index(['topicid', 'id'], 'topicid_id');
}); });
\Illuminate\Support\Facades\DB::statement('alter table posts add fulltext body(body)');
} }
/** /**
@@ -24,7 +24,12 @@ class CreateTorrentsCustomFieldValuesTable extends Migration
$table->dateTime('created_at'); $table->dateTime('created_at');
$table->dateTime('updated_at'); $table->dateTime('updated_at');
}); });
\Illuminate\Support\Facades\DB::statement('alter table torrents_custom_field_values add index(custom_field_value(191))'); if (\Nexus\Database\NexusDB::isMysql()) {
\Illuminate\Support\Facades\DB::statement('alter table torrents_custom_field_values add index(custom_field_value(191))');
} else if (Nexus\Database\NexusDB::isPgsql()) {
\Illuminate\Support\Facades\DB::statement('alter table torrents_custom_field_values left(custom_field_value,191)');
}
} }
/** /**
@@ -18,6 +18,7 @@ class CreateTorrentsTable extends Migration
} }
Schema::create('torrents', function (Blueprint $table) { Schema::create('torrents', function (Blueprint $table) {
$table->mediumIncrements('id'); $table->mediumIncrements('id');
$table->binary('info_hash', 20)->nullable()->unique();
$table->string('name')->default('')->index('name'); $table->string('name')->default('')->index('name');
$table->string('filename')->default(''); $table->string('filename')->default('');
$table->string('save_as')->default(''); $table->string('save_as')->default('');
@@ -58,14 +59,13 @@ class CreateTorrentsTable extends Migration
$table->dateTime('picktime')->nullable(); $table->dateTime('picktime')->nullable();
$table->dateTime('last_reseed')->nullable(); $table->dateTime('last_reseed')->nullable();
$table->mediumText('pt_gen')->nullable(); $table->mediumText('pt_gen')->nullable();
// $table->integer('tags')->default(0);
$table->text('technical_info')->nullable(); $table->text('technical_info')->nullable();
$table->index(['visible', 'pos_state', 'id'], 'visible_pos_id'); $table->index(['visible', 'pos_state', 'id'], 'visible_pos_id');
$table->index(['category', 'visible', 'banned'], 'category_visible_banned'); $table->index(['category', 'visible', 'banned'], 'category_visible_banned');
$table->index(['visible', 'banned', 'pos_state', 'id'], 'visible_banned_pos_id'); $table->index(['visible', 'banned', 'pos_state', 'id'], 'visible_banned_pos_id');
}); });
$sql = 'alter table torrents add column `info_hash` binary(20) NOT NULL after id, add unique info_hash(`info_hash`)'; // $sql = 'alter table torrents add column `info_hash` binary(20) NOT NULL after id, add unique info_hash(`info_hash`)';
\Illuminate\Support\Facades\DB::statement($sql); // \Illuminate\Support\Facades\DB::statement($sql);
} }
/** /**
@@ -13,6 +13,10 @@ return new class extends Migration
*/ */
public function up() public function up()
{ {
if (\Nexus\Database\NexusDB::isPgsql()) {
//fresh install no need
return;
}
$tableFields = \App\Repositories\UpgradeRepository::DATETIME_INVALID_VALUE_FIELDS; $tableFields = \App\Repositories\UpgradeRepository::DATETIME_INVALID_VALUE_FIELDS;
foreach ($tableFields as $table => $fields) { foreach ($tableFields as $table => $fields) {
@@ -15,23 +15,23 @@ return new class extends Migration
public function up() public function up()
{ {
$tableName = 'peers'; $tableName = 'peers';
$result = DB::select('show index from ' . $tableName); $columnNames = ['torrent', 'peer_id'];
$indexToDrop = []; // 1. 获取该表所有的索引信息
foreach ($result as $item) { $indexesToDelete = \Nexus\Database\NexusDB::listColumnIndexNames($tableName, $columnNames);
if (in_array($item->Column_name, ['torrent', 'peer_id'])) {
$indexToDrop[$item->Key_name] = "drop index " . $item->Key_name; // 3. 执行删除操作
Schema::table($tableName, function (Blueprint $table) use ($indexesToDelete) {
foreach ($indexesToDelete as $indexName) {
// 如果是主键,需要单独处理
if ($indexName === 'primary') {
$table->dropPrimary();
} else {
$table->dropIndex($indexName);
}
} }
} $table->index(['torrent', 'peer_id']);
if (!empty($indexToDrop)) { $table->index('peer_id');
$sql = sprintf("alter table %s %s", $tableName, implode(', ', $indexToDrop)); });
DB::statement($sql);
}
$sql = "alter table $tableName add index idx_torrent_peer(`torrent`, `peer_id`(20))";
DB::statement($sql);
$sql = "alter table $tableName add index idx_peer(`peer_id`(20))";
DB::statement($sql);
} }
@@ -4,6 +4,7 @@ use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Nexus\Database\NexusDB;
return new class extends Migration return new class extends Migration
{ {
@@ -15,26 +16,24 @@ return new class extends Migration
public function up() public function up()
{ {
$tableName = 'snatched'; $tableName = 'snatched';
$result = DB::select('show index from ' . $tableName); $columnNames = ['torrentid', 'userid'];
$indexToDrop = []; // 1. 获取该表所有的索引信息
foreach ($result as $item) { $indexesToDelete = NexusDB::listColumnIndexNames($tableName, $columnNames);
if (in_array($item->Column_name, ['torrentid', 'userid'])) {
if ($item->Non_unique == 0) { // 3. 执行删除操作
return; Schema::table($tableName, function (Blueprint $table) use ($indexesToDelete) {
foreach ($indexesToDelete as $indexName) {
// 如果是主键,需要单独处理
if ($indexName === 'primary') {
$table->dropPrimary();
} else {
$table->dropIndex($indexName);
} }
$indexToDrop[$item->Key_name] = "drop index " . $item->Key_name;
} }
} $table->unique(['torrentid', 'userid']);
if (!empty($indexToDrop)) { $table->index('userid');
$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);
} }
/** /**
@@ -15,15 +15,24 @@ return new class extends Migration
public function up() public function up()
{ {
$tableName = 'peers'; $tableName = 'peers';
$result = DB::select('show index from ' . $tableName); $columnNames = ['torrent', 'peer_id', 'userid'];
$toDropIndex = 'idx_torrent_peer'; // 1. 获取该表所有的索引信息
foreach ($result as $item) { $indexesToDelete = \Nexus\Database\NexusDB::listColumnIndexNames($tableName, $columnNames);
if ($item->Key_name == $toDropIndex) {
DB::statement("alter table $tableName drop index $toDropIndex"); // 3. 执行删除操作
break; Schema::table($tableName, function (Blueprint $table) use ($indexesToDelete) {
foreach ($indexesToDelete as $indexName) {
// 如果是主键,需要单独处理
if ($indexName === 'primary') {
$table->dropPrimary();
} else {
$table->dropIndex($indexName);
}
} }
} $table->unique(['torrent', 'peer_id', 'userid']);
DB::statement("alter table $tableName add unique unique_torrent_peer_user(`torrent`, `peer_id`, `userid`)"); $table->index('peer_id');
$table->index('userid');
});
} }
@@ -13,10 +13,10 @@ return new class extends Migration
*/ */
public function up() public function up()
{ {
$columnInfo = \Nexus\Database\NexusDB::getMysqlColumnInfo("torrents", "cache_stamp"); // $columnInfo = \Nexus\Database\NexusDB::getMysqlColumnInfo("torrents", "cache_stamp");
if ($columnInfo["DATA_TYPE"] == "int") { // if ($columnInfo["DATA_TYPE"] == "int") {
return; // return;
} // }
Schema::table('torrents', function (Blueprint $table) { Schema::table('torrents', function (Blueprint $table) {
$table->integer("cache_stamp")->default(0)->change(); $table->integer("cache_stamp")->default(0)->change();
}); });
@@ -13,8 +13,9 @@ return new class extends Migration
*/ */
public function up() public function up()
{ {
$sql = 'ALTER table torrents MODIFY column `info_hash` binary(20) DEFAULT NULL'; Schema::table('torrents', function (Blueprint $table) {
\Illuminate\Support\Facades\DB::statement($sql); $table->binary('info_hash', 20)->nullable()->change();
});
} }
/** /**
@@ -11,6 +11,9 @@ return new class extends Migration
*/ */
public function up(): void public function up(): void
{ {
if (\Nexus\Database\NexusDB::isPgsql()) {
return;
}
$tableFields = \App\Repositories\UpgradeRepository::DATETIME_INVALID_VALUE_FIELDS; $tableFields = \App\Repositories\UpgradeRepository::DATETIME_INVALID_VALUE_FIELDS;
foreach ($tableFields as $table => $fields) { foreach ($tableFields as $table => $fields) {
+3 -3
View File
@@ -1,7 +1,7 @@
<?php <?php
$config = require ROOT_PATH . 'config/nexus.php'; $dbConfig = nexus_config('nexus.database');
$connectionMysql = $config['mysql']; $config = $dbConfig['connections'][$dbConfig['default']];
\Nexus\Database\NexusDB::bootEloquent($connectionMysql); \Nexus\Database\NexusDB::bootEloquent($config);
+24
View File
@@ -461,6 +461,30 @@ class NexusDB
return nexus_config('nexus.database.default'); return nexus_config('nexus.database.default');
} }
public static function isMysql(): bool
{
return self::getConnectionName() === 'mysql';
}
public static function isPgsql(): bool
{
return self::getConnectionName() === 'pgsql';
}
public static function listColumnIndexNames(string $table, array $columns): array
{
$indexes = Schema::getIndexes($table);
$indexesNames = [];
foreach ($indexes as $index) {
foreach ($columns as $columnName) {
if (in_array($columnName, $index['columns'])) {
$indexesNames[] = $index['name'];
}
}
}
return $indexesNames;
}
} }
+5 -1
View File
@@ -28,7 +28,7 @@ class Install
protected $envNames = [ protected $envNames = [
'TIMEZONE', 'TIMEZONE',
'DB_HOST', 'DB_PORT', 'DB_USERNAME', 'DB_PASSWORD', 'DB_DATABASE', 'DB_CONNECTION', 'DB_HOST', 'DB_PORT', 'DB_USERNAME', 'DB_PASSWORD', 'DB_DATABASE',
'REDIS_HOST', 'REDIS_PORT', 'REDIS_DB', 'REDIS_PASSWORD', 'REDIS_HOST', 'REDIS_PORT', 'REDIS_DB', 'REDIS_PASSWORD',
'UID_STARTS', 'UID_STARTS',
]; ];
@@ -483,6 +483,10 @@ class Install
$item['type'] = 'select'; $item['type'] = 'select';
$item['options'] = $this->listTimeZone(); $item['options'] = $this->listTimeZone();
} }
if ($name == 'DB_CONNECTION') {
$item['type'] = 'select';
$item['options'] = ['mysql', 'pgsql'];
}
$formControls[] = $item; $formControls[] = $item;
} }