Passed
Push — master ( aeae18...ff96ad )
by Pauli
02:20
created

Version010000Date20210903000000   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 256
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 27
eloc 152
c 1
b 0
f 0
dl 0
loc 256
rs 10

20 Methods

Rating   Name   Duplication   Size   Complexity  
A migrateMusicBookmarks() 0 14 1
A setUniqueIndex() 0 3 2
A setIndex() 0 3 2
A changeSchema() 0 16 1
A setColumns() 0 4 2
A migrateMusicTracks() 0 27 1
A setColumn() 0 3 2
A migrateMusicAlbums() 0 22 1
A preSchemaChange() 0 1 1
A postSchemaChange() 0 1 1
A migrateMusicArtists() 0 17 1
A getOrCreateTable() 0 5 2
A dropObsoleteColumn() 0 3 2
A migrateMusicCache() 0 11 1
A migrateMusicRadioStations() 0 13 1
A migrateAmpacheUsers() 0 11 1
A migrateMusicAmpacheSessions() 0 11 1
A migrateMusicGenres() 0 13 1
A migrateMusicPlaylists() 0 13 1
A setPrimaryKey() 0 3 2
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.0.0 level from any previous version starting from v0.4.0
14
 */
15
class Version010000Date20210903000000 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
35
		$this->migrateMusicArtists($schema);
36
		$this->migrateMusicAlbums($schema);
37
		$this->migrateMusicTracks($schema);
38
		$this->migrateMusicPlaylists($schema);
39
		$this->migrateMusicGenres($schema);
40
		$this->migrateMusicRadioStations($schema);
41
		$this->migrateMusicAmpacheSessions($schema);
42
		$this->migrateAmpacheUsers($schema);
43
		$this->migrateMusicCache($schema);
44
		$this->migrateMusicBookmarks($schema);
45
46
		return $schema;
47
	}
48
49
	/**
50
	 * @param IOutput $output
51
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
52
	 * @param array $options
53
	 */
54
	public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
55
	}
56
57
	private function migrateMusicArtists(ISchemaWrapper $schema) {
58
		$table = $this->getOrCreateTable($schema, 'music_artists');
59
		$this->setColumns($table, [
60
			[ 'id',				'integer',	['autoincrement' => true, 'notnull' => true, 'unsigned' => true] ],
61
			[ 'user_id',		'string',	['notnull' => true, 'length' => 64] ],
62
			[ 'name',			'string',	['notnull' => false, 'length' => 256] ],
63
			[ 'cover_file_id',	'bigint',	['notnull' => false,'length' => 10] ],
64
			[ 'mbid',			'string',	['notnull' => false, 'length' => 36] ],
65
			[ 'hash',			'string',	['notnull' => true, 'length' => 32] ],
66
			[ 'starred',		'datetime',	['notnull' => false] ],
67
			[ 'created',		'datetime',	['notnull' => false] ],
68
			[ 'updated',		'datetime',	['notnull' => false] ]
69
		]);
70
		$this->dropObsoleteColumn($table, 'image');
71
72
		$this->setPrimaryKey($table, ['id']);
73
		$this->setUniqueIndex($table, 'user_id_hash_idx', ['user_id', 'hash']);
74
	}
75
76
	private function migrateMusicAlbums(ISchemaWrapper $schema) {
77
		$table = $this->getOrCreateTable($schema, 'music_albums');
78
		$this->setColumns($table, [
79
			[ 'id',					'integer',	['autoincrement' => true, 'notnull' => true, 'unsigned' => true] ],
80
			[ 'user_id',			'string',	['notnull' => true, 'length' => 64] ],
81
			[ 'name',				'string',	['notnull' => false, 'length' => 256] ],
82
			[ 'cover_file_id',		'bigint',	['notnull' => false, 'length' => 10] ],
83
			[ 'mbid',				'string',	['notnull' => false, 'length' => 36] ],
84
			[ 'disk',				'integer',	['notnull' => false, 'unsigned' => true] ],
85
			[ 'mbid_group',			'string',	['notnull' => false, 'length' => 36] ],
86
			[ 'album_artist_id',	'integer',	['notnull' => false] ],
87
			[ 'hash',				'string',	['notnull' => true, 'length' => 32] ],
88
			[ 'starred',			'datetime',	['notnull' => false] ],
89
			[ 'created',			'datetime',	['notnull' => false] ],
90
			[ 'updated',			'datetime',	['notnull' => false] ]
91
		]);
92
		$this->dropObsoleteColumn($table, 'year');
93
94
		$this->setPrimaryKey($table, ['id']);
95
		$this->setIndex($table, 'ma_cover_file_id_idx', ['cover_file_id']);
96
		$this->setIndex($table, 'ma_album_artist_id_idx', ['album_artist_id']);
97
		$this->setUniqueIndex($table, 'ma_user_id_hash_idx', ['user_id', 'hash']);
98
	}
