passman/lib/Migration/Version02031335Date20211001122343.php
binsky 1d75efad36 move favicon data to new icon column if required
Signed-off-by: binsky <timo@binsky.org>
2021-10-01 16:55:05 +02:00

90 lines
2.5 KiB
PHP

<?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();
}
}
}
}
}