From bf69b76c713e099c4923ed1adf1fc76a9b32ad9f Mon Sep 17 00:00:00 2001 From: Matthew Baggett Date: Thu, 1 Sep 2022 15:25:32 +0200 Subject: [PATCH] Consolidate verifyDumpSucceeded and checksumCheck --- syncer/AbstractSyncer.php | 38 ++++++++++++++++++++++++++++++++++++++ syncer/MysqlSyncer.php | 26 ++------------------------ syncer/PostgresSyncer.php | 26 ++------------------------ 3 files changed, 42 insertions(+), 48 deletions(-) diff --git a/syncer/AbstractSyncer.php b/syncer/AbstractSyncer.php index 65221dd..534192a 100644 --- a/syncer/AbstractSyncer.php +++ b/syncer/AbstractSyncer.php @@ -37,6 +37,7 @@ abstract class AbstractSyncer count($filesInS3), $showLimit )); + /** @var FileAttributes $file */ foreach (array_slice($filesInS3, 0, $showLimit) as $file) { $this->logger->debug(sprintf( @@ -143,4 +144,41 @@ abstract class AbstractSyncer return $uncompressedFile; } + + protected function checksumCheck($dumpFile): void + { + // Checksum dump and don't upload if the checksum is the same as last time. + $hash = sha1_file("/dumps/{$dumpFile}"); + if ($this->localFilesystem->has('previous_hash') && $hash == $this->localFilesystem->read('previous_hash')) { + $this->logger->debug(sprintf( + '%s Dump of %s matches previous dump (%s), not uploading the same file again.', + Emoji::abacus(), + $dumpFile, + substr($hash, 0, 7) + )); + + exit; + } + $this->localFilesystem->write('previous_hash', $hash); + } + + protected function verifyDumpSucceeded($dumpFile): void + { + if (!$this->localFilesystem->fileExists($dumpFile)) { + $this->logger->critical('Database dump failed'); + + exit; + } + if (!$this->localFilesystem->fileSize($dumpFile) > 0) { + $this->logger->critical('Dump file was created, but was empty.'); + + exit; + } + $this->logger->debug(sprintf( + 'Dump file was made, and is %s uncompressed', + ByteSize::formatMetric( + $this->localFilesystem->fileSize($dumpFile) + ) + )); + } } diff --git a/syncer/MysqlSyncer.php b/syncer/MysqlSyncer.php index 8d7d355..e8fe16d 100644 --- a/syncer/MysqlSyncer.php +++ b/syncer/MysqlSyncer.php @@ -2,7 +2,6 @@ namespace S3DB\Sync; -use Rych\ByteSize\ByteSize; use Spatie\Emoji\Emoji; class MysqlSyncer extends AbstractSyncer @@ -15,31 +14,10 @@ class MysqlSyncer extends AbstractSyncer passthru($command); // Verify the dump worked - if (!$this->localFilesystem->fileExists($dumpFile)) { - $this->logger->critical('Database dump failed'); - - exit; - } - $this->logger->debug(sprintf( - 'Dump file was made, and is %s uncompressed', - ByteSize::formatMetric( - $this->localFilesystem->fileSize($dumpFile) - ) - )); + $this->verifyDumpSucceeded($dumpFile); // Checksum dump and don't upload if the checksum is the same as last time. - $hash = sha1_file("/dumps/{$dumpFile}"); - if ($this->localFilesystem->has('previous_hash') && $hash == $this->localFilesystem->read('previous_hash')) { - $this->logger->debug(sprintf( - '%s Dump of %s matches previous dump (%s), not uploading the same file again.', - Emoji::abacus(), - $dumpFile, - substr($hash, 0, 7) - )); - - exit; - } - $this->localFilesystem->write('previous_hash', $hash); + $this->checksumCheck($dumpFile); // XZ compress dump $compressedDumpFile = $this->compress($dumpFile); diff --git a/syncer/PostgresSyncer.php b/syncer/PostgresSyncer.php index 884767b..6614436 100644 --- a/syncer/PostgresSyncer.php +++ b/syncer/PostgresSyncer.php @@ -2,7 +2,6 @@ namespace S3DB\Sync; -use Rych\ByteSize\ByteSize; use Spatie\Emoji\Emoji; class PostgresSyncer extends AbstractSyncer @@ -15,31 +14,10 @@ class PostgresSyncer extends AbstractSyncer passthru($command); // Verify the dump worked - if (!$this->localFilesystem->fileExists($dumpFile)) { - $this->logger->critical('Database dump failed'); - - exit; - } - $this->logger->debug(sprintf( - 'Dump file was made, and is %s uncompressed', - ByteSize::formatMetric( - $this->localFilesystem->fileSize($dumpFile) - ) - )); + $this->verifyDumpSucceeded($dumpFile); // Checksum dump and don't upload if the checksum is the same as last time. - $hash = sha1_file("/dumps/{$dumpFile}"); - if ($this->localFilesystem->has('previous_hash') && $hash == $this->localFilesystem->read('previous_hash')) { - $this->logger->debug(sprintf( - '%s Dump of %s matches previous dump (%s), not uploading the same file again.', - Emoji::abacus(), - $dumpFile, - substr($hash, 0, 7) - )); - - exit; - } - $this->localFilesystem->write('previous_hash', $hash); + $this->checksumCheck($dumpFile); // XZ compress dump $compressedDumpFile = $this->compress($dumpFile);