From d1b823073f67621d57c139d1a92f3b8ebba5d98b Mon Sep 17 00:00:00 2001 From: Constantin Simonis Date: Tue, 18 Mar 2025 16:19:53 +0100 Subject: [PATCH] add migration --- migrations/Version20250318151059.php | 55 ++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 migrations/Version20250318151059.php diff --git a/migrations/Version20250318151059.php b/migrations/Version20250318151059.php new file mode 100644 index 0000000..b678d6e --- /dev/null +++ b/migrations/Version20250318151059.php @@ -0,0 +1,55 @@ +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'); + } +}