166 lines
6.3 KiB
PHP
166 lines
6.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace DoctrineMigrations;
|
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
|
use Doctrine\Migrations\AbstractMigration;
|
|
|
|
final class Version20260227181000 extends AbstractMigration
|
|
{
|
|
public function getDescription(): string
|
|
{
|
|
return 'Apply SQLite-safe table rebuilds to remove legacy Spotify and unused album columns';
|
|
}
|
|
|
|
public function up(Schema $schema): void
|
|
{
|
|
if ($this->isSqlite()) {
|
|
$this->migrateSqlite();
|
|
return;
|
|
}
|
|
|
|
if ($this->hasTable($schema, 'albums')) {
|
|
$this->addSql('ALTER TABLE albums DROP CONSTRAINT IF EXISTS FK_F4E2474FB03A8386');
|
|
$this->addSql('DROP INDEX IF EXISTS IDX_F4E2474FB03A8386');
|
|
$this->addSql('DROP INDEX IF EXISTS uniq_album_local_id');
|
|
$this->addSql('DROP INDEX IF EXISTS UNIQ_F4E2474F5D5A2101');
|
|
$this->addSql('DROP INDEX IF EXISTS UNIQ_F4E2474FA905FC5C');
|
|
|
|
if ($this->hasColumn($schema, 'albums', 'created_by_id')) {
|
|
$this->addSql('ALTER TABLE albums DROP COLUMN created_by_id');
|
|
}
|
|
if ($this->hasColumn($schema, 'albums', 'local_id')) {
|
|
$this->addSql('ALTER TABLE albums DROP COLUMN local_id');
|
|
}
|
|
if ($this->hasColumn($schema, 'albums', 'cover_url')) {
|
|
$this->addSql('ALTER TABLE albums DROP COLUMN cover_url');
|
|
}
|
|
if ($this->hasColumn($schema, 'albums', 'cover_image_path')) {
|
|
$this->addSql('ALTER TABLE albums DROP COLUMN cover_image_path');
|
|
}
|
|
if ($this->hasColumn($schema, 'albums', 'spotify_id')) {
|
|
$this->addSql('ALTER TABLE albums DROP COLUMN spotify_id');
|
|
}
|
|
}
|
|
|
|
if ($this->hasColumn($schema, 'album_tracks', 'spotify_track_id')) {
|
|
$this->addSql('ALTER TABLE album_tracks DROP COLUMN spotify_track_id');
|
|
}
|
|
|
|
if ($this->hasColumn($schema, 'reviews', 'spotify_album_id')) {
|
|
$this->addSql('ALTER TABLE reviews DROP COLUMN spotify_album_id');
|
|
}
|
|
}
|
|
|
|
public function down(Schema $schema): void
|
|
{
|
|
// Forward-only migration: reverting would require data-loss-prone table rewrites.
|
|
}
|
|
|
|
private function migrateSqlite(): void
|
|
{
|
|
$this->addSql('PRAGMA foreign_keys = OFF');
|
|
|
|
$this->addSql(
|
|
"CREATE TABLE albums_new (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
|
external_id VARCHAR(64) DEFAULT NULL,
|
|
source VARCHAR(16) NOT NULL,
|
|
name VARCHAR(255) NOT NULL,
|
|
artists CLOB NOT NULL,
|
|
release_date VARCHAR(20) DEFAULT NULL,
|
|
total_tracks INTEGER NOT NULL,
|
|
external_url VARCHAR(1024) DEFAULT NULL,
|
|
created_at DATETIME NOT NULL,
|
|
updated_at DATETIME NOT NULL,
|
|
genres CLOB NOT NULL DEFAULT '[]'
|
|
)"
|
|
);
|
|
$this->addSql(
|
|
"INSERT INTO albums_new (
|
|
id, external_id, source, name, artists, release_date, total_tracks, external_url, created_at, updated_at, genres
|
|
)
|
|
SELECT
|
|
id,
|
|
CASE
|
|
WHEN external_id IS NOT NULL AND TRIM(external_id) <> '' THEN external_id
|
|
WHEN local_id IS NOT NULL AND TRIM(local_id) <> '' THEN local_id
|
|
ELSE 'legacy:' || id
|
|
END AS external_id,
|
|
CASE
|
|
WHEN source = 'spotify' OR source IS NULL OR TRIM(source) = '' THEN 'external'
|
|
ELSE source
|
|
END AS source,
|
|
name,
|
|
artists,
|
|
release_date,
|
|
total_tracks,
|
|
external_url,
|
|
created_at,
|
|
updated_at,
|
|
COALESCE(genres, '[]') AS genres
|
|
FROM albums"
|
|
);
|
|
$this->addSql('DROP TABLE albums');
|
|
$this->addSql('ALTER TABLE albums_new RENAME TO albums');
|
|
$this->addSql('CREATE UNIQUE INDEX IF NOT EXISTS uniq_album_external_id ON albums (external_id)');
|
|
|
|
$this->addSql(
|
|
"CREATE TABLE album_tracks_new (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
|
album_id INTEGER NOT NULL,
|
|
external_track_id VARCHAR(64) DEFAULT NULL,
|
|
disc_number INTEGER NOT NULL,
|
|
track_number INTEGER NOT NULL,
|
|
name VARCHAR(512) NOT NULL,
|
|
duration_ms INTEGER NOT NULL,
|
|
preview_url VARCHAR(1024) DEFAULT NULL,
|
|
CONSTRAINT FK_5E4A3B3B1137ABCF FOREIGN KEY (album_id) REFERENCES albums (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE
|
|
)"
|
|
);
|
|
$this->addSql(
|
|
"INSERT INTO album_tracks_new (
|
|
id, album_id, external_track_id, disc_number, track_number, name, duration_ms, preview_url
|
|
)
|
|
SELECT
|
|
id,
|
|
album_id,
|
|
CASE
|
|
WHEN external_track_id IS NOT NULL AND TRIM(external_track_id) <> '' THEN external_track_id
|
|
WHEN spotify_track_id IS NOT NULL AND TRIM(spotify_track_id) <> '' THEN spotify_track_id
|
|
ELSE NULL
|
|
END AS external_track_id,
|
|
disc_number,
|
|
track_number,
|
|
name,
|
|
duration_ms,
|
|
preview_url
|
|
FROM album_tracks"
|
|
);
|
|
$this->addSql('DROP TABLE album_tracks');
|
|
$this->addSql('ALTER TABLE album_tracks_new RENAME TO album_tracks');
|
|
$this->addSql('CREATE INDEX IF NOT EXISTS IDX_5E4A3B3B1137ABCF ON album_tracks (album_id)');
|
|
$this->addSql('CREATE UNIQUE INDEX IF NOT EXISTS uniq_album_disc_track ON album_tracks (album_id, disc_number, track_number)');
|
|
|
|
$this->addSql('PRAGMA foreign_keys = ON');
|
|
}
|
|
|
|
private function hasTable(Schema $schema, string $tableName): bool
|
|
{
|
|
return $schema->hasTable($tableName);
|
|
}
|
|
|
|
private function hasColumn(Schema $schema, string $tableName, string $columnName): bool
|
|
{
|
|
return $this->hasTable($schema, $tableName) && $schema->getTable($tableName)->hasColumn($columnName);
|
|
}
|
|
|
|
private function isSqlite(): bool
|
|
{
|
|
return $this->connection->getDatabasePlatform()->getName() === 'sqlite';
|
|
}
|
|
}
|
|
|