|
| 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 | + * Give calendarsubscriptions.calendarorder a default value. |
| 12 | + */ |
| 13 | +final class Version20260908210000 extends AbstractMigration |
| 14 | +{ |
| 15 | + public function getDescription(): string |
| 16 | + { |
| 17 | + return 'Default calendarsubscriptions.calendarorder to 0, as sabre/dav omits the column when a client subscribes without a calendar-order'; |
| 18 | + } |
| 19 | + |
| 20 | + public function up(Schema $schema): void |
| 21 | + { |
| 22 | + $engine = $this->connection->getDatabasePlatform()->getName(); |
| 23 | + |
| 24 | + // \Sabre\CalDAV\Backend\PDO::createSubscription() only lists `calendarorder` in its |
| 25 | + // INSERT when the client sent {http://apple.com/ns/ical/}calendar-order. Without a |
| 26 | + // default, subscribing to a feed then fails with a NOT NULL violation (HTTP 500). |
| 27 | + if ('mysql' === $engine) { |
| 28 | + $this->addSql('ALTER TABLE calendarsubscriptions CHANGE calendarorder calendarorder INT DEFAULT 0 NOT NULL'); |
| 29 | + } elseif ('postgresql' === $engine) { |
| 30 | + $this->addSql('ALTER TABLE calendarsubscriptions ALTER COLUMN calendarorder SET DEFAULT 0'); |
| 31 | + } elseif ('sqlite' === $engine) { |
| 32 | + // SQLite cannot alter a column in place: add the replacement, copy, swap, drop. |
| 33 | + $this->addSql('ALTER TABLE calendarsubscriptions ADD COLUMN new_calendarorder INTEGER DEFAULT 0 NOT NULL'); |
| 34 | + $this->addSql('UPDATE calendarsubscriptions SET new_calendarorder = calendarorder'); |
| 35 | + $this->addSql('ALTER TABLE calendarsubscriptions RENAME COLUMN calendarorder TO old_calendarorder'); |
| 36 | + $this->addSql('ALTER TABLE calendarsubscriptions RENAME COLUMN new_calendarorder TO calendarorder'); |
| 37 | + $this->addSql('ALTER TABLE calendarsubscriptions DROP COLUMN old_calendarorder'); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public function down(Schema $schema): void |
| 42 | + { |
| 43 | + $engine = $this->connection->getDatabasePlatform()->getName(); |
| 44 | + |
| 45 | + if ('mysql' === $engine) { |
| 46 | + $this->addSql('ALTER TABLE calendarsubscriptions CHANGE calendarorder calendarorder INT NOT NULL'); |
| 47 | + } elseif ('postgresql' === $engine) { |
| 48 | + $this->addSql('ALTER TABLE calendarsubscriptions ALTER COLUMN calendarorder DROP DEFAULT'); |
| 49 | + } elseif ('sqlite' === $engine) { |
| 50 | + // SQLite refuses to ADD a NOT NULL column without a default, so the only way back |
| 51 | + // is to rebuild the table with its original definition. |
| 52 | + $this->addSql('CREATE TABLE calendarsubscriptions_old (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, uri VARCHAR(255) NOT NULL, principaluri VARCHAR(255) NOT NULL, source CLOB DEFAULT NULL, displayname VARCHAR(255) DEFAULT NULL, refreshrate VARCHAR(10) DEFAULT NULL, calendarorder INTEGER NOT NULL, calendarcolor VARCHAR(10) DEFAULT NULL, striptodos SMALLINT DEFAULT NULL, stripalarms SMALLINT DEFAULT NULL, stripattachments SMALLINT DEFAULT NULL, lastmodified INTEGER DEFAULT NULL)'); |
| 53 | + $this->addSql('INSERT INTO calendarsubscriptions_old (id, uri, principaluri, source, displayname, refreshrate, calendarorder, calendarcolor, striptodos, stripalarms, stripattachments, lastmodified) SELECT id, uri, principaluri, source, displayname, refreshrate, calendarorder, calendarcolor, striptodos, stripalarms, stripattachments, lastmodified FROM calendarsubscriptions'); |
| 54 | + $this->addSql('DROP TABLE calendarsubscriptions'); |
| 55 | + $this->addSql('ALTER TABLE calendarsubscriptions_old RENAME TO calendarsubscriptions'); |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments