|
| 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 | + * Store DAV sync tokens as integers. |
| 12 | + * |
| 13 | + * Adapted from the work of @AnnoyingTechnology in tchapi/davis#277. |
| 14 | + */ |
| 15 | +final class Version20260909100000 extends AbstractMigration |
| 16 | +{ |
| 17 | + public function getDescription(): string |
| 18 | + { |
| 19 | + return 'Store DAV sync tokens as integers so that sync-collection range queries compare them numerically'; |
| 20 | + } |
| 21 | + |
| 22 | + public function up(Schema $schema): void |
| 23 | + { |
| 24 | + $engine = $this->connection->getDatabasePlatform()->getName(); |
| 25 | + |
| 26 | + // sabre/dav compares and orders sync tokens numerically: |
| 27 | + // WHERE synctoken >= ? AND synctoken < ? ... ORDER BY synctoken |
| 28 | + // Stored as text, '10' sorts before '9', so a client syncing across a decimal-width |
| 29 | + // boundary is told the collection advanced but is handed none of the changes. |
| 30 | + if ('mysql' === $engine) { |
| 31 | + $this->addSql('ALTER TABLE addressbooks CHANGE synctoken synctoken INT DEFAULT 1 NOT NULL'); |
| 32 | + $this->addSql('ALTER TABLE calendars CHANGE synctoken synctoken INT DEFAULT 1 NOT NULL'); |
| 33 | + $this->addSql('ALTER TABLE addressbookchanges CHANGE synctoken synctoken INT DEFAULT 1 NOT NULL'); |
| 34 | + } elseif ('postgresql' === $engine) { |
| 35 | + // addressbooks and calendars were already converted by Version20230209142217; |
| 36 | + // only addressbookchanges is still text here. |
| 37 | + $this->addSql('ALTER TABLE addressbookchanges ALTER COLUMN synctoken TYPE INT USING synctoken::integer'); |
| 38 | + foreach (['addressbooks', 'calendars', 'addressbookchanges'] as $table) { |
| 39 | + $this->addSql(sprintf('ALTER TABLE %s ALTER COLUMN synctoken SET DEFAULT 1', $table)); |
| 40 | + } |
| 41 | + } elseif ('sqlite' === $engine) { |
| 42 | + // A VARCHAR column has TEXT affinity in SQLite, so the comparison is textual there |
| 43 | + // too. SQLite cannot alter a column in place: add the replacement, copy, swap, drop. |
| 44 | + foreach (['addressbooks', 'calendars', 'addressbookchanges'] as $table) { |
| 45 | + $this->replaceSyncTokenColumn($table, 'INTEGER', 'INTEGER', '1'); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public function down(Schema $schema): void |
| 51 | + { |
| 52 | + $engine = $this->connection->getDatabasePlatform()->getName(); |
| 53 | + |
| 54 | + if ('mysql' === $engine) { |
| 55 | + $this->addSql('ALTER TABLE addressbooks CHANGE synctoken synctoken VARCHAR(255) NOT NULL'); |
| 56 | + $this->addSql('ALTER TABLE calendars CHANGE synctoken synctoken VARCHAR(255) NOT NULL'); |
| 57 | + $this->addSql('ALTER TABLE addressbookchanges CHANGE synctoken synctoken VARCHAR(255) NOT NULL'); |
| 58 | + } elseif ('postgresql' === $engine) { |
| 59 | + // Only addressbookchanges goes back to text: addressbooks and calendars were |
| 60 | + // already integers before this migration, and reverting them would reintroduce |
| 61 | + // the error Version20230209142217 fixed (synctoken + 1 on a text column). |
| 62 | + foreach (['addressbooks', 'calendars', 'addressbookchanges'] as $table) { |
| 63 | + $this->addSql(sprintf('ALTER TABLE %s ALTER COLUMN synctoken DROP DEFAULT', $table)); |
| 64 | + } |
| 65 | + $this->addSql('ALTER TABLE addressbookchanges ALTER COLUMN synctoken TYPE VARCHAR(255) USING synctoken::varchar'); |
| 66 | + } elseif ('sqlite' === $engine) { |
| 67 | + // NB: SQLite refuses to ADD a NOT NULL column without a default, so the restored |
| 68 | + // columns keep a harmless DEFAULT '1' that the original schema did not have. |
| 69 | + foreach (['addressbooks', 'calendars', 'addressbookchanges'] as $table) { |
| 70 | + $this->replaceSyncTokenColumn($table, 'VARCHAR(255)', 'TEXT', "'1'"); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private function replaceSyncTokenColumn(string $table, string $type, string $cast, string $default): void |
| 76 | + { |
| 77 | + $this->addSql(sprintf('ALTER TABLE %s ADD COLUMN new_synctoken %s DEFAULT %s NOT NULL', $table, $type, $default)); |
| 78 | + $this->addSql(sprintf('UPDATE %s SET new_synctoken = CAST(synctoken AS %s)', $table, $cast)); |
| 79 | + $this->addSql(sprintf('ALTER TABLE %s RENAME COLUMN synctoken TO old_synctoken', $table)); |
| 80 | + $this->addSql(sprintf('ALTER TABLE %s RENAME COLUMN new_synctoken TO synctoken', $table)); |
| 81 | + $this->addSql(sprintf('ALTER TABLE %s DROP COLUMN old_synctoken', $table)); |
| 82 | + } |
| 83 | +} |
0 commit comments