|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace DoctrineMigrations; |
| 6 | + |
| 7 | +use App\Migration\AbstractMultiPlatformMigration; |
| 8 | +use Doctrine\DBAL\Schema\Schema; |
| 9 | + |
| 10 | +final class Version20250220215048 extends AbstractMultiPlatformMigration |
| 11 | +{ |
| 12 | + public function getDescription(): string |
| 13 | + { |
| 14 | + return 'Split $path property for attachments into $internal_path and $external_path'; |
| 15 | + } |
| 16 | + |
| 17 | + public function mySQLUp(Schema $schema): void |
| 18 | + { |
| 19 | + //Create the new columns as nullable (that is easier modifying them) |
| 20 | + $this->addSql('ALTER TABLE attachments ADD internal_path VARCHAR(255) DEFAULT NULL, ADD external_path VARCHAR(255) DEFAULT NULL'); |
| 21 | + |
| 22 | + //Copy the data from path to external_path and remove the path column |
| 23 | + $this->addSql('UPDATE attachments SET external_path=path'); |
| 24 | + $this->addSql('ALTER TABLE attachments DROP path'); |
| 25 | + |
| 26 | + |
| 27 | + $this->addSql('UPDATE attachments SET internal_path=external_path WHERE external_path LIKE \'#%MEDIA#%%\' ESCAPE \'#\''); |
| 28 | + $this->addSql('UPDATE attachments SET internal_path=external_path WHERE external_path LIKE \'#%BASE#%%\' ESCAPE \'#\''); |
| 29 | + $this->addSql('UPDATE attachments SET internal_path=external_path WHERE external_path LIKE \'#%SECURE#%%\' ESCAPE \'#\''); |
| 30 | + $this->addSql('UPDATE attachments SET internal_path=external_path WHERE external_path LIKE \'#%FOOTPRINTS#%%\' ESCAPE \'#\''); |
| 31 | + $this->addSql('UPDATE attachments SET internal_path=external_path WHERE external_path LIKE \'#%FOOTPRINTS3D#%%\' ESCAPE \'#\''); |
| 32 | + $this->addSql('UPDATE attachments SET external_path=NULL WHERE internal_path IS NOT NULL'); |
| 33 | + } |
| 34 | + |
| 35 | + public function mySQLDown(Schema $schema): void |
| 36 | + { |
| 37 | + $this->addSql('UPDATE attachments SET external_path=internal_path WHERE internal_path IS NOT NULL'); |
| 38 | + $this->addSql('ALTER TABLE attachments DROP internal_path'); |
| 39 | + $this->addSql('ALTER TABLE attachments RENAME COLUMN external_path TO path'); |
| 40 | + } |
| 41 | + |
| 42 | + public function postgreSQLUp(Schema $schema): void |
| 43 | + { |
| 44 | + //We can use the same SQL for PostgreSQL as for MySQL |
| 45 | + $this->mySQLUp($schema); |
| 46 | + } |
| 47 | + |
| 48 | + public function postgreSQLDown(Schema $schema): void |
| 49 | + { |
| 50 | + //We can use the same SQL for PostgreSQL as for MySQL |
| 51 | + $this->mySQLDown($schema); |
| 52 | + } |
| 53 | + |
| 54 | + public function sqLiteUp(Schema $schema): void |
| 55 | + { |
| 56 | + $this->addSql('CREATE TEMPORARY TABLE __temp__attachments AS SELECT id, type_id, original_filename, show_in_table, name, last_modified, datetime_added, class_name, element_id, path FROM attachments'); |
| 57 | + $this->addSql('DROP TABLE attachments'); |
| 58 | + $this->addSql('CREATE TABLE attachments (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, type_id INTEGER NOT NULL, original_filename VARCHAR(255) DEFAULT NULL, show_in_table BOOLEAN NOT NULL, name VARCHAR(255) NOT NULL, last_modified DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, datetime_added DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, class_name VARCHAR(255) NOT NULL, element_id INTEGER NOT NULL, internal_path VARCHAR(255) DEFAULT NULL, external_path VARCHAR(255) DEFAULT NULL, CONSTRAINT FK_47C4FAD6C54C8C93 FOREIGN KEY (type_id) REFERENCES attachment_types (id) ON UPDATE NO ACTION ON DELETE NO ACTION NOT DEFERRABLE INITIALLY IMMEDIATE)'); |
| 59 | + $this->addSql('INSERT INTO attachments (id, type_id, original_filename, show_in_table, name, last_modified, datetime_added, class_name, element_id, external_path) SELECT id, type_id, original_filename, show_in_table, name, last_modified, datetime_added, class_name, element_id, path FROM __temp__attachments'); |
| 60 | + $this->addSql('DROP TABLE __temp__attachments'); |
| 61 | + $this->addSql('CREATE INDEX attachment_element_idx ON attachments (class_name, element_id)'); |
| 62 | + $this->addSql('CREATE INDEX attachment_name_idx ON attachments (name)'); |
| 63 | + $this->addSql('CREATE INDEX attachments_idx_class_name_id ON attachments (class_name, id)'); |
| 64 | + $this->addSql('CREATE INDEX attachments_idx_id_element_id_class_name ON attachments (id, element_id, class_name)'); |
| 65 | + $this->addSql('CREATE INDEX IDX_47C4FAD6C54C8C93 ON attachments (type_id)'); |
| 66 | + $this->addSql('CREATE INDEX IDX_47C4FAD61F1F2A24 ON attachments (element_id)'); |
| 67 | + |
| 68 | + $this->addSql('UPDATE attachments SET internal_path=external_path WHERE external_path LIKE \'#%MEDIA#%%\' ESCAPE \'#\''); |
| 69 | + $this->addSql('UPDATE attachments SET internal_path=external_path WHERE external_path LIKE \'#%BASE#%%\' ESCAPE \'#\''); |
| 70 | + $this->addSql('UPDATE attachments SET internal_path=external_path WHERE external_path LIKE \'#%SECURE#%%\' ESCAPE \'#\''); |
| 71 | + $this->addSql('UPDATE attachments SET internal_path=external_path WHERE external_path LIKE \'#%FOOTPRINTS#%%\' ESCAPE \'#\''); |
| 72 | + $this->addSql('UPDATE attachments SET internal_path=external_path WHERE external_path LIKE \'#%FOOTPRINTS3D#%%\' ESCAPE \'#\''); |
| 73 | + $this->addSql('UPDATE attachments SET external_path=NULL WHERE internal_path IS NOT NULL'); |
| 74 | + } |
| 75 | + |
| 76 | + public function sqLiteDown(Schema $schema): void |
| 77 | + { |
| 78 | + //Reuse the MySQL down migration: |
| 79 | + $this->mySQLDown($schema); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | +} |
0 commit comments