48 lines
1.5 KiB
PHP
48 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace DoctrineMigrations;
|
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
|
use Doctrine\Migrations\AbstractMigration;
|
|
|
|
final class Version20260401101500 extends AbstractMigration
|
|
{
|
|
public function getDescription(): string
|
|
{
|
|
return 'Add nullable album owner relation (albums.owner_id -> users.id)';
|
|
}
|
|
|
|
public function up(Schema $schema): void
|
|
{
|
|
if ($this->isSqlite()) {
|
|
$this->addSql('ALTER TABLE albums ADD COLUMN owner_id INTEGER DEFAULT NULL');
|
|
$this->addSql('CREATE INDEX IF NOT EXISTS IDX_ALBUM_OWNER ON albums (owner_id)');
|
|
return;
|
|
}
|
|
|
|
$this->addSql('ALTER TABLE albums ADD owner_id INT DEFAULT NULL');
|
|
$this->addSql('CREATE INDEX IDX_ALBUM_OWNER ON albums (owner_id)');
|
|
$this->addSql('ALTER TABLE albums ADD CONSTRAINT FK_ALBUM_OWNER FOREIGN KEY (owner_id) REFERENCES users (id) ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE');
|
|
}
|
|
|
|
public function down(Schema $schema): void
|
|
{
|
|
if ($this->isSqlite()) {
|
|
// SQLite down-migration omitted to avoid lossy table rebuild.
|
|
return;
|
|
}
|
|
|
|
$this->addSql('ALTER TABLE albums DROP CONSTRAINT FK_ALBUM_OWNER');
|
|
$this->addSql('DROP INDEX IDX_ALBUM_OWNER');
|
|
$this->addSql('ALTER TABLE albums DROP COLUMN owner_id');
|
|
}
|
|
|
|
private function isSqlite(): bool
|
|
{
|
|
return $this->connection->getDatabasePlatform()->getName() === 'sqlite';
|
|
}
|
|
}
|
|
|