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.0 level from the v1.4.0 level |
14
|
|
|
*/ |
15
|
|
|
class Version010900Date20231001225600 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->fixInconsistentIdTypes($schema); |
35
|
|
|
$this->allowNegativeYear($schema); |
36
|
|
|
$this->ampacheSessionChanges($schema); |
37
|
|
|
$this->addRatingFields($schema); |
38
|
|
|
return $schema; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param IOutput $output |
43
|
|
|
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
44
|
|
|
* @param array $options |
45
|
|
|
*/ |
46
|
|
|
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Some of the foreign keys referring to entity IDs have been previously defined as signed |
51
|
|
|
* although the referred primary key has always been unsigned. |
52
|
|
|
*/ |
53
|
|
|
private function fixInconsistentIdTypes(ISchemaWrapper $schema) { |
54
|
|
|
$schema->getTable('music_albums')->changeColumn('album_artist_id', ['unsigned' => true]); |
|
|
|
|
55
|
|
|
$schema->getTable('music_tracks')->changeColumn('artist_id', ['unsigned' => true]) |
|
|
|
|
56
|
|
|
->changeColumn('album_id', ['unsigned' => true]); |
57
|
|
|
$schema->getTable('music_bookmarks')->changeColumn('entry_id', ['unsigned' => true]); |
|
|
|
|
58
|
|
|
$schema->getTable('music_ampache_users')->changeColumn('id', ['unsigned' => true]); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Although untypical, it's not totally impossible that some historical piece of music would |
63
|
|
|
* be tagged with a negative year indicating a year BCE. |
64
|
|
|
*/ |
65
|
|
|
private function allowNegativeYear(ISchemaWrapper $schema) { |
66
|
|
|
$schema->getTable('music_tracks')->changeColumn('year', ['unsigned' => false]); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Add the new fields to the `music_ampache_sessions` table |
71
|
|
|
*/ |
72
|
|
|
private function ampacheSessionChanges(ISchemaWrapper $schema) { |
73
|
|
|
// On SQLite, it's not possible to add notnull columns to an existing table without a default value, see |
74
|
|
|
// https://stackoverflow.com/questions/3170634/cannot-add-a-not-null-column-with-default-value-null-in-sqlite3. |
75
|
|
|
// To work around this, we need to recreate the entire music_ampache_sessions table from scratch. |
76
|
|
|
$schema->dropTable('music_ampache_sessions'); |
77
|
|
|
$table = $schema->createTable('music_ampache_sessions'); |
78
|
|
|
|
79
|
|
|
$table->addColumn('id', 'integer', ['autoincrement' => true, 'notnull' => true, 'unsigned' => true]); |
80
|
|
|
$table->addColumn('user_id', 'string', ['notnull' => true, 'length' => 64]); |
81
|
|
|
$table->addColumn('token', 'string', ['notnull' => true, 'length' => 64]); |
82
|
|
|
$table->addColumn('expiry', 'integer', ['notnull' => true, 'unsigned' => true]); |
83
|
|
|
$table->addColumn('api_version', 'string', ['notnull' => false, 'length' => 16]); |
84
|
|
|
$table->addColumn('ampache_user_id', 'integer', ['notnull' => true, 'unsigned' => true]); |
85
|
|
|
|
86
|
|
|
$table->setPrimaryKey(['id']); |
87
|
|
|
$table->addUniqueIndex(['token'], 'music_ampache_sessions_index'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Add the new field 'rating' to applicable tables |
92
|
|
|
*/ |
93
|
|
|
private function addRatingFields(ISchemaWrapper $schema) { |
94
|
|
|
$tableNames = [ |
95
|
|
|
'music_artists', |
96
|
|
|
'music_albums', |
97
|
|
|
'music_tracks', |
98
|
|
|
'music_playlists', |
99
|
|
|
'music_podcast_channels', |
100
|
|
|
'music_podcast_episodes' |
101
|
|
|
]; |
102
|
|
|
|
103
|
|
|
foreach ($tableNames as $tableName) { |
104
|
|
|
$table = $schema->getTable($tableName); |
105
|
|
|
$this->setColumn($table, 'rating', 'integer', ['notnull' => true, 'default' => 0]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Also, add 'starred' field for playlists |
109
|
|
|
$this->setColumn($schema->getTable('music_playlists'), 'starred', 'datetime', ['notnull' => false]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function setColumn($table, string $name, string $type, array $args) { |
113
|
|
|
if (!$table->hasColumn($name)) { |
114
|
|
|
$table->addColumn($name, $type, $args); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.