99
100
	private function migrateMusicTracks(ISchemaWrapper $schema) {
101
		$table = $this->getOrCreateTable($schema, 'music_tracks');
102
		$this->setColumns($table, [
103
			[ 'id',			'integer',	['autoincrement' => true, 'notnull' => true, 'unsigned' => true, ] ],
104
			[ 'user_id',	'string',	['notnull' => true, 'length' => 64, ] ],
105
			[ 'title',		'string',	['notnull' => true, 'length' => 256, ] ],
106
			[ 'number',		'integer',	['notnull' => false, 'unsigned' => true] ],
107
			[ 'disk',		'integer',	['notnull' => false, 'unsigned' => true] ],
108
			[ 'year',		'integer',	['notnull' => false, 'unsigned' => true] ],
109
			[ 'artist_id',	'integer',	['notnull' => false] ],
110
			[ 'album_id',	'integer',	['notnull' => false] ],
111
			[ 'length',		'integer',	['notnull' => false, 'unsigned' => true] ],
112
			[ 'file_id',	'bigint',	['notnull' => true, 'length' => 10] ],
113
			[ 'bitrate',	'integer',	['notnull' => false, 'unsigned' => true] ],
114
			[ 'mimetype',	'string',	['notnull' => true, 'length' => 256] ],
115
			[ 'mbid',		'string',	['notnull' => false, 'length' => 36] ],
116
			[ 'starred',	'datetime',	['notnull' => false] ],
117
			[ 'genre_id',	'integer',	['notnull' => false, 'unsigned' => true, ] ],
118
			[ 'created',	'datetime', ['notnull' => false] ],
119
			[ 'updated',	'datetime', ['notnull' => false] ]
120
		]);
121
122
		$this->setPrimaryKey($table, ['id']);
123
		$this->setIndex($table, 'music_tracks_artist_id_idx', ['artist_id']);
124
		$this->setIndex($table, 'music_tracks_album_id_idx', ['album_id']);
125
		$this->setIndex($table, 'music_tracks_user_id_idx', ['user_id']);
126
		$this->setUniqueIndex($table, 'music_tracks_file_user_id_idx', ['file_id', 'user_id']);
127
	}
128
129
	private function migrateMusicPlaylists(ISchemaWrapper $schema) {
130
		$table = $this->getOrCreateTable($schema, 'music_playlists');
131
		$this->setColumns($table, [
132
			[ 'id',			'integer',	['autoincrement' => true, 'notnull' => true, 'unsigned' => true] ],
133
			[ 'user_id',	'string',	['notnull' => true, 'length' => 64] ],
134
			[ 'name',		'string',	['notnull' => false, 'length' => 256] ],
135
			[ 'track_ids',	'text',		['notnull' => false] ],
136
			[ 'created',	'datetime',	['notnull' => false] ],
137
			[ 'updated',	'datetime',	['notnull' => false] ],
138
			[ 'comment',	'string',	['notnull' => false, 'length' => 256] ]
139
		]);
140
141
		$this->setPrimaryKey($table, ['id']);
142
	}
143
144
	private function migrateMusicGenres(ISchemaWrapper $schema) {
145
		$table = $this->getOrCreateTable($schema, 'music_genres');
146
		$this->setColumns($table, [
147
			[ 'id',			'integer',	['autoincrement' => true, 'notnull' => true, 'unsigned' => true] ],
148
			[ 'user_id',	'string',	['notnull' => true, 'length' => 64] ],
149
			[ 'name',		'string',	['notnull' => false, 'length' => 64] ],
150
			[ 'lower_name',	'string',	['notnull' => false, 'length' => 64] ],
151
			[ 'created',	'datetime',	['notnull' => false] ],
152
			[ 'updated',	'datetime',	['notnull' => false] ]
153
		]);
154
155
		$this->setPrimaryKey($table, ['id']);
156
		$this->setUniqueIndex($table, 'mg_lower_name_user_id_idx', ['lower_name', 'user_id']);
157
	}
158
159
	private function migrateMusicRadioStations(ISchemaWrapper $schema) {
160
		$table = $this->getOrCreateTable($schema, 'music_radio_stations');
161
		$this->setColumns($table, [
162
			[ 'id', 		'integer',	['autoincrement' => true, 'notnull' => true, 'unsigned' => true] ],
163
			[ 'user_id',	'string',	['notnull' => true, 'length' => 64] ],
164
			[ 'name',		'string',	['notnull' => false, 'length' => 256] ],
165
			[ 'stream_url',	'string',	['notnull' => true, 'length' => 2048] ],
166
			[ 'home_url',	'string',	['notnull' => false, 'length' => 2048] ],
167
			[ 'created',	'datetime',	['notnull' => false] ],
168
			[ 'updated',	'datetime',	['notnull' => false] ]
169
		]);
170
171
		$this->setPrimaryKey($table, ['id']);
172
	}
