Passed
Push — master ( 2de08e...4a9dd8 )
by Matias
29:45 queued 15:46
created

Version000515Date20200409003814::preSchemaChange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 1
ccs 0
cts 1
cp 0
crap 2
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace OCA\FaceRecognition\Migration;
5
6
use Closure;
7
8
use OCP\DB\ISchemaWrapper;
9
10
use OCP\IDBConnection;
11
use OCP\Migration\SimpleMigrationStep;
12
use OCP\Migration\IOutput;
13
14
class Version000515Date20200409003814 extends SimpleMigrationStep {
15
16
	/** @var IDBConnection */
17
	protected $connection;
18
19
	/**
20
	 * @param IDBConnection $connection
21
	 */
22
	public function __construct(IDBConnection $connection) {
23
		$this->connection = $connection;
24
	}
25
26
	/**
27
	 * @param IOutput $output
28
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
29
	 * @param array $options
30
	 */
31
	public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
32
	}
33
34
	/**
35
	 * @param IOutput $output
36
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
37
	 * @param array $options
38
	 * @return null|ISchemaWrapper
39
	 */
40
	public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
41
		$this->migratePreferencesKey('preferences', 'facerecognition', 'recreate-clusters', 'recreate_clusters');
42
		$this->migratePreferencesKey('preferences', 'facerecognition', 'force-create-clusters', 'force_create_clusters');
43
44
		$this->migratePreferencesKey('appconfig', 'facerecognition', 'handle-external-files', 'handle_external_files');
45
		$this->migratePreferencesKey('appconfig', 'facerecognition', 'handle-shared-files', 'handle_shared_files');
46
		$this->migratePreferencesKey('appconfig', 'facerecognition', 'min-confidence', 'min_confidence');
47
		$this->migratePreferencesKey('appconfig', 'facerecognition', 'show-not-grouped', 'show_not_grouped');
48
49
		$this->deletePreferencesKey('appconfig', 'facerecognition', 'memory-limits');
50
		$this->deletePreferencesKey('appconfig', 'facerecognition', 'queue-done');
51
		$this->deletePreferencesKey('appconfig', 'facerecognition', 'starttime');
52
	}
53
54
	/**
55
	 * @param IOutput $output
56
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
57
	 * @param array $options
58
	 */
59
	public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
60
	}
61
62
	protected function migratePreferencesKey($table, $appName, $key, $toKey) {
63
		$qb = $this->connection->getQueryBuilder();
64
		$qb->update($table)
65
			->set('configkey', $qb->createNamedParameter($toKey))
66
			->where($qb->expr()->eq('configkey', $qb->createNamedParameter($key)))
67
			->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter($appName)));
68
		$qb->execute();
69
	}
70
71
	protected function deletePreferencesKey($table, $appName, $key) {
72
		$qb = $this->connection->getQueryBuilder();
73
		$qb->delete($table)
74
			->where($qb->expr()->eq('appid', $qb->createNamedParameter($appName)))
75
			->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key)));
76
		$qb->execute();
77
	}
78
79
}
80