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->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('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) {
$table->mediumIncrements('id');
$table->binary('info_hash', 20)->nullable()->unique();
$table->string('name')->default('')->index('name');
$table->string('filename')->default('');
$table->string('save_as')->default('');
@@ -58,14 +59,13 @@ class CreateTorrentsTable extends Migration
$table->dateTime('picktime')->nullable();
$table->dateTime('last_reseed')->nullable();
$table->mediumText('pt_gen')->nullable();
// $table->integer('tags')->default(0);
$table->text('technical_info')->nullable();
$table->index(['visible', 'pos_state', 'id'], 'visible_pos_id');
$table->index(['category', 'visible', 'banned'], 'category_visible_banned');
$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`)';
\Illuminate\Support\Facades\DB::statement($sql);
// $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);
}
/**
@@ -13,6 +13,10 @@ return new class extends Migration
*/
public function up()
{
if (\Nexus\Database\NexusDB::isPgsql()) {
//fresh install no need
return;
}
$tableFields = \App\Repositories\UpgradeRepository::DATETIME_INVALID_VALUE_FIELDS;
foreach ($tableFields as $table => $fields) {
@@ -15,23 +15,23 @@ return new class extends Migration
public function up()
{
$tableName = 'peers';
$result = DB::select('show index from ' . $tableName);
$indexToDrop = [];
foreach ($result as $item) {
if (in_array($item->Column_name, ['torrent', 'peer_id'])) {
$indexToDrop[$item->Key_name] = "drop index " . $item->Key_name;
$columnNames = ['torrent', 'peer_id'];
// 1. 获取该表所有的索引信息
$indexesToDelete = \Nexus\Database\NexusDB::listColumnIndexNames($tableName, $columnNames);
// 3. 执行删除操作
Schema::table($tableName, function (Blueprint $table) use ($indexesToDelete) {
foreach ($indexesToDelete as $indexName) {
// 如果是主键,需要单独处理
if ($indexName === 'primary') {
$table->dropPrimary();
} else {
$table->dropIndex($indexName);
}
}
}
if (!empty($indexToDrop)) {
$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);
$table->index(['torrent', 'peer_id']);
$table->index('peer_id');
});
}
@@ -4,6 +4,7 @@ use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
use Nexus\Database\NexusDB;
return new class extends Migration
{
@@ -15,26 +16,24 @@ return new class extends Migration
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;
$columnNames = ['torrentid', 'userid'];
// 1. 获取该表所有的索引信息
$indexesToDelete = NexusDB::listColumnIndexNames($tableName, $columnNames);
// 3. 执行删除操作
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;
}
}
if (!empty($indexToDrop)) {
$sql = sprintf("alter table %s %s", $tableName, implode(', ', $indexToDrop));
DB::statement($sql);
}
$table->unique(['torrentid', 'userid']);
$table->index('userid');
});
$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()
{
$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;
$columnNames = ['torrent', 'peer_id', 'userid'];
// 1. 获取该表所有的索引信息
$indexesToDelete = \Nexus\Database\NexusDB::listColumnIndexNames($tableName, $columnNames);
// 3. 执行删除操作
Schema::table($tableName, function (Blueprint $table) use ($indexesToDelete) {
foreach ($indexesToDelete as $indexName) {
// 如果是主键,需要单独处理
if ($indexName === 'primary') {
$table->dropPrimary();
} else {
$table->dropIndex($indexName);
}
}
}
DB::statement("alter table $tableName add unique unique_torrent_peer_user(`torrent`, `peer_id`, `userid`)");
$table->unique(['torrent', 'peer_id', 'userid']);
$table->index('peer_id');
$table->index('userid');
});
}
@@ -13,10 +13,10 @@ return new class extends Migration
*/
public function up()
{
$columnInfo = \Nexus\Database\NexusDB::getMysqlColumnInfo("torrents", "cache_stamp");
if ($columnInfo["DATA_TYPE"] == "int") {
return;
}
// $columnInfo = \Nexus\Database\NexusDB::getMysqlColumnInfo("torrents", "cache_stamp");
// if ($columnInfo["DATA_TYPE"] == "int") {
// return;
// }
Schema::table('torrents', function (Blueprint $table) {
$table->integer("cache_stamp")->default(0)->change();
});
@@ -13,8 +13,9 @@ return new class extends Migration
*/
public function up()
{
$sql = 'ALTER table torrents MODIFY column `info_hash` binary(20) DEFAULT NULL';
\Illuminate\Support\Facades\DB::statement($sql);
Schema::table('torrents', function (Blueprint $table) {
$table->binary('info_hash', 20)->nullable()->change();
});
}
/**
@@ -11,6 +11,9 @@ return new class extends Migration
*/
public function up(): void
{
if (\Nexus\Database\NexusDB::isPgsql()) {
return;
}
$tableFields = \App\Repositories\UpgradeRepository::DATETIME_INVALID_VALUE_FIELDS;
foreach ($tableFields as $table => $fields) {
+3 -3
View File
@@ -1,7 +1,7 @@
<?php
$config = require ROOT_PATH . 'config/nexus.php';
$connectionMysql = $config['mysql'];
\Nexus\Database\NexusDB::bootEloquent($connectionMysql);
$dbConfig = nexus_config('nexus.database');
$config = $dbConfig['connections'][$dbConfig['default']];
\Nexus\Database\NexusDB::bootEloquent($config);
+24
View File
@@ -461,6 +461,30 @@ class NexusDB
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 = [
'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',
'UID_STARTS',
];
@@ -483,6 +483,10 @@ class Install
$item['type'] = 'select';
$item['options'] = $this->listTimeZone();
}
if ($name == 'DB_CONNECTION') {
$item['type'] = 'select';
$item['options'] = ['mysql', 'pgsql'];
}
$formControls[] = $item;
}