From 8e6300079a406152ce470cf7c5cdb54fa99e63b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sat, 17 Sep 2022 18:43:08 +0200 Subject: [PATCH] Fixed some migration issues, when migrating from old Part-DB versions Fixes issue #170, #67 --- migrations/Version20190902140506.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/migrations/Version20190902140506.php b/migrations/Version20190902140506.php index 2238eb62..38f1e4c7 100644 --- a/migrations/Version20190902140506.php +++ b/migrations/Version20190902140506.php @@ -55,6 +55,24 @@ final class Version20190902140506 extends AbstractMultiPlatformMigration //Deactive SQL Modes (especially NO_ZERO_DATE, which prevents updating) $this->addSql("SET sql_mode = ''"); + /************************************************************************************************************** + * Normalize old database -> bring it in a format we can later use (manually generated SQL) + **************************************************************************************************************/ + + //It seems that the old Part-DB versions saved a zero at group_id when no group was selected. This causes later problems with the foreign key, so we have to fix this by setting it to null. + $this->addSql('UPDATE users SET users.group_id = NULL WHERE users.group_id = 0;'); + + //Old Part-DB versions did not deleted orderdetails when deleting an Part. This causes problems with the foreign key, so we have to fix this by deleting all orderdetails which are not connected to a part. + $this->addSql('DELETE FROM `orderdetails` WHERE (SELECT COUNT(parts.id) FROM parts WHERE parts.id = orderdetails.part_id) = 0;'); + //Same for pricedetails afterwards + $this->addSql('DELETE FROM `pricedetails` WHERE (SELECT COUNT(orderdetails.id) FROM orderdetails WHERE orderdetails.id = pricedetails.orderdetails_id) = 0;'); + //For attachments + $this->addSql('DELETE FROM `attachements` WHERE attachements.class_name = "Part" AND (SELECT COUNT(parts.id) FROM parts WHERE parts.id = attachements.element_id) = 0;'); + + /************************************************************************************************************** + * Doctrine generated SQL + **************************************************************************************************************/ + //Rename attachment tables (fix typos) $this->addSql('RENAME TABLE `attachement_types` TO `attachment_types`;'); $this->addSql('RENAME TABLE `attachements` TO `attachments`;');