diff options
author | binsky <timo@binsky.org> | 2021-10-01 17:55:05 +0300 |
---|---|---|
committer | binsky <timo@binsky.org> | 2021-10-01 17:55:05 +0300 |
commit | 1d75efad36600e267a38825f3c4d2e6b63a2eb3b (patch) | |
tree | 64638e8a8c7f73791b6891b080689261e5075f99 | |
parent | 61881b1c233d0d70372e27215e78ea9ac5c35560 (diff) |
move favicon data to new icon column if required
Signed-off-by: binsky <timo@binsky.org>
-rw-r--r-- | lib/Migration/Version02031335Date20211001122343.php | 89 |
1 files changed, 89 insertions, 0 deletions
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 @@ +<?php + +declare(strict_types=1); + +namespace OCA\Passman\Migration; + +use Closure; +use OCP\DB\ISchemaWrapper; +use OCP\IDBConnection; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +/** + * move favicon data to new icon column (for old installations) + */ +class Version02031335Date20211001122343 extends SimpleMigrationStep { + + /** @var IDBConnection */ + protected $connection; + + protected $oldColumn = 'favicon'; + protected $newColumn = 'icon'; + protected $dataMigrationRequired = false; + + /** + * @param IDBConnection $connection + */ + public function __construct(IDBConnection $connection) { + $this->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(); + } + } + } + } +} |