@@ -56,7 +56,18 @@ public function up(): void
5656 foreach ($ indexes as [$ columns , $ name ]) {
5757 // drop any auto-generated long form if present
5858 $ longName = $ tableName . '_ ' . implode ('_ ' , $ columns ) . '_index ' ;
59- DB ::statement ("DROP INDEX IF EXISTS ` $ longName` ON ` $ tableName` " );
59+ // older MySQL versions do not support "IF EXISTS" on DROP
60+ // INDEX, so check information_schema first and only run the
61+ // ALTER when the index is present.
62+ $ dbName = DB ::getDatabaseName ();
63+ $ idxExists = DB ::selectOne (
64+ 'SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS
65+ WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND INDEX_NAME = ? ' ,
66+ [$ dbName , $ tableName , $ longName ]
67+ );
68+ if ($ idxExists ) {
69+ DB ::statement ("ALTER TABLE ` $ tableName` DROP INDEX ` $ longName` " );
70+ }
6071
6172 // create the index with the explicit name; if it already
6273 // exists this will quietly do nothing
@@ -90,14 +101,20 @@ public function down(): void
90101 'virtual_events_team_status_idx ' ,
91102 'virtual_events_start_end_idx ' ,
92103 ] as $ idx ) {
93- // can't easily know which table the index belongs to, so run a
94- // blind ALTER. If it fails because the index doesn't exist we'll
95- // ignore it (the statement is wrapped in try/catch).
96- // We prefer DB::statement so we can use IF EXISTS safely.
97- // Extract table name from index naming convention.
104+ // determine table name from index naming convention
98105 $ parts = explode ('_ ' , $ idx );
99106 $ tableName = $ parts [0 ];
100- DB ::statement ("DROP INDEX IF EXISTS ` $ idx` ON ` $ tableName` " );
107+
108+ // check existence via information_schema before trying to drop
109+ $ dbName = DB ::getDatabaseName ();
110+ $ idxExists = DB ::selectOne (
111+ 'SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS
112+ WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND INDEX_NAME = ? ' ,
113+ [$ dbName , $ tableName , $ idx ]
114+ );
115+ if ($ idxExists ) {
116+ DB ::statement ("ALTER TABLE ` $ tableName` DROP INDEX ` $ idx` " );
117+ }
101118 }
102119 }
103120};
0 commit comments