|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace DoctrineMigrations; |
| 6 | + |
| 7 | +use Doctrine\DBAL\Schema\Schema; |
| 8 | +use Doctrine\Migrations\AbstractMigration; |
| 9 | + |
| 10 | +/** |
| 11 | + * Allow addressbooks.displayname to be null. |
| 12 | + */ |
| 13 | +final class Version20260908220000 extends AbstractMigration |
| 14 | +{ |
| 15 | + public function getDescription(): string |
| 16 | + { |
| 17 | + return 'Allow addressbooks.displayname to be null, as a display name is optional when a client creates an address book'; |
| 18 | + } |
| 19 | + |
| 20 | + public function up(Schema $schema): void |
| 21 | + { |
| 22 | + $engine = $this->connection->getDatabasePlatform()->getName(); |
| 23 | + |
| 24 | + // A display name is optional in CardDAV: \Sabre\CardDAV\Backend\PDO::createAddressBook() |
| 25 | + // binds NULL when the client's MKCOL carries no {DAV:}displayname, and updateAddressBook() |
| 26 | + // does the same when a PROPPATCH removes it. With a NOT NULL column both fail (HTTP 500). |
| 27 | + if ('mysql' === $engine) { |
| 28 | + $this->addSql('ALTER TABLE addressbooks CHANGE displayname displayname VARCHAR(255) DEFAULT NULL'); |
| 29 | + } elseif ('postgresql' === $engine) { |
| 30 | + $this->addSql('ALTER TABLE addressbooks ALTER COLUMN displayname DROP NOT NULL'); |
| 31 | + } elseif ('sqlite' === $engine) { |
| 32 | + // SQLite cannot alter a column in place: add the replacement, copy, swap, drop. |
| 33 | + $this->addSql('ALTER TABLE addressbooks ADD COLUMN new_displayname VARCHAR(255) DEFAULT NULL'); |
| 34 | + $this->addSql('UPDATE addressbooks SET new_displayname = displayname'); |
| 35 | + $this->addSql('ALTER TABLE addressbooks RENAME COLUMN displayname TO old_displayname'); |
| 36 | + $this->addSql('ALTER TABLE addressbooks RENAME COLUMN new_displayname TO displayname'); |
| 37 | + $this->addSql('ALTER TABLE addressbooks DROP COLUMN old_displayname'); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public function down(Schema $schema): void |
| 42 | + { |
| 43 | + $engine = $this->connection->getDatabasePlatform()->getName(); |
| 44 | + |
| 45 | + // Address books created without a display name would violate the restored NOT NULL, |
| 46 | + // so fall back to their uri rather than losing the row. |
| 47 | + $this->addSql('UPDATE addressbooks SET displayname = uri WHERE displayname IS NULL'); |
| 48 | + |
| 49 | + if ('mysql' === $engine) { |
| 50 | + $this->addSql('ALTER TABLE addressbooks CHANGE displayname displayname VARCHAR(255) NOT NULL'); |
| 51 | + } elseif ('postgresql' === $engine) { |
| 52 | + $this->addSql('ALTER TABLE addressbooks ALTER COLUMN displayname SET NOT NULL'); |
| 53 | + } elseif ('sqlite' === $engine) { |
| 54 | + // SQLite refuses to ADD a NOT NULL column without a default, so rebuild the table. |
| 55 | + $this->addSql('CREATE TABLE addressbooks_old (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, principaluri VARCHAR(255) NOT NULL, displayname VARCHAR(255) NOT NULL, uri VARCHAR(255) NOT NULL, description CLOB DEFAULT NULL, synctoken VARCHAR(255) NOT NULL, included_in_birthday_calendar INTEGER DEFAULT 0)'); |
| 56 | + $this->addSql('INSERT INTO addressbooks_old (id, principaluri, displayname, uri, description, synctoken, included_in_birthday_calendar) SELECT id, principaluri, displayname, uri, description, synctoken, included_in_birthday_calendar FROM addressbooks'); |
| 57 | + $this->addSql('DROP TABLE addressbooks'); |
| 58 | + $this->addSql('ALTER TABLE addressbooks_old RENAME TO addressbooks'); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments