Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Version02031335Date20211001122343.php « Migration « lib - github.com/nextcloud/passman.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4d738fd544e5d7afb4a33fe50cdcc426cf7552d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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();
				}
			}
		}
	}
}