From ea4d98d4e1618f8713e10518570bbfa2033a4758 Mon Sep 17 00:00:00 2001 From: DariusIII Date: Thu, 29 Nov 2018 14:47:00 +0100 Subject: [PATCH] Use DB::statement in place of DB::unprepared + DB::commit --- Blacklight/Binaries.php | 3 +-- Blacklight/Sharing.php | 3 +-- Blacklight/db/PreDb.php | 4 +-- Changelog | 1 + app/Console/Commands/NntmuxResetDb.php | 16 ++++------- app/Console/Commands/NntmuxResetTruncate.php | 28 +++++++------------- app/Models/Group.php | 13 ++++----- cli/data/predb_import_daily_batch.php | 15 +++++------ misc/sphinxsearch/create_se_tables.php | 6 ++--- misc/sphinxsearch/populate_rt_indexes.php | 3 +-- misc/testing/DB/add_movieinfo_id.php | 7 ++--- misc/testing/DB/change_imdb_column.php | 13 +++------ misc/update/tmux/bin/groupfixrelnames.php | 3 +-- misc/update/tmux/bin/update_groups.php | 3 +-- 14 files changed, 41 insertions(+), 77 deletions(-) diff --git a/Blacklight/Binaries.php b/Blacklight/Binaries.php index e65729093..f949ca076 100755 --- a/Blacklight/Binaries.php +++ b/Blacklight/Binaries.php @@ -1082,8 +1082,7 @@ class Binaries ORDER BY numberid ASC LIMIT %d', $tableNames['prname'], $groupArr['id'], $this->_partRepairMaxTries, $this->_partRepairLimit)); } catch (\PDOException $e) { if (preg_match('/SQLSTATE\[42S02\]: Base table or view not found/i', $e->getMessage())) { - DB::unprepared("CREATE TABLE {$tableNames['prname']} LIKE missed_parts"); - DB::commit(); + DB::statement("CREATE TABLE {$tableNames['prname']} LIKE missed_parts"); } if ($e->getMessage() === 'SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction') { $this->colorCli->notice('Deadlock occurred'); diff --git a/Blacklight/Sharing.php b/Blacklight/Sharing.php index 8f59af620..7df400c2a 100755 --- a/Blacklight/Sharing.php +++ b/Blacklight/Sharing.php @@ -141,8 +141,7 @@ class Sharing */ public function initSettings(&$siteGuid = '') { - DB::unprepared('TRUNCATE TABLE sharing'); - DB::commit(); + DB::statement('TRUNCATE TABLE sharing'); $siteName = uniqid('nntmux_', true); DB::insert( sprintf( diff --git a/Blacklight/db/PreDb.php b/Blacklight/db/PreDb.php index d75f16636..bbfb2222f 100755 --- a/Blacklight/db/PreDb.php +++ b/Blacklight/db/PreDb.php @@ -161,7 +161,7 @@ SQL_EXPORT; $this->prepareSQLLoadData($options); } - return $this->ps['LoadData']->execute([':path' => $options['path']]); + return $this->ps['LoadData']->execute($options['path']); } /** @@ -200,7 +200,7 @@ SQL_EXPORT; $this->ps['Truncate']->execute(); - $this->ps['LoadData']->execute([':path' => $filespec]); + $this->ps['LoadData']->execute(['path' => $filespec]); $this->ps['DeleteShort']->execute(); diff --git a/Changelog b/Changelog index 07585eca8..65b424dd0 100755 --- a/Changelog +++ b/Changelog @@ -1,4 +1,5 @@ 2018-11-29 DariusIII + * Chg: Use DB::statement in place of DB::unprepared + DB::commit * Chg: Use str_plural for duration * Chg: Update composer/xdebug-handler to version 1.3.1 * Chg: Update predb_import script, it should work now diff --git a/app/Console/Commands/NntmuxResetDb.php b/app/Console/Commands/NntmuxResetDb.php index 08ea6b138..0d2aca303 100644 --- a/app/Console/Commands/NntmuxResetDb.php +++ b/app/Console/Commands/NntmuxResetDb.php @@ -43,8 +43,7 @@ class NntmuxResetDb extends Command if ($this->confirm('This script removes all releases, nzb files, samples, previews , nfos, truncates all article tables and resets all groups. Are you sure you want reset the DB?')) { $timestart = now(); - DB::unprepared('SET FOREIGN_KEY_CHECKS = 0;'); - DB::commit(); + DB::statement('SET FOREIGN_KEY_CHECKS = 0;'); Group::query()->update([ 'first_record' => 0, @@ -80,16 +79,12 @@ class NntmuxResetDb extends Command 'releases_groups', ]; foreach ($arr as &$value) { - $rel = DB::unprepared("TRUNCATE TABLE $value"); - DB::commit(); - if ($rel === true) { - $this->info('Truncating '.$value.' completed.'); - } + DB::statement("TRUNCATE TABLE $value"); + $this->info('Truncating '.$value.' completed.'); } unset($value); $this->info('Truncating binaries, collections, missed_parts and parts tables...'); - DB::unprepared("CALL loop_cbpm('truncate')"); - DB::commit(); + DB::statement("CALL loop_cbpm('truncate')"); $this->info('Truncating completed.'); (new SphinxSearch())->truncateRTIndex(); @@ -108,8 +103,7 @@ class NntmuxResetDb extends Command } $this->info('Deleted all releases, images, previews and samples. This script finished '.now()->diffForHumans($timestart).' start'); - DB::unprepared('SET FOREIGN_KEY_CHECKS = 1;'); - DB::commit(); + DB::statement('SET FOREIGN_KEY_CHECKS = 1;'); } else { $this->info('Script execution stopped'); } diff --git a/app/Console/Commands/NntmuxResetTruncate.php b/app/Console/Commands/NntmuxResetTruncate.php index d3dbbe49d..d7f3b113f 100644 --- a/app/Console/Commands/NntmuxResetTruncate.php +++ b/app/Console/Commands/NntmuxResetTruncate.php @@ -41,17 +41,13 @@ class NntmuxResetTruncate extends Command { Group::query()->update(['first_record' => 0, 'first_record_postdate' => null, 'last_record' => 0, 'last_record_postdate' => null, 'last_updated' => null]); $this->info('Reseting all groups completed.'); - DB::unprepared('SET FOREIGN_KEY_CHECKS = 0;'); - DB::commit(); + DB::statement('SET FOREIGN_KEY_CHECKS = 0;'); $arr = ['parts', 'missed_parts', 'binaries', 'collections', 'multigroup_parts', 'multigroup_missed_parts', 'multigroup_binaries', 'multigroup_collections']; foreach ($arr as &$value) { - $rel = DB::unprepared("TRUNCATE TABLE $value"); - DB::commit(); - if ($rel === true) { - $this->info("Truncating $value completed."); - } + DB::statement("TRUNCATE TABLE $value"); + $this->info("Truncating $value completed."); } unset($value); @@ -59,24 +55,18 @@ class NntmuxResetTruncate extends Command $tbl = $row->Name; if (preg_match('/collections_\d+/', $tbl) || preg_match('/binaries_\d+/', $tbl) || preg_match('/parts_\d+/', $tbl) || preg_match('/missed_parts_\d+/', $tbl) || preg_match('/\d+_collections/', $tbl) || preg_match('/\d+_binaries/', $tbl) || preg_match('/\d+_parts/', $tbl) || preg_match('/\d+_missed_parts_\d+/', $tbl)) { if ($this->argument('type') === 'true') { - $rel = DB::unprepared("DROP TABLE $tbl"); - DB::commit(); - if ($rel === true) { - $this->info("Dropping $tbl completed."); - } + DB::statement("DROP TABLE $tbl"); + $this->info("Dropping $tbl completed."); } else { - $rel = DB::unprepared("TRUNCATE TABLE $tbl"); - DB::commit(); - if ($rel === true) { - $this->info("Truncating $tbl completed."); - } + DB::statement("TRUNCATE TABLE $tbl"); + $this->info("Truncating $tbl completed."); + } } } $delcount = Release::query()->where('nzbstatus', '=', 0)->delete(); $this->info($delcount.' releases had no nzb, deleted.'); - DB::unprepared('SET FOREIGN_KEY_CHECKS = 1;'); - DB::commit(); + DB::statement('SET FOREIGN_KEY_CHECKS = 1;'); } } diff --git a/app/Models/Group.php b/app/Models/Group.php index 9b8878169..6f7cb18dc 100644 --- a/app/Models/Group.php +++ b/app/Models/Group.php @@ -347,7 +347,7 @@ class Group extends Model MissedPart::query()->where('groups_id', $id)->delete(); foreach (self::$cbpm as $tablePrefix) { - DB::unprepared( + DB::statement( "DROP TABLE IF EXISTS {$tablePrefix}_{$id}" ); } @@ -374,7 +374,7 @@ class Group extends Model public static function resetall(): bool { foreach (self::$cbpm as $tablePrefix) { - DB::unprepared("TRUNCATE TABLE {$tablePrefix}"); + DB::statement("TRUNCATE TABLE {$tablePrefix}"); } $groups = self::query()->select(['id'])->get(); @@ -382,7 +382,7 @@ class Group extends Model if ($groups instanceof \Traversable) { foreach ($groups as $group) { foreach (self::$cbpm as $tablePrefix) { - DB::unprepared("DROP TABLE IF EXISTS {$tablePrefix}_{$group['id']}"); + DB::statement("DROP TABLE IF EXISTS {$tablePrefix}_{$group['id']}"); } } } @@ -570,12 +570,9 @@ class Group extends Model public static function createNewTPGTables($groupID): bool { foreach (self::$cbpm as $tablePrefix) { - if (DB::unprepared( + DB::statement( "CREATE TABLE IF NOT EXISTS {$tablePrefix}_{$groupID} LIKE {$tablePrefix}" - ) === null - ) { - return false; - } + ); } return true; diff --git a/cli/data/predb_import_daily_batch.php b/cli/data/predb_import_daily_batch.php index 0b09cd70d..71e161409 100755 --- a/cli/data/predb_import_daily_batch.php +++ b/cli/data/predb_import_daily_batch.php @@ -151,14 +151,14 @@ foreach ($data as $dir => $files) { $predb->executeTruncate(); // Import file into predb_imports - $predb->executeLoadData( + dd($predb->executeLoadData( [ 'fields' => '\\t\\t', 'lines' => '\\r\\n', 'local' => $local, 'path' => $dumpFile, ] - ); + )); // Remove any titles where length <=8 if ($verbose === true) { @@ -173,7 +173,7 @@ foreach ($data as $dir => $files) { $predb->executeUpdateGroupID(); $colorCli->info('Inserting records from temporary table into predb table'); - $predb->executeInsert(); + $inserted =$predb->executeInsert(); // Delete the dump. unlink($dumpFile); @@ -182,12 +182,9 @@ foreach ($data as $dir => $files) { settings_array($match[2] + 1, $progress), ['read' => false] ); - echo sprintf( - "Successfully imported PreDB dump %d (%s), %d dumps remaining\n", - $match[2], - date('Y-m-d', $match[2]), - --$total - ); + if ($inserted === true) { + echo sprintf("Successfully imported PreDB dump %d (%s), %d dumps remaining\n", $match[2], date('Y-m-d', $match[2]), --$total); + } } else { echo "Ignoring: {$file['download_url']}\n"; } diff --git a/misc/sphinxsearch/create_se_tables.php b/misc/sphinxsearch/create_se_tables.php index efdbc34e4..e36c8404e 100644 --- a/misc/sphinxsearch/create_se_tables.php +++ b/misc/sphinxsearch/create_se_tables.php @@ -36,10 +36,8 @@ $tables = []; $tables['releases_se'] = sprintf($tableSQL_releases, $sphinxConnection); foreach ($tables as $table => $query) { - DB::unprepared("DROP TABLE IF EXISTS $table;"); - DB::commit(); - DB::unprepared($query); - DB::commit(); + DB::statement("DROP TABLE IF EXISTS $table;"); + DB::statement($query); } echo 'All done! If you messed up your sphinx connection info, you can rerun this script.'.PHP_EOL; diff --git a/misc/sphinxsearch/populate_rt_indexes.php b/misc/sphinxsearch/populate_rt_indexes.php index 05f1b4a3f..8d988bffe 100644 --- a/misc/sphinxsearch/populate_rt_indexes.php +++ b/misc/sphinxsearch/populate_rt_indexes.php @@ -23,8 +23,7 @@ if (! isset($argv[1]) || $argv[1] !== 'releases_rt') { function populate_rt($table, $max) { if ($table === 'releases_rt') { - DB::unprepared('SET SESSION group_concat_max_len=16384;'); - DB::commit(); + DB::statement('SET SESSION group_concat_max_len=16384;'); $query = 'SELECT r.id, r.name, r.searchname, r.fromname, IFNULL(GROUP_CONCAT(rf.name SEPARATOR " "),"") filename FROM releases r LEFT JOIN release_files rf ON(r.id=rf.releases_id) diff --git a/misc/testing/DB/add_movieinfo_id.php b/misc/testing/DB/add_movieinfo_id.php index b3490013d..d4c4e4a9d 100644 --- a/misc/testing/DB/add_movieinfo_id.php +++ b/misc/testing/DB/add_movieinfo_id.php @@ -11,7 +11,7 @@ require_once dirname(__DIR__, 3).DIRECTORY_SEPARATOR.'bootstrap/autoload.php'; $sql = Release::query()->whereNotNull('imdbid')->where('imdbid', '<>', '0000000')->get(['imdbid', 'id']); -DB::unprepared(' +DB::statement(' DROP TABLE IF EXISTS movie_temp; CREATE TABLE movie_temp ( releases_id INT(11), @@ -22,8 +22,6 @@ DB::unprepared(' COLLATE utf8_unicode_ci; '); -DB::commit(); - $count = $sql->count(); echo 'Copying '.$count.' imdbid values'.PHP_EOL; @@ -48,6 +46,5 @@ echo 'Finished inserting new values into releases table'.PHP_EOL; $check = Release::query()->whereNotNull('movieinfo_id')->count('id'); if ($check === $count) { - DB::unprepared('DROP TABLE movie_temp'); - DB::commit(); + DB::statement('DROP TABLE movie_temp'); } diff --git a/misc/testing/DB/change_imdb_column.php b/misc/testing/DB/change_imdb_column.php index 4a40395a5..7adb905af 100644 --- a/misc/testing/DB/change_imdb_column.php +++ b/misc/testing/DB/change_imdb_column.php @@ -10,7 +10,7 @@ require_once dirname(__DIR__, 3).DIRECTORY_SEPARATOR.'bootstrap/autoload.php'; $sql = Release::query()->whereNotNull('imdbid')->where('imdbid', '<>', '0000000')->get(['imdbid', 'id']); -DB::unprepared(' +DB::statement(' DROP TABLE IF EXISTS movie_temp; CREATE TABLE movie_temp ( releases_id INT(11), @@ -21,8 +21,6 @@ DB::unprepared(' COLLATE utf8_unicode_ci; '); -DB::commit(); - $count = $sql->count(); echo 'Copying '.$count.' imdbid values'.PHP_EOL; @@ -34,10 +32,8 @@ foreach ($sql as $movie) { echo PHP_EOL.'Finished copying '.$sql->count().' imdbid values'.PHP_EOL; -DB::unprepared('ALTER TABLE releases DROP imdbid'); -DB::commit(); -DB::unprepared('ALTER TABLE releases ADD imdbid VARCHAR(15) DEFAULT NULL'); -DB::commit(); +DB::statement('ALTER TABLE releases DROP imdbid'); +DB::statement('ALTER TABLE releases ADD imdbid VARCHAR(15) DEFAULT NULL'); echo 'Updating releases table with new values'.PHP_EOL; @@ -51,6 +47,5 @@ echo 'Finished inserting new values into releases table'.PHP_EOL; $check = Release::query()->whereNotNull('imdbid')->where('imdbid', '<>', '0000000')->count('id'); if ($check === $count) { - DB::unprepared('DROP TABLE movie_temp'); - DB::commit(); + DB::statement('DROP TABLE movie_temp'); } diff --git a/misc/update/tmux/bin/groupfixrelnames.php b/misc/update/tmux/bin/groupfixrelnames.php index ce535313f..9a8708567 100644 --- a/misc/update/tmux/bin/groupfixrelnames.php +++ b/misc/update/tmux/bin/groupfixrelnames.php @@ -27,8 +27,7 @@ switch (true) { case $type === 'standard' && $guidChar !== null && $maxPerRun !== null && is_numeric($maxPerRun): // Allow for larger filename return sets - DB::unprepared('SET SESSION group_concat_max_len = 65536'); - DB::commit(); + DB::statement('SET SESSION group_concat_max_len = 65536'); // Find releases to process. We only want releases that have no PreDB match, have not been renamed, exist // in Other Categories, have already been PP Add/NFO processed, and haven't been fully fixRelName processed diff --git a/misc/update/tmux/bin/update_groups.php b/misc/update/tmux/bin/update_groups.php index 969b48250..fbbca0703 100644 --- a/misc/update/tmux/bin/update_groups.php +++ b/misc/update/tmux/bin/update_groups.php @@ -29,8 +29,7 @@ if ($nntp->isError($data)) { $colorCli->header('Inserting new values into short_groups table.'); -DB::unprepared('TRUNCATE TABLE short_groups'); -DB::commit(); +DB::statement('TRUNCATE TABLE short_groups'); // Put into an array all active groups $result = array_pluck(Group::query()->where('active', '=', 1)->orWhere('backfill', '=', 1)->get(['name']), 'name');