From a99a4529374a4fcfe97abd98c525ad45e81e27c3 Mon Sep 17 00:00:00 2001 From: binsky Date: Sat, 2 Oct 2021 16:46:47 +0200 Subject: use try catch to not crash systems with untested databases Signed-off-by: binsky --- .../Version02031335Date20211001122343.php | 33 ++++++++++++++-------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/Migration/Version02031335Date20211001122343.php b/lib/Migration/Version02031335Date20211001122343.php index 4d738fd5..e5775218 100644 --- a/lib/Migration/Version02031335Date20211001122343.php +++ b/lib/Migration/Version02031335Date20211001122343.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace OCA\Passman\Migration; use Closure; +use Doctrine\DBAL\Exception; use OCP\DB\ISchemaWrapper; use OCP\IDBConnection; use OCP\Migration\IOutput; @@ -68,21 +69,29 @@ class Version02031335Date20211001122343 extends SimpleMigrationStep { public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { if ($this->dataMigrationRequired) { $updateQuery = $this->connection->getQueryBuilder(); - $updateQuery->update('passman_credentials') - ->set($this->newColumn, $this->oldColumn) - ->where($this->newColumn . ' IS NULL') - ->andWhere($this->oldColumn . ' IS NOT NULL') - ->executeStatement(); + try { + $updateQuery->update('passman_credentials') + ->set($this->newColumn, $this->oldColumn) + ->where($this->newColumn . ' IS NULL') + ->andWhere($this->oldColumn . ' IS NOT NULL') + ->executeStatement(); - /** @var ISchemaWrapper $schema */ - $schema = $schemaClosure(); + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); - if ($schema->hasTable('passman_credentials')) { - $table = $schema->getTable('passman_credentials'); - if ($table->hasColumn($this->oldColumn) && $table->hasColumn($this->newColumn)) { - $dropColumnStatement = $this->connection->prepare('ALTER TABLE ' . $table->getName() . ' DROP COLUMN ' . $this->oldColumn . ';'); - $dropColumnStatement->execute(); + if ($schema->hasTable('passman_credentials')) { + $table = $schema->getTable('passman_credentials'); + if ($table->hasColumn($this->oldColumn) && $table->hasColumn($this->newColumn)) { + $dropColumnStatement = $this->connection->prepare('ALTER TABLE ' . $table->getName() . ' DROP COLUMN ' . $this->oldColumn . ';'); + try { + $dropColumnStatement->execute(); + } catch (Exception $e) { + echo "Migration was not able to drop the old " . $this->oldColumn . "column" . PHP_EOL; + } + } } + } catch (\OCP\DB\Exception $e) { + echo "Migration was not able to copy data from " . $this->oldColumn . " to " . $this->newColumn . PHP_EOL; } } } -- cgit v1.2.3