Fix migration to use PHP-level index existence checks

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
root
2025-12-27 23:16:26 +00:00
parent 4db772b132
commit 97fc6270f9

View File

@@ -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");