From 1d75efad36600e267a38825f3c4d2e6b63a2eb3b Mon Sep 17 00:00:00 2001 From: binsky Date: Fri, 1 Oct 2021 16:55:05 +0200 Subject: [PATCH] move favicon data to new icon column if required Signed-off-by: binsky --- .../Version02031335Date20211001122343.php | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 lib/Migration/Version02031335Date20211001122343.php diff --git a/lib/Migration/Version02031335Date20211001122343.php b/lib/Migration/Version02031335Date20211001122343.php new file mode 100644 index 00000000..4d738fd5 --- /dev/null +++ b/lib/Migration/Version02031335Date20211001122343.php @@ -0,0 +1,89 @@ +connection = $connection; + } + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + */ + public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { + } + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * @return null|ISchemaWrapper + */ + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + if ($schema->hasTable('passman_credentials')) { + $table = $schema->getTable('passman_credentials'); + if ($table->hasColumn($this->oldColumn) && !$table->hasColumn($this->newColumn)) { + $table->addColumn($this->newColumn, 'text', [ + 'notnull' => false, + ]); + $this->dataMigrationRequired = true; + } + } + + return $schema; + } + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + */ + 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(); + + /** @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(); + } + } + } + } +}