123 lines
5.0 KiB
PHP
123 lines
5.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace DoctrineMigrations;
|
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
|
use Doctrine\Migrations\AbstractMigration;
|
|
|
|
final class Version20260227174500 extends AbstractMigration
|
|
{
|
|
public function getDescription(): string
|
|
{
|
|
return 'Drop legacy Spotify and unused album columns (created_by_id, cover_url, local_id, cover_image_path)';
|
|
}
|
|
|
|
public function up(Schema $schema): void
|
|
{
|
|
if ($this->isSqlite()) {
|
|
// SQLite column drops/constraint rewrites are intentionally skipped in this migration.
|
|
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
|
|
{
|
|
if ($this->isSqlite()) {
|
|
return;
|
|
}
|
|
|
|
if ($this->hasTable($schema, 'albums')) {
|
|
if (!$this->hasColumn($schema, 'albums', 'spotify_id')) {
|
|
$this->addSql('ALTER TABLE albums ADD spotify_id VARCHAR(64) DEFAULT NULL');
|
|
}
|
|
if (!$this->hasColumn($schema, 'albums', 'local_id')) {
|
|
$this->addSql('ALTER TABLE albums ADD local_id VARCHAR(64) DEFAULT NULL');
|
|
}
|
|
if (!$this->hasColumn($schema, 'albums', 'created_by_id')) {
|
|
$this->addSql('ALTER TABLE albums ADD created_by_id INT DEFAULT NULL');
|
|
}
|
|
if (!$this->hasColumn($schema, 'albums', 'cover_url')) {
|
|
$this->addSql('ALTER TABLE albums ADD cover_url VARCHAR(1024) DEFAULT NULL');
|
|
}
|
|
if (!$this->hasColumn($schema, 'albums', 'cover_image_path')) {
|
|
$this->addSql('ALTER TABLE albums ADD cover_image_path VARCHAR(255) DEFAULT NULL');
|
|
}
|
|
|
|
if (!$this->indexExists($schema, 'albums', 'UNIQ_F4E2474FA905FC5C')) {
|
|
$this->addSql('CREATE UNIQUE INDEX UNIQ_F4E2474FA905FC5C ON albums (spotify_id)');
|
|
}
|
|
if (!$this->indexExists($schema, 'albums', 'UNIQ_F4E2474F5D5A2101')) {
|
|
$this->addSql('CREATE UNIQUE INDEX UNIQ_F4E2474F5D5A2101 ON albums (local_id)');
|
|
}
|
|
if (!$this->indexExists($schema, 'albums', 'IDX_F4E2474FB03A8386')) {
|
|
$this->addSql('CREATE INDEX IDX_F4E2474FB03A8386 ON albums (created_by_id)');
|
|
}
|
|
|
|
$this->addSql('ALTER TABLE albums ADD CONSTRAINT FK_F4E2474FB03A8386 FOREIGN KEY (created_by_id) REFERENCES users (id) ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE');
|
|
}
|
|
|
|
if (!$this->hasColumn($schema, 'album_tracks', 'spotify_track_id')) {
|
|
$this->addSql('ALTER TABLE album_tracks ADD spotify_track_id VARCHAR(64) DEFAULT NULL');
|
|
}
|
|
|
|
if (!$this->hasColumn($schema, 'reviews', 'spotify_album_id')) {
|
|
$this->addSql('ALTER TABLE reviews ADD spotify_album_id VARCHAR(64) DEFAULT NULL');
|
|
}
|
|
}
|
|
|
|
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 indexExists(Schema $schema, string $tableName, string $indexName): bool
|
|
{
|
|
return $this->hasTable($schema, $tableName) && $schema->getTable($tableName)->hasIndex($indexName);
|
|
}
|
|
|
|
private function isSqlite(): bool
|
|
{
|
|
return $this->connection->getDatabasePlatform()->getName() === 'sqlite';
|
|
}
|
|
}
|
|
|