mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-05-06 13:07:23 +08:00
pg support of duplicate key update
This commit is contained in:
@@ -29,7 +29,7 @@ class MigrateTorrentsTableTextColumn extends Command
|
||||
public function handle()
|
||||
{
|
||||
if (Schema::hasTable("torrent_extras") && Schema::hasColumn("torrents", "descr")) {
|
||||
NexusDB::statement("insert into torrent_extras (torrent_id, descr, media_info, nfo, pt_gen, created_at) select id, descr, technical_info, nfo, pt_gen, now() from torrents on duplicate key update torrent_id = values(torrent_id)");
|
||||
NexusDB::statement("insert into torrent_extras (torrent_id, descr, media_info, nfo, pt_gen, created_at) select id, descr, technical_info, nfo, pt_gen, now() from torrents " . NexusDB::upsertField(['torrent_id'], ['torrent_id']));
|
||||
}
|
||||
$columns = ["ori_descr", "descr", "nfo", "technical_info", "pt_gen"];
|
||||
$sql = "alter table torrents ";
|
||||
|
||||
@@ -28,7 +28,6 @@ class RemoveUserDonorStatus
|
||||
->with('language')
|
||||
->where('donor', 'yes')
|
||||
->whereNotNull('donoruntil')
|
||||
->where('donoruntil', '!=', '0000-00-00 00:00:00')
|
||||
->where('donoruntil', '<', now())
|
||||
->get();
|
||||
$userModifyLogs = [];
|
||||
|
||||
@@ -372,7 +372,6 @@ class User extends Authenticatable implements FilamentUser, HasName
|
||||
{
|
||||
return $query->where('donor', 'yes')->where(function (Builder $query) {
|
||||
return $query->whereNull('donoruntil')
|
||||
->orWhere('donoruntil', '0000-00-00 00:00:00')
|
||||
->orWhere('donoruntil', '>=', now());
|
||||
});
|
||||
}
|
||||
|
||||
@@ -244,8 +244,8 @@ class AttendanceRepository extends BaseRepository
|
||||
return 0;
|
||||
}
|
||||
$sql = sprintf(
|
||||
"insert into `%s` (`uid`, `points`, `date`) values %s on duplicate key update `uid` = values(`uid`)",
|
||||
$table, implode(',', $insert)
|
||||
'insert into %s (uid, points, "date") values %s %s',
|
||||
$table, implode(',', $insert), NexusDB::upsertField(['uid'], ['uid'])
|
||||
);
|
||||
NexusDB::statement($sql);
|
||||
$insertCount = count($insert);
|
||||
|
||||
@@ -1049,13 +1049,13 @@ class ExamRepository extends BaseRepository
|
||||
if ($donateStatus == User::DONATE_YES) {
|
||||
$baseQuery->where(function (Builder $query) {
|
||||
$query->where('donor', 'yes')->where(function (Builder $query) {
|
||||
$query->where('donoruntil', '0000-00-00 00:00:00')->orWhereNull('donoruntil')->orWhere('donoruntil', '>=', Carbon::now());
|
||||
$query->whereNull('donoruntil')->orWhere('donoruntil', '>=', Carbon::now());
|
||||
});
|
||||
});
|
||||
} elseif ($donateStatus == User::DONATE_NO) {
|
||||
$baseQuery->where(function (Builder $query) {
|
||||
$query->where('donor', 'no')->orWhere(function (Builder $query) {
|
||||
$query->where('donoruntil', '!=','0000-00-00 00:00:00')->whereNotNull('donoruntil')->where('donoruntil', '<', Carbon::now());
|
||||
$query->whereNotNull('donoruntil')->where('donoruntil', '<', Carbon::now());
|
||||
});
|
||||
});
|
||||
} else {
|
||||
|
||||
@@ -400,7 +400,7 @@ class HitAndRunRepository extends BaseRepository
|
||||
->selectRaw("count(*) as counts, uid")
|
||||
->where('status', HitAndRun::STATUS_UNREACHED)
|
||||
->groupBy('uid')
|
||||
->having("counts", '>=', $disableCounts)
|
||||
->havingRaw("count(*) >= $disableCounts")
|
||||
;
|
||||
if ($setting['diff_in_section']) {
|
||||
$query->whereHas('torrent.basic_category', function (Builder $query) use ($setting) {
|
||||
|
||||
@@ -45,8 +45,8 @@ class SettingRepository extends BaseRepository
|
||||
return true;
|
||||
}
|
||||
$sql = sprintf(
|
||||
"insert into `%s` (`name`, `value`) values %s on duplicate key update `value` = values(`value`)",
|
||||
$settingModel->getTable(), implode(', ', $values)
|
||||
'insert into %s (name, "value") values %s %s',
|
||||
$settingModel->getTable(), implode(', ', $values), NexusDB::upsertField(['name'], ['value'])
|
||||
);
|
||||
$result = DB::insert($sql);
|
||||
do_log("sql: $sql, result: $result");
|
||||
|
||||
@@ -133,7 +133,7 @@ class TagRepository extends BaseRepository
|
||||
}
|
||||
$page++;
|
||||
}
|
||||
$sql .= sprintf("%s on duplicate key update updated_at = values(updated_at)", implode(', ', $values));
|
||||
$sql .= sprintf("%s %s", implode(', ', $values), NexusDB::upsertField(['torrent_id', 'tag_id'], ['updated_at']));
|
||||
do_log("migrate sql: $sql");
|
||||
NexusDB::statement($sql);
|
||||
do_log("[MIGRATE_TORRENT_TAG] done!");
|
||||
|
||||
@@ -749,7 +749,7 @@ class TorrentRepository extends BaseRepository
|
||||
$values[] = sprintf("(%s, %s, '%s', '%s')", $torrentId, $tagId, $time, $time);
|
||||
}
|
||||
}
|
||||
$sql .= implode(', ', $values) . " on duplicate key update updated_at = values(updated_at)";
|
||||
$sql .= implode(', ', $values) . " " . NexusDB::upsertField(['torrent_id', 'tag_id'], ['updated_at']);
|
||||
if ($remove) {
|
||||
TorrentTag::query()->whereIn('torrent_id', $idArr)->delete();
|
||||
}
|
||||
|
||||
+3
-1
@@ -35,6 +35,7 @@
|
||||
"ext-xml": "*",
|
||||
"ext-zend-opcache": "*",
|
||||
"ext-zip": "*",
|
||||
"ext-pdo": "*",
|
||||
"calebporzio/sushi": "^2.5",
|
||||
"cybercog/laravel-clickhouse": "dev-master",
|
||||
"elasticsearch/elasticsearch": "^7.16",
|
||||
@@ -87,7 +88,8 @@
|
||||
"@php artisan key:generate --ansi"
|
||||
],
|
||||
"post-update-cmd": [
|
||||
"@php artisan filament:upgrade"
|
||||
"@php artisan filament:upgrade",
|
||||
"@php artisan livewire:publish --assets"
|
||||
]
|
||||
},
|
||||
"extra": {
|
||||
|
||||
+11
-6
@@ -139,7 +139,8 @@ function promotion($class, $down_floor_gb, $minratio, $time_week, $addinvite = 0
|
||||
|
||||
function demotion($class,$deratio){
|
||||
$newclass = $class - 1;
|
||||
$sql = "SELECT id FROM users WHERE class = $class AND uploaded / downloaded < $deratio";
|
||||
// $sql = "SELECT id FROM users WHERE class = $class AND uploaded / downloaded < $deratio";
|
||||
$sql = "SELECT id FROM users WHERE class = $class AND uploaded < downloaded * $deratio";
|
||||
$res = sql_query($sql) or sqlerr(__FILE__, __LINE__);
|
||||
$matchUserCount = mysql_num_rows($res);
|
||||
do_log("sql: $sql, match user count: $matchUserCount");
|
||||
@@ -585,7 +586,8 @@ function docleanup($forceAll = 0, $printProgress = false) {
|
||||
|
||||
//3.delete unconfirmed accounts
|
||||
$deadtime = time() - $signup_timeout;
|
||||
sql_query("DELETE FROM users WHERE status = 'pending' AND added < FROM_UNIXTIME($deadtime) AND last_login < FROM_UNIXTIME($deadtime) AND last_access < FROM_UNIXTIME($deadtime)") or sqlerr(__FILE__, __LINE__);
|
||||
$deadlineField = \Nexus\Database\NexusDB::fromUnixTimestampField($deadtime);
|
||||
sql_query("DELETE FROM users WHERE status = 'pending' AND added < $deadlineField AND last_login < $deadlineField AND last_access < $deadlineField") or sqlerr(__FILE__, __LINE__);
|
||||
// $query = \App\Models\User::query()
|
||||
// ->where('status', 'pending')
|
||||
// ->whereRaw("added < FROM_UNIXTIME($deadtime)")
|
||||
@@ -619,7 +621,7 @@ function docleanup($forceAll = 0, $printProgress = false) {
|
||||
}
|
||||
|
||||
//7.delete regimage codes
|
||||
sql_query("TRUNCATE TABLE `regimages`") or sqlerr(__FILE__, __LINE__);
|
||||
sql_query("TRUNCATE TABLE regimages") or sqlerr(__FILE__, __LINE__);
|
||||
$log = "delete regimage codes";
|
||||
do_log($log);
|
||||
if ($printProgress) {
|
||||
@@ -1069,7 +1071,7 @@ function docleanup($forceAll = 0, $printProgress = false) {
|
||||
|
||||
//delete old shoutbox
|
||||
$until = TIMENOW - $length;
|
||||
sql_query("DELETE FROM shoutbox WHERE `date` < ".sqlesc($until)) or sqlerr(__FILE__, __LINE__);
|
||||
sql_query("DELETE FROM shoutbox WHERE date < $until") or sqlerr(__FILE__, __LINE__);
|
||||
$log = "delete old shoutbox";
|
||||
do_log($log);
|
||||
if ($printProgress) {
|
||||
@@ -1078,7 +1080,7 @@ function docleanup($forceAll = 0, $printProgress = false) {
|
||||
|
||||
//delete old general log
|
||||
$until = date("Y-m-d H:i:s",(TIMENOW - $length));
|
||||
sql_query("DELETE FROM sitelog WHERE added < ".sqlesc($until)) or sqlerr(__FILE__, __LINE__);
|
||||
sql_query("DELETE FROM sitelog WHERE added < " . sqlesc($until)) or sqlerr(__FILE__, __LINE__);
|
||||
$log = "delete old general log";
|
||||
do_log($log);
|
||||
if ($printProgress) {
|
||||
@@ -1157,7 +1159,10 @@ function docleanup($forceAll = 0, $printProgress = false) {
|
||||
|
||||
//8.lock topics where last post was made more than x days ago
|
||||
$secs = 365*24*60*60;
|
||||
sql_query("UPDATE topics, posts SET topics.locked='yes' WHERE topics.lastpost = posts.id AND topics.sticky = 'no' AND UNIX_TIMESTAMP(posts.added) < ".TIMENOW." - $secs") or sqlerr(__FILE__, __LINE__);
|
||||
$postAddedField = \Nexus\Database\NexusDB::unixTimestampField('posts.added');
|
||||
$diff = TIMENOW - $secs;
|
||||
// sql_query("UPDATE topics, posts SET topics.locked='yes' WHERE topics.lastpost = posts.id AND topics.sticky = 'no' AND $postAddedField < ".TIMENOW." - $secs") or sqlerr(__FILE__, __LINE__);
|
||||
sql_query("UPDATE topics SET locked='yes' WHERE sticky = 'no' AND lastpost in (select id from posts where $postAddedField < $diff)");
|
||||
|
||||
$log = "lock topics where last post was made more than x days ago";
|
||||
do_log($log);
|
||||
|
||||
@@ -5418,14 +5418,7 @@ function saveSetting(string $prefix, array $nameAndValue, string $autoload = 'ye
|
||||
}
|
||||
$data[] = sprintf("(%s, %s, %s, %s, '%s')", sqlesc("$prefix.$name"), sqlesc($value), sqlesc($datetimeNow), sqlesc($datetimeNow), $autoload);
|
||||
}
|
||||
$sql .= implode(",", $data);
|
||||
if (\Nexus\Database\NexusDB::isMysql()) {
|
||||
$sql .= " on duplicate key update value = values(value)";
|
||||
} else if (\Nexus\Database\NexusDB::isPgsql()) {
|
||||
$sql .= " on conflict (name) do update set value = EXCLUDED.value";
|
||||
} else {
|
||||
throw new \RuntimeException('Not supported database.');
|
||||
}
|
||||
$sql .= implode(",", $data) . " " . \Nexus\Database\NexusDB::upsertField(['name'], ['value']);
|
||||
\Nexus\Database\NexusDB::statement($sql);
|
||||
clear_setting_cache();
|
||||
do_action("nexus_setting_update");
|
||||
|
||||
@@ -522,4 +522,39 @@ class NexusDB
|
||||
}
|
||||
}
|
||||
|
||||
public static function fromUnixTimestampField(int $timestamp): string
|
||||
{
|
||||
if (self::isMysql()) {
|
||||
return sprintf("FROM_UNIXTIME(%d)", $timestamp);
|
||||
} elseif (self::isPgsql()) {
|
||||
return sprintf("to_timestamp(%d)", $timestamp);
|
||||
} else {
|
||||
throw new \RuntimeException('Not supported database.');
|
||||
}
|
||||
}
|
||||
|
||||
public static function upsertField(array $uniqueFields, array $updateFields): string
|
||||
{
|
||||
if (self::isMysql()) {
|
||||
$updates = [];
|
||||
foreach ($updateFields ?: ['id'] as $field) {
|
||||
$updates[] = "`$field` = VALUES(`$field`)";
|
||||
}
|
||||
return sprintf("ON DUPLICATE KEY UPDATE %s", implode(', ', $updates));
|
||||
} elseif (self::isPgsql()) {
|
||||
if (empty($updateFields)) {
|
||||
$updateStr = "NOTHING";
|
||||
} else {
|
||||
$updates = [];
|
||||
foreach ($updateFields as $field) {
|
||||
$updates[] = "$field = EXCLUDED.$field";
|
||||
}
|
||||
$updateStr = "UPDATE SET " . implode(', ', $updates);
|
||||
}
|
||||
return sprintf("ON CONFLICT (%s) DO %s", implode(', ', $uniqueFields), $updateStr);
|
||||
} else {
|
||||
throw new \RuntimeException('Not supported database.');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
-2
@@ -566,8 +566,8 @@ if (($left > 0 || $event == "completed") && $az['class'] < \App\Models\HitAndRun
|
||||
if ($snatchInfo['downloaded'] >= $requiredDownloaded) {
|
||||
$nowStr = date('Y-m-d H:i:s');
|
||||
$sql = sprintf(
|
||||
"insert into hit_and_runs (uid, torrent_id, snatched_id, created_at, updated_at) values (%d, %d, %d, '%s', '%s') on duplicate key update updated_at = '%s'",
|
||||
$userid, $torrentid, $snatchInfo['id'], $nowStr, $nowStr, $nowStr
|
||||
"insert into hit_and_runs (uid, torrent_id, snatched_id, created_at, updated_at) values (%d, %d, %d, '%s', '%s') %s",
|
||||
$userid, $torrentid, $snatchInfo['id'], $nowStr, $nowStr, \Nexus\Database\NexusDB::upsertField(['uid', 'torrent_id'], ['updated_at'])
|
||||
);
|
||||
$affectedRows = sql_query($sql);
|
||||
$hitAndRunId = mysql_insert_id();
|
||||
|
||||
Reference in New Issue
Block a user