diff --git a/lib/Migration/Version010000Date20210903000000.php b/lib/Migration/Version010000Date20210903000000.php new file mode 100644 index 000000000..b09df0d0c --- /dev/null +++ b/lib/Migration/Version010000Date20210903000000.php @@ -0,0 +1,273 @@ +migrateMusicArtists($schema); + $this->migrateMusicAlbums($schema); + $this->migrateMusicTracks($schema); + $this->migrateMusicPlaylists($schema); + $this->migrateMusicGenres($schema); + $this->migrateMusicRadioStations($schema); + $this->migrateMusicAmpacheSessions($schema); + $this->migrateAmpacheUsers($schema); + $this->migrateMusicCache($schema); + $this->migrateMusicBookmarks($schema); + + return $schema; + } + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + */ + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { + } + + private function migrateMusicArtists(ISchemaWrapper $schema) { + $table = $this->getOrCreateTable($schema, 'music_artists'); + $this->setColumns($table, [ + [ 'id', 'integer', ['autoincrement' => true, 'notnull' => true, 'unsigned' => true] ], + [ 'user_id', 'string', ['notnull' => true, 'length' => 64] ], + [ 'name', 'string', ['notnull' => false, 'length' => 256] ], + [ 'cover_file_id', 'bigint', ['notnull' => false,'length' => 10] ], + [ 'mbid', 'string', ['notnull' => false, 'length' => 36] ], + [ 'hash', 'string', ['notnull' => true, 'length' => 32] ], + [ 'starred', 'datetime', ['notnull' => false] ], + [ 'created', 'datetime', ['notnull' => false] ], + [ 'updated', 'datetime', ['notnull' => false] ] + ]); + $this->dropObsoleteColumn($table, 'image'); + + $this->setPrimaryKey($table, ['id']); + $this->setUniqueIndex($table, 'user_id_hash_idx', ['user_id', 'hash']); + } + + private function migrateMusicAlbums(ISchemaWrapper $schema) { + $table = $this->getOrCreateTable($schema, 'music_albums'); + $this->setColumns($table, [ + [ 'id', 'integer', ['autoincrement' => true, 'notnull' => true, 'unsigned' => true] ], + [ 'user_id', 'string', ['notnull' => true, 'length' => 64] ], + [ 'name', 'string', ['notnull' => false, 'length' => 256] ], + [ 'cover_file_id', 'bigint', ['notnull' => false, 'length' => 10] ], + [ 'mbid', 'string', ['notnull' => false, 'length' => 36] ], + [ 'disk', 'integer', ['notnull' => false, 'unsigned' => true] ], + [ 'mbid_group', 'string', ['notnull' => false, 'length' => 36] ], + [ 'album_artist_id', 'integer', ['notnull' => false] ], + [ 'hash', 'string', ['notnull' => true, 'length' => 32] ], + [ 'starred', 'datetime', ['notnull' => false] ], + [ 'created', 'datetime', ['notnull' => false] ], + [ 'updated', 'datetime', ['notnull' => false] ] + ]); + $this->dropObsoleteColumn($table, 'year'); + + $this->setPrimaryKey($table, ['id']); + $this->setIndex($table, 'ma_cover_file_id_idx', ['cover_file_id']); + $this->setIndex($table, 'ma_album_artist_id_idx', ['album_artist_id']); + $this->setUniqueIndex($table, 'ma_user_id_hash_idx', ['user_id', 'hash']); + } + + private function migrateMusicTracks(ISchemaWrapper $schema) { + $table = $this->getOrCreateTable($schema, 'music_tracks'); + $this->setColumns($table, [ + [ 'id', 'integer', ['autoincrement' => true, 'notnull' => true, 'unsigned' => true, ] ], + [ 'user_id', 'string', ['notnull' => true, 'length' => 64, ] ], + [ 'title', 'string', ['notnull' => true, 'length' => 256, ] ], + [ 'number', 'integer', ['notnull' => false, 'unsigned' => true] ], + [ 'disk', 'integer', ['notnull' => false, 'unsigned' => true] ], + [ 'year', 'integer', ['notnull' => false, 'unsigned' => true] ], + [ 'artist_id', 'integer', ['notnull' => false] ], + [ 'album_id', 'integer', ['notnull' => false] ], + [ 'length', 'integer', ['notnull' => false, 'unsigned' => true] ], + [ 'file_id', 'bigint', ['notnull' => true, 'length' => 10] ], + [ 'bitrate', 'integer', ['notnull' => false, 'unsigned' => true] ], + [ 'mimetype', 'string', ['notnull' => true, 'length' => 256] ], + [ 'mbid', 'string', ['notnull' => false, 'length' => 36] ], + [ 'starred', 'datetime', ['notnull' => false] ], + [ 'genre_id', 'integer', ['notnull' => false, 'unsigned' => true, ] ], + [ 'created', 'datetime', ['notnull' => false] ], + [ 'updated', 'datetime', ['notnull' => false] ] + ]); + + $this->setPrimaryKey($table, ['id']); + $this->setIndex($table, 'music_tracks_artist_id_idx', ['artist_id']); + $this->setIndex($table, 'music_tracks_album_id_idx', ['album_id']); + $this->setIndex($table, 'music_tracks_user_id_idx', ['user_id']); + $this->setUniqueIndex($table, 'music_tracks_file_user_id_idx', ['file_id', 'user_id']); + } + + private function migrateMusicPlaylists(ISchemaWrapper $schema) { + $table = $this->getOrCreateTable($schema, 'music_playlists'); + $this->setColumns($table, [ + [ 'id', 'integer', ['autoincrement' => true, 'notnull' => true, 'unsigned' => true] ], + [ 'user_id', 'string', ['notnull' => true, 'length' => 64] ], + [ 'name', 'string', ['notnull' => false, 'length' => 256] ], + [ 'track_ids', 'text', ['notnull' => false] ], + [ 'created', 'datetime', ['notnull' => false] ], + [ 'updated', 'datetime', ['notnull' => false] ], + [ 'comment', 'string', ['notnull' => false, 'length' => 256] ] + ]); + + $this->setPrimaryKey($table, ['id']); + } + + private function migrateMusicGenres(ISchemaWrapper $schema) { + $table = $this->getOrCreateTable($schema, 'music_genres'); + $this->setColumns($table, [ + [ 'id', 'integer', ['autoincrement' => true, 'notnull' => true, 'unsigned' => true] ], + [ 'user_id', 'string', ['notnull' => true, 'length' => 64] ], + [ 'name', 'string', ['notnull' => false, 'length' => 64] ], + [ 'lower_name', 'string', ['notnull' => false, 'length' => 64] ], + [ 'created', 'datetime', ['notnull' => false] ], + [ 'updated', 'datetime', ['notnull' => false] ] + ]); + + $this->setPrimaryKey($table, ['id']); + $this->setUniqueIndex($table, 'mg_lower_name_user_id_idx', ['lower_name', 'user_id']); + } + + private function migrateMusicRadioStations(ISchemaWrapper $schema) { + $table = $this->getOrCreateTable($schema, 'music_radio_stations'); + $this->setColumns($table, [ + [ 'id', 'integer', ['autoincrement' => true, 'notnull' => true, 'unsigned' => true] ], + [ 'user_id', 'string', ['notnull' => true, 'length' => 64] ], + [ 'name', 'string', ['notnull' => false, 'length' => 256] ], + [ 'stream_url', 'string', ['notnull' => true, 'length' => 2048] ], + [ 'home_url', 'string', ['notnull' => false, 'length' => 2048] ], + [ 'created', 'datetime', ['notnull' => false] ], + [ 'updated', 'datetime', ['notnull' => false] ] + ]); + + $this->setPrimaryKey($table, ['id']); + } + + private function migrateMusicAmpacheSessions(ISchemaWrapper $schema) { + $table = $this->getOrCreateTable($schema, 'music_ampache_sessions'); + $this->setColumns($table, [ + [ 'id', 'integer', ['autoincrement' => true, 'notnull' => true, 'unsigned' => true] ], + [ 'user_id', 'string', ['notnull' => true, 'length' => 64] ], + [ 'token', 'string', ['notnull' => true, 'length' => 64] ], + [ 'expiry', 'integer', ['notnull' => true,'unsigned' => true] ] + ]); + + $this->setPrimaryKey($table, ['id']); + $this->setUniqueIndex($table, 'music_ampache_sessions_index', ['token']); + } + + private function migrateAmpacheUsers(ISchemaWrapper $schema) { + $table = $this->getOrCreateTable($schema, 'music_ampache_users'); + $this->setColumns($table, [ + [ 'id', 'integer', ['autoincrement' => true, 'notnull' => true, 'length' => 4] ], + [ 'user_id', 'string', ['notnull' => true, 'length' => 64] ], + [ 'description', 'string', ['notnull' => false, 'length' => 64] ], + [ 'hash', 'string', ['notnull' => true, 'length' => 64] ] + ]); + + $this->setPrimaryKey($table, ['id']); + $this->setUniqueIndex($table, 'music_ampache_users_index', ['hash', 'user_id']); + } + + private function migrateMusicCache(ISchemaWrapper $schema) { + $table = $this->getOrCreateTable($schema, 'music_cache'); + $this->setColumns($table, [ + [ 'id', 'integer', ['autoincrement' => true, 'notnull' => true, 'unsigned' => true] ], + [ 'key', 'string', ['notnull' => true, 'length' => 64] ], + [ 'user_id', 'string', ['notnull' => true, 'length' => 64] ], + [ 'data', 'text', ['notnull' => false] ], + ]); + + $this->setPrimaryKey($table, ['id']); + $this->setUniqueIndex($table, 'music_cache_index', ['user_id', 'key']); + } + + private function migrateMusicBookmarks(ISchemaWrapper $schema) { + $table = $this->getOrCreateTable($schema, 'music_bookmarks'); + $this->setColumns($table, [ + [ 'id', 'integer', ['autoincrement' => true, 'notnull' => true, 'unsigned' => true] ], + [ 'user_id', 'string', ['notnull' => true, 'length' => 64] ], + [ 'track_id', 'integer', ['notnull' => true] ], + [ 'position', 'integer', ['notnull' => true] ], + [ 'comment', 'string', ['notnull' => false, 'length' => 256] ], + [ 'created', 'datetime', ['notnull' => false] ], + [ 'updated', 'datetime', ['notnull' => false] ] + ]); + + $this->setPrimaryKey($table, ['id']); + $this->setUniqueIndex($table, 'music_bookmarks_user_track', ['user_id', 'track_id']); + } + + private function getOrCreateTable(ISchemaWrapper $schema, string $name) { + if (!$schema->hasTable($name)) { + return $schema->createTable($name); + } else { + return $schema->getTable($name); + } + } + + private function setColumn($table, string $name, string $type, array $args) { + if (!$table->hasColumn($name)) { + $table->addColumn($name, $type, $args); + } + } + + private function setColumns($table, array $nameTypeArgsPerCol) { + foreach ($nameTypeArgsPerCol as $nameTypeArgs) { + list($name, $type, $args) = $nameTypeArgs; + $this->setColumn($table, $name, $type, $args); + } + } + + private function dropObsoleteColumn($table, string $name) { + if ($table->hasColumn($name)) { + $table->dropColumn($name); + } + } + + private function setIndex($table, string $name, array $columns) { + if (!$table->hasIndex($name)) { + $table->addIndex($columns, $name); + } + } + + private function setUniqueIndex($table, string $name, array $columns) { + if (!$table->hasIndex($name)) { + $table->addUniqueIndex($columns, $name); + } + } + + private function setPrimaryKey($table, array $columns) { + if (!$table->hasPrimaryKey()) { + $table->setPrimaryKey($columns); + } + } +} diff --git a/lib/Migration/Version010200Date20210513171803.php b/lib/Migration/Version010200Date20210513171803.php deleted file mode 100644 index 5b3d1c7bc..000000000 --- a/lib/Migration/Version010200Date20210513171803.php +++ /dev/null @@ -1,401 +0,0 @@ -hasTable('music_artists')) { - $table = $schema->createTable('music_artists'); - $table->addColumn('id', 'integer', [ - 'autoincrement' => true, - 'notnull' => true, - 'unsigned' => true, - ]); - $table->addColumn('user_id', 'string', [ - 'notnull' => true, - 'length' => 64, - ]); - $table->addColumn('name', 'string', [ - 'notnull' => false, - 'length' => 256, - ]); - $table->addColumn('cover_file_id', 'bigint', [ - 'notnull' => false, - 'length' => 10, - ]); - $table->addColumn('mbid', 'string', [ - 'notnull' => false, - 'length' => 36, - ]); - $table->addColumn('hash', 'string', [ - 'notnull' => true, - 'length' => 32, - ]); - $table->addColumn('starred', 'datetime', [ - 'notnull' => false, - ]); - $table->addColumn('created', 'datetime', [ - 'notnull' => false, - ]); - $table->addColumn('updated', 'datetime', [ - 'notnull' => false, - ]); - $table->setPrimaryKey(['id']); - $table->addUniqueIndex(['user_id', 'hash'], 'user_id_hash_idx'); - } - - if (!$schema->hasTable('music_albums')) { - $table = $schema->createTable('music_albums'); - $table->addColumn('id', 'integer', [ - 'autoincrement' => true, - 'notnull' => true, - 'unsigned' => true, - ]); - $table->addColumn('user_id', 'string', [ - 'notnull' => true, - 'length' => 64, - ]); - $table->addColumn('name', 'string', [ - 'notnull' => false, - 'length' => 256, - ]); - $table->addColumn('cover_file_id', 'bigint', [ - 'notnull' => false, - 'length' => 10, - ]); - $table->addColumn('mbid', 'string', [ - 'notnull' => false, - 'length' => 36, - ]); - $table->addColumn('disk', 'integer', [ - 'notnull' => false, - 'unsigned' => true, - ]); - $table->addColumn('mbid_group', 'string', [ - 'notnull' => false, - 'length' => 36, - ]); - $table->addColumn('album_artist_id', 'integer', [ - 'notnull' => false, - ]); - $table->addColumn('hash', 'string', [ - 'notnull' => true, - 'length' => 32, - ]); - $table->addColumn('starred', 'datetime', [ - 'notnull' => false, - ]); - $table->addColumn('created', 'datetime', [ - 'notnull' => false, - ]); - $table->addColumn('updated', 'datetime', [ - 'notnull' => false, - ]); - $table->setPrimaryKey(['id']); - $table->addIndex(['cover_file_id'], 'ma_cover_file_id_idx'); - $table->addIndex(['album_artist_id'], 'ma_album_artist_id_idx'); - $table->addUniqueIndex(['user_id', 'hash'], 'ma_user_id_hash_idx'); - } - - if (!$schema->hasTable('music_tracks')) { - $table = $schema->createTable('music_tracks'); - $table->addColumn('id', 'integer', [ - 'autoincrement' => true, - 'notnull' => true, - 'unsigned' => true, - ]); - $table->addColumn('user_id', 'string', [ - 'notnull' => true, - 'length' => 64, - ]); - $table->addColumn('title', 'string', [ - 'notnull' => true, - 'length' => 256, - ]); - $table->addColumn('number', 'integer', [ - 'notnull' => false, - 'unsigned' => true, - ]); - $table->addColumn('disk', 'integer', [ - 'notnull' => false, - 'unsigned' => true, - ]); - $table->addColumn('year', 'integer', [ - 'notnull' => false, - 'unsigned' => true, - ]); - $table->addColumn('artist_id', 'integer', [ - 'notnull' => false, - ]); - $table->addColumn('album_id', 'integer', [ - 'notnull' => false, - ]); - $table->addColumn('length', 'integer', [ - 'notnull' => false, - 'unsigned' => true, - ]); - $table->addColumn('file_id', 'bigint', [ - 'notnull' => true, - 'length' => 10, - ]); - $table->addColumn('bitrate', 'integer', [ - 'notnull' => false, - 'unsigned' => true, - ]); - $table->addColumn('mimetype', 'string', [ - 'notnull' => true, - 'length' => 256, - ]); - $table->addColumn('mbid', 'string', [ - 'notnull' => false, - 'length' => 36, - ]); - $table->addColumn('starred', 'datetime', [ - 'notnull' => false, - ]); - $table->addColumn('genre_id', 'integer', [ - 'notnull' => false, - 'unsigned' => true, - ]); - $table->addColumn('created', 'datetime', [ - 'notnull' => false, - ]); - $table->addColumn('updated', 'datetime', [ - 'notnull' => false, - ]); - $table->setPrimaryKey(['id']); - $table->addIndex(['artist_id'], 'music_tracks_artist_id_idx'); - $table->addIndex(['album_id'], 'music_tracks_album_id_idx'); - $table->addIndex(['user_id'], 'music_tracks_user_id_idx'); - $table->addUniqueIndex(['file_id', 'user_id'], 'music_tracks_file_user_id_idx'); - } - - if (!$schema->hasTable('music_playlists')) { - $table = $schema->createTable('music_playlists'); - $table->addColumn('id', 'integer', [ - 'autoincrement' => true, - 'notnull' => true, - 'unsigned' => true, - ]); - $table->addColumn('user_id', 'string', [ - 'notnull' => true, - 'length' => 64, - ]); - $table->addColumn('name', 'string', [ - 'notnull' => false, - 'length' => 256, - ]); - $table->addColumn('track_ids', 'text', [ - 'notnull' => false, - ]); - $table->addColumn('created', 'datetime', [ - 'notnull' => false, - ]); - $table->addColumn('updated', 'datetime', [ - 'notnull' => false, - ]); - $table->addColumn('comment', 'string', [ - 'notnull' => false, - 'length' => 256, - ]); - $table->setPrimaryKey(['id']); - } - - if (!$schema->hasTable('music_genres')) { - $table = $schema->createTable('music_genres'); - $table->addColumn('id', 'integer', [ - 'autoincrement' => true, - 'notnull' => true, - 'unsigned' => true, - ]); - $table->addColumn('user_id', 'string', [ - 'notnull' => true, - 'length' => 64, - ]); - $table->addColumn('name', 'string', [ - 'notnull' => false, - 'length' => 64, - ]); - $table->addColumn('lower_name', 'string', [ - 'notnull' => false, - 'length' => 64, - ]); - $table->addColumn('created', 'datetime', [ - 'notnull' => false, - ]); - $table->addColumn('updated', 'datetime', [ - 'notnull' => false, - ]); - $table->setPrimaryKey(['id']); - $table->addUniqueIndex(['lower_name', 'user_id'], 'mg_lower_name_user_id_idx'); - } - - if (!$schema->hasTable('music_radio_stations')) { - $table = $schema->createTable('music_radio_stations'); - $table->addColumn('id', 'integer', [ - 'autoincrement' => true, - 'notnull' => true, - 'unsigned' => true, - ]); - $table->addColumn('user_id', 'string', [ - 'notnull' => true, - 'length' => 64, - ]); - $table->addColumn('name', 'string', [ - 'notnull' => false, - 'length' => 256, - ]); - $table->addColumn('stream_url', 'string', [ - 'notnull' => true, - 'length' => 2048, - ]); - $table->addColumn('home_url', 'string', [ - 'notnull' => false, - 'length' => 2048, - ]); - $table->addColumn('created', 'datetime', [ - 'notnull' => false, - ]); - $table->addColumn('updated', 'datetime', [ - 'notnull' => false, - ]); - $table->setPrimaryKey(['id']); - } - - if (!$schema->hasTable('music_ampache_sessions')) { - $table = $schema->createTable('music_ampache_sessions'); - $table->addColumn('id', 'integer', [ - 'autoincrement' => true, - 'notnull' => true, - 'unsigned' => true, - ]); - $table->addColumn('user_id', 'string', [ - 'notnull' => true, - 'length' => 64, - ]); - $table->addColumn('token', 'string', [ - 'notnull' => true, - 'length' => 64, - ]); - $table->addColumn('expiry', 'integer', [ - 'notnull' => true, - 'unsigned' => true, - ]); - $table->setPrimaryKey(['id']); - $table->addUniqueIndex(['token'], 'music_ampache_sessions_index'); - } - - if (!$schema->hasTable('music_ampache_users')) { - $table = $schema->createTable('music_ampache_users'); - $table->addColumn('id', 'integer', [ - 'autoincrement' => true, - 'notnull' => true, - 'length' => 4, - ]); - $table->addColumn('user_id', 'string', [ - 'notnull' => true, - 'length' => 64, - ]); - $table->addColumn('description', 'string', [ - 'notnull' => false, - 'length' => 64, - ]); - $table->addColumn('hash', 'string', [ - 'notnull' => true, - 'length' => 64, - ]); - $table->setPrimaryKey(['id']); - $table->addUniqueIndex(['hash', 'user_id'], 'music_ampache_users_index'); - } - - if (!$schema->hasTable('music_cache')) { - $table = $schema->createTable('music_cache'); - $table->addColumn('id', 'integer', [ - 'autoincrement' => true, - 'notnull' => true, - 'unsigned' => true, - ]); - $table->addColumn('key', 'string', [ - 'notnull' => true, - 'length' => 64, - ]); - $table->addColumn('user_id', 'string', [ - 'notnull' => true, - 'length' => 64, - ]); - $table->addColumn('data', 'text', [ - 'notnull' => false, - ]); - $table->setPrimaryKey(['id']); - $table->addUniqueIndex(['user_id', 'key'], 'music_cache_index'); - } - - if (!$schema->hasTable('music_bookmarks')) { - $table = $schema->createTable('music_bookmarks'); - $table->addColumn('id', 'integer', [ - 'autoincrement' => true, - 'notnull' => true, - 'unsigned' => true, - ]); - $table->addColumn('user_id', 'string', [ - 'notnull' => true, - 'length' => 64, - ]); - $table->addColumn('track_id', 'integer', [ - 'notnull' => true, - ]); - $table->addColumn('position', 'integer', [ - 'notnull' => true, - ]); - $table->addColumn('comment', 'string', [ - 'notnull' => false, - 'length' => 256, - ]); - $table->addColumn('created', 'datetime', [ - 'notnull' => false, - ]); - $table->addColumn('updated', 'datetime', [ - 'notnull' => false, - ]); - $table->setPrimaryKey(['id']); - $table->addUniqueIndex(['user_id', 'track_id'], 'music_bookmarks_user_track'); - } - return $schema; - } - - /** - * @param IOutput $output - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` - * @param array $options - */ - public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { - } -}