Use DB::statement in place of DB::unprepared + DB::commit

This commit is contained in:
DariusIII
2018-11-29 14:47:00 +01:00
parent c9b26a40f4
commit ea4d98d4e1
14 changed files with 41 additions and 77 deletions
+1 -2
View File
@@ -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');
+1 -2
View File
@@ -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(
+2 -2
View File
@@ -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();
+1
View File
@@ -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
+5 -11
View File
@@ -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');
}
+9 -19
View File
@@ -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;');
}
}
+5 -8
View File
@@ -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;
+6 -9
View File
@@ -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";
}
+2 -4
View File
@@ -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;
+1 -2
View File
@@ -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)
+2 -5
View File
@@ -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');
}
+4 -9
View File
@@ -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');
}
+1 -2
View File
@@ -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
+1 -2
View File
@@ -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');