55
migrations/Version20250318151059.php
Normal file
55
migrations/Version20250318151059.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace DoctrineMigrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
/**
|
||||
* Auto-generated Migration: Please modify to your needs!
|
||||
*/
|
||||
final class Version20250318151059 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
// First make sure customer_id is nullable
|
||||
$this->addSql('ALTER TABLE payment ALTER COLUMN customer_id DROP NOT NULL');
|
||||
|
||||
// Add payment_id to customer
|
||||
$this->addSql('ALTER TABLE customer ADD payment_id INT DEFAULT NULL');
|
||||
|
||||
// Migrate data
|
||||
$this->addSql('UPDATE customer c SET payment_id = (SELECT p.id FROM payment p WHERE p.customer_id = c.id)');
|
||||
|
||||
// Add constraints
|
||||
$this->addSql('ALTER TABLE customer ADD CONSTRAINT FK_81398E094C3A3BB FOREIGN KEY (payment_id) REFERENCES payment (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
|
||||
$this->addSql('CREATE UNIQUE INDEX UNIQ_81398E094C3A3BB ON customer (payment_id)');
|
||||
|
||||
// Remove old relationship
|
||||
$this->addSql('ALTER TABLE payment DROP CONSTRAINT fk_6d28840d9395c3f3');
|
||||
$this->addSql('DROP INDEX uniq_6d28840d9395c3f3');
|
||||
$this->addSql('ALTER TABLE payment DROP customer_id');
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
// this down() migration is auto-generated, please modify it to your needs
|
||||
$this->addSql('CREATE SCHEMA public');
|
||||
$this->addSql('ALTER TABLE payment ADD customer_id INT NOT NULL');
|
||||
$this->addSql('ALTER TABLE
|
||||
payment
|
||||
ADD
|
||||
CONSTRAINT fk_6d28840d9395c3f3 FOREIGN KEY (customer_id) REFERENCES customer (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
|
||||
$this->addSql('CREATE UNIQUE INDEX uniq_6d28840d9395c3f3 ON payment (customer_id)');
|
||||
$this->addSql('ALTER TABLE customer DROP CONSTRAINT FK_81398E094C3A3BB');
|
||||
$this->addSql('DROP INDEX UNIQ_81398E094C3A3BB');
|
||||
$this->addSql('ALTER TABLE customer DROP payment_id');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user