173
174
	private function migrateMusicAmpacheSessions(ISchemaWrapper $schema) {
175
		$table = $this->getOrCreateTable($schema, 'music_ampache_sessions');
176
		$this->setColumns($table, [
177
			[ 'id',			'integer',	['autoincrement' => true, 'notnull' => true, 'unsigned' => true] ],
178
			[ 'user_id',	'string',	['notnull' => true, 'length' => 64] ],
179
			[ 'token',		'string',	['notnull' => true, 'length' => 64] ],
180
			[ 'expiry',		'integer',	['notnull' => true,'unsigned' => true] ]
181
		]);
182
183
		$this->setPrimaryKey($table, ['id']);
184
		$this->setUniqueIndex($table, 'music_ampache_sessions_index', ['token']);
185
	}
186
187
	private function migrateAmpacheUsers(ISchemaWrapper $schema) {
188
		$table = $this->getOrCreateTable($schema, 'music_ampache_users');
189
		$this->setColumns($table, [
190
			[ 'id',				'integer',	['autoincrement' => true, 'notnull' => true, 'length' => 4] ],
191
			[ 'user_id',		'string',	['notnull' => true, 'length' => 64] ],
192
			[ 'description',	'string',	['notnull' => false, 'length' => 64] ],
193
			[ 'hash',			'string',	['notnull' => true, 'length' => 64] ]
194
		]);
195
196
		$this->setPrimaryKey($table, ['id']);
197
		$this->setUniqueIndex($table, 'music_ampache_users_index', ['hash', 'user_id']);
198
	}
199
200
	private function migrateMusicCache(ISchemaWrapper $schema) {
201
		$table = $this->getOrCreateTable($schema, 'music_cache');
202
		$this->setColumns($table, [
203
			[ 'id',			'integer',	['autoincrement' => true, 'notnull' => true, 'unsigned' => true] ],
204
			[ 'key',		'string',	['notnull' => true, 'length' => 64] ],
205
			[ 'user_id',	'string',	['notnull' => true, 'length' => 64] ],
206
			[ 'data',		'text',		['notnull' => false] ],
207
		]);
208
209
		$this->setPrimaryKey($table, ['id']);
210
		$this->setUniqueIndex($table, 'music_cache_index', ['user_id', 'key']);
211
	}
212
213
	private function migrateMusicBookmarks(ISchemaWrapper $schema) {
214
		$table = $this->getOrCreateTable($schema, 'music_bookmarks');
215
		$this->setColumns($table, [
216
			[ 'id',			'integer',		['autoincrement' => true, 'notnull' => true, 'unsigned' => true] ],
217
			[ 'user_id',	'string',		['notnull' => true, 'length' => 64] ],
218
			[ 'track_id',	'integer',		['notnull' => true] ],
219
			[ 'position',	'integer',		['notnull' => true] ],
220
			[ 'comment',	'string',		['notnull' => false, 'length' => 256] ],
221
			[ 'created',	'datetime',		['notnull' => false] ],
222
			[ 'updated',	'datetime',		['notnull' => false] ]
223
		]);
224
225
		$this->setPrimaryKey($table, ['id']);
226
		$this->setUniqueIndex($table, 'music_bookmarks_user_track', ['user_id', 'track_id']);
227
	}
228
229
	private function getOrCreateTable(ISchemaWrapper $schema, string $name) {
230
		if (!$schema->hasTable($name)) {
231
			return $schema->createTable($name);
232
		} else {
233
			return $schema->getTable($name);
234
		}
235
	}
236
237
	private function setColumn($table, string $name, string $type, array $args) {
238
		if (!$table->hasColumn($name)) {
239
			$table->addColumn($name, $type, $args);
240
		}
241
	}
242
243
	private function setColumns($table, array $nameTypeArgsPerCol) {
244
		foreach ($nameTypeArgsPerCol as $nameTypeArgs) {
245
			list($name, $type, $args) = $nameTypeArgs;
246
			$this->setColumn($table, $name, $type, $args);
247
		}
248
	}
249
250
	private function dropObsoleteColumn($table, string $name) {
251
		if ($table->hasColumn($name)) {
252
			$table->dropColumn($name);
253
		}
254
	}
255
256
	private function setIndex($table, string $name, array $columns) {
257
		if (!$table->hasIndex($name)) {
258
			$table->addIndex($columns, $name);
259
		}
260
	}
261
262
	private function setUniqueIndex($table, string $name, array $columns) {
263
		if (!$table->hasIndex($name)) {
264
			$table->addUniqueIndex($columns, $name);
265
		}
266
	}
267
268
	private function setPrimaryKey($table, array $columns) {
269
		if (!$table->hasPrimaryKey()) {
270
			$table->setPrimaryKey($columns);
271
		}
272
	}
273
}
274