From f3da345bf34036f80fb30eee74ed4b3d948bf0a5 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 15 Feb 2025 11:17:14 +0100 Subject: [PATCH] Fix unique_short_code_plus_domain index in Microsoft SQL --- .../Core/migrations/Version20250215100756.php | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 module/Core/migrations/Version20250215100756.php diff --git a/module/Core/migrations/Version20250215100756.php b/module/Core/migrations/Version20250215100756.php new file mode 100644 index 00000000..77e9599e --- /dev/null +++ b/module/Core/migrations/Version20250215100756.php @@ -0,0 +1,43 @@ +skipIf(! $this->isMicrosoftSql()); + + // Drop the existing unique index + $shortUrls = $schema->getTable('short_urls'); + $shortUrls->dropIndex('unique_short_code_plus_domain'); + } + + public function postUp(Schema $schema): void + { + // The only way to get the index properly generated is by hardcoding the SQL. + // Since this migration is run Microsoft SQL only, it is safe to use this approach. + $this->connection->executeStatement( + 'CREATE UNIQUE INDEX unique_short_code_plus_domain ON short_urls (short_code, domain_id);', + ); + } + + private function isMicrosoftSql(): bool + { + return $this->connection->getDatabasePlatform() instanceof SQLServerPlatform; + } +}