Passed
Push — master ( 3145c0...db6a69 )
by Pauli
02:48
created

postSchemaChange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

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
rs 10
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.9.1 level from the v1.4.0 level, part 2
14
 */
15
class Version010901Date20231008194501 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->ampacheSessionChanges($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 fields to the `music_ampache_sessions` table
48
	 */
49
	private function ampacheSessionChanges(ISchemaWrapper $schema) {
50
		// On SQLite, it's not possible to add notnull columns to an existing table without a default value, see
51
		// https://stackoverflow.com/questions/3170634/cannot-add-a-not-null-column-with-default-value-null-in-sqlite3.
52
		// To work around this, we need to recreate the entire music_ampache_sessions table from scratch.
53
54
		// The previous table version was dropped in the previous script Version010900Date20231001225600 and here we create it anew.
55
		$table = $schema->createTable('music_ampache_sessions');
56
57
		$table->addColumn('id',					'integer',	['autoincrement' => true, 'notnull' => true, 'unsigned' => true]);
58
		$table->addColumn('user_id',			'string',	['notnull' => true, 'length' => 64]);
59
		$table->addColumn('token',				'string',	['notnull' => true, 'length' => 64]);
60
		$table->addColumn('expiry',				'integer',	['notnull' => true, 'unsigned' => true]);
61
		$table->addColumn('api_version',		'string',	['notnull' => false, 'length' => 16]);
62
		$table->addColumn('ampache_user_id',	'integer',	['notnull' => true, 'unsigned' => true]);
63
64
		$table->setPrimaryKey(['id']);
65
		$table->addUniqueIndex(['token'], 'music_ampache_sessions_index');
66
	}
67
}
68