diff --git a/migrations/Version20230417211732.php b/migrations/Version20230417211732.php new file mode 100644 index 00000000..c60d7c0b --- /dev/null +++ b/migrations/Version20230417211732.php @@ -0,0 +1,51 @@ +addSql('DELETE FROM attachments WHERE class_name = "PartDB\\\\Part" AND NOT EXISTS (SELECT id FROM parts WHERE id = attachments.element_id)'); + $this->addSql('DELETE FROM attachments WHERE class_name = "PartDB\\\\Device" AND NOT EXISTS (SELECT id FROM projects WHERE id = attachments.element_id)'); + + // Replace all attachments where class_name is the legacy "PartDB\Part" with the new version "Part" + //We have to use 4 backslashes here, as PHP reduces them to 2 backslashes, which MySQL interprets as an escaped backslash. + $this->addSql('UPDATE attachments SET class_name = "Part" WHERE class_name = "PartDB\\\\Part"'); + //Do the same with PartDB\Device and Device + $this->addSql('UPDATE attachments SET class_name = "Device" WHERE class_name = "PartDB\\\\Device"'); + + + } + + public function mySQLDown(Schema $schema): void + { + // We can not revert this migration, because we don't know the old class name. + } + + public function sqLiteUp(Schema $schema): void + { + //As legacy database can only be migrated to MySQL, we don't need to implement this method. + $this->skipIf(true, 'Not needed for SQLite'); + } + + public function sqLiteDown(Schema $schema): void + { + //As we done nothing, we don't need to implement this method. + } +}