shouldAddColumn($schema, 'albums', 'external_id')) { $this->addSql('ALTER TABLE albums ADD external_id VARCHAR(64) DEFAULT NULL'); } if ($this->shouldAddColumn($schema, 'album_tracks', 'external_track_id')) { $this->addSql('ALTER TABLE album_tracks ADD external_track_id VARCHAR(64) DEFAULT NULL'); } if ($this->shouldAddColumn($schema, 'users', 'oauth_provider')) { $this->addSql('ALTER TABLE users ADD oauth_provider VARCHAR(50) DEFAULT NULL'); } if ($this->shouldAddColumn($schema, 'users', 'oauth_id')) { $this->addSql('ALTER TABLE users ADD oauth_id VARCHAR(190) DEFAULT NULL'); } if ($this->hasColumn($schema, 'albums', 'spotify_id') && $this->hasColumn($schema, 'albums', 'external_id')) { $this->addSql('UPDATE albums SET external_id = spotify_id WHERE external_id IS NULL AND spotify_id IS NOT NULL'); } if ($this->hasColumn($schema, 'albums', 'source')) { $this->addSql("UPDATE albums SET source = 'external' WHERE source = 'spotify'"); if (!$this->isSqlite()) { $this->addSql("ALTER TABLE albums ALTER source SET DEFAULT 'external'"); } } if ($this->hasColumn($schema, 'album_tracks', 'spotify_track_id') && $this->hasColumn($schema, 'album_tracks', 'external_track_id')) { $this->addSql('UPDATE album_tracks SET external_track_id = spotify_track_id WHERE external_track_id IS NULL AND spotify_track_id IS NOT NULL'); } if (!$this->indexExists($schema, 'albums', 'uniq_album_external_id')) { $this->addSql('CREATE UNIQUE INDEX uniq_album_external_id ON albums (external_id)'); } if (!$this->indexExists($schema, 'users', 'uniq_users_oauth_identity')) { $this->addSql('CREATE UNIQUE INDEX uniq_users_oauth_identity ON users (oauth_provider, oauth_id)'); } } public function down(Schema $schema): void { if ($this->isSqlite()) { // SQLite column drops require table rebuilds; keep schema forward-only here. return; } if ($this->indexExists($schema, 'users', 'uniq_users_oauth_identity')) { $this->addSql('DROP INDEX uniq_users_oauth_identity'); } if ($this->indexExists($schema, 'albums', 'uniq_album_external_id')) { $this->addSql('DROP INDEX uniq_album_external_id'); } if ($this->hasColumn($schema, 'users', 'oauth_id')) { $this->addSql('ALTER TABLE users DROP COLUMN oauth_id'); } if ($this->hasColumn($schema, 'users', 'oauth_provider')) { $this->addSql('ALTER TABLE users DROP COLUMN oauth_provider'); } if ($this->hasColumn($schema, 'album_tracks', 'external_track_id')) { $this->addSql('ALTER TABLE album_tracks DROP COLUMN external_track_id'); } if ($this->hasColumn($schema, 'albums', 'external_id')) { $this->addSql('ALTER TABLE albums DROP COLUMN external_id'); } if ($this->hasColumn($schema, 'albums', 'source')) { $this->addSql("UPDATE albums SET source = 'spotify' WHERE source = 'external'"); $this->addSql("ALTER TABLE albums ALTER source SET DEFAULT 'spotify'"); } } private function shouldAddColumn(Schema $schema, string $tableName, string $columnName): bool { return $this->hasTable($schema, $tableName) && !$this->hasColumn($schema, $tableName, $columnName); } 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'; } }