Passed
Push — master ( c7cb4e...ce9a66 )
by Pauli
03:03
created

Version020100Date20241114220300::setColumn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 4
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace OCA\Music\Migration;
6
7
use Closure;
8
use OCP\DB\ISchemaWrapper;
9
use OCP\Migration\SimpleMigrationStep;
10
use OCP\Migration\IOutput;
11
12
/**
13
 * Migrate the DB schema to Music v2.1.0 level from the v1.9.1 level
14
 */
15
class Version020100Date20241114220300 extends SimpleMigrationStep {
16
17
	/**
18
	 * @param IOutput $output
19
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
20
	 * @param array $options
21
	 */
22
	public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
23
	}
24
25
	/**
26
	 * @param IOutput $output
27
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
28
	 * @param array $options
29
	 * @return null|ISchemaWrapper
30
	 */
31
	public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
32
		/** @var ISchemaWrapper $schema */
33
		$schema = $schemaClosure();
34
		$this->addDirtyFieldToTrack($schema);
35
		return $schema;
36
	}
37
38
	/**
39
	 * @param IOutput $output
40
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
41
	 * @param array $options
42
	 */
43
	public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
44
	}
45
46
	/**
47
	 * Add the new field 'dirty' to the table 'music_tracks'
48
	 */
49
	private function addDirtyFieldToTrack(ISchemaWrapper $schema) {
50
		$table = $schema->getTable('music_tracks');
51
		$this->setColumn($table, 'dirty', 'smallint', ['notnull' => true, 'default' => 0]);
52
	}
53
54
	private function setColumn($table, string $name, string $type, array $args) : void {
55
		if (!$table->hasColumn($name)) {
56
			$table->addColumn($name, $type, $args);
57
		}
58
	}
59
}
60