Passed
Push — master ( 3c5a6f...e94730 )
by Pauli
09:42 queued 11s
created

migrateMusicTracks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
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 v1.4.0 level from the v1.3.x level
14
 */
15
class Version010400Date20211002223000 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->migrateMusicTracks($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
	private function migrateMusicTracks(ISchemaWrapper $schema) {
47
		$table = $schema->getTable('music_tracks');
48
		$this->setColumns($table, [
49
			[ 'play_count',		'integer',	['notnull' => true, 'unsigned' => true] ],
50
			[ 'last_played',	'datetime', ['notnull' => false] ]
51
		]);
52
	}
53
54
	private function setColumn($table, string $name, string $type, array $args) {
55
		if (!$table->hasColumn($name)) {
56
			$table->addColumn($name, $type, $args);
57
		}
58
	}
59
60
	private function setColumns($table, array $nameTypeArgsPerCol) {
61
		foreach ($nameTypeArgsPerCol as $nameTypeArgs) {
62
			list($name, $type, $args) = $nameTypeArgs;
63
			$this->setColumn($table, $name, $type, $args);
64
		}
65
	}
66
67
}
68