diff --git a/database/migrations/2025_12_27_225224_convert_polymorphic_columns_to_type_refs.php b/database/migrations/2025_12_27_225224_convert_polymorphic_columns_to_type_refs.php index 91849e9f9..a677814fd 100755 --- a/database/migrations/2025_12_27_225224_convert_polymorphic_columns_to_type_refs.php +++ b/database/migrations/2025_12_27_225224_convert_polymorphic_columns_to_type_refs.php @@ -18,13 +18,25 @@ return new class extends Migration */ public function up() { + // Helper to drop index only if it exists (MySQL doesn't support DROP INDEX IF EXISTS) + $dropIndexIfExists = function($table, $index) { + $exists = DB::select( + "SELECT COUNT(*) as cnt FROM INFORMATION_SCHEMA.STATISTICS + WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND INDEX_NAME = ?", + [$table, $index] + )[0]->cnt > 0; + if ($exists) { + DB::statement("ALTER TABLE {$table} DROP INDEX {$index}"); + } + }; + // Convert polymorphic type columns from VARCHAR to BIGINT // These columns will now store type_ref IDs instead of class name strings // Existing data is cleared since there's no production data to migrate // _file_attachments: Convert fileable_type from VARCHAR to BIGINT // First drop the existing index that uses the VARCHAR column - DB::statement("ALTER TABLE _file_attachments DROP INDEX IF EXISTS idx_fileable"); + $dropIndexIfExists('_file_attachments', 'idx_fileable'); // Clear existing data and modify column type DB::statement("UPDATE _file_attachments SET fileable_type = NULL"); DB::statement("ALTER TABLE _file_attachments MODIFY fileable_type BIGINT NULL"); @@ -33,8 +45,8 @@ return new class extends Migration // _search_indexes: Convert indexable_type from VARCHAR to BIGINT // First drop the existing indexes that use the VARCHAR column - DB::statement("ALTER TABLE _search_indexes DROP INDEX IF EXISTS idx_indexable"); - DB::statement("ALTER TABLE _search_indexes DROP INDEX IF EXISTS unique_indexable"); + $dropIndexIfExists('_search_indexes', 'idx_indexable'); + $dropIndexIfExists('_search_indexes', 'unique_indexable'); // Clear existing data and modify column type DB::statement("TRUNCATE TABLE _search_indexes"); DB::statement("ALTER TABLE _search_indexes MODIFY indexable_type BIGINT NOT NULL");