|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Database\Migrations\Migration; |
| 4 | +use Illuminate\Database\Schema\Blueprint; |
| 5 | +use Illuminate\Support\Facades\Schema; |
| 6 | + |
| 7 | +return new class extends Migration |
| 8 | +{ |
| 9 | + /** |
| 10 | + * Run the migrations. |
| 11 | + */ |
| 12 | + public function up(): void |
| 13 | + { |
| 14 | + Schema::table('players', function (Blueprint $table) { |
| 15 | + // Optimize findMatch() - called via /api/v2/qm/{ladder}/{playerName} |
| 16 | + $table->index(['ladder_id', 'username'], 'idx_players_ladder_username'); |
| 17 | + // Optimize lookups by user_id (used in multiple services) |
| 18 | + $table->index('user_id', 'idx_players_user_id'); |
| 19 | + }); |
| 20 | + |
| 21 | + Schema::table('users', function (Blueprint $table) { |
| 22 | + // Optimize checkUserForBans(), used every time findMatch() is called |
| 23 | + $table->index('ip_address_id', 'idx_users_ip_address_id'); |
| 24 | + $table->index('primary_user_id', 'idx_users_primary_user_id'); |
| 25 | + }); |
| 26 | + |
| 27 | + Schema::table('game_reports', function (Blueprint $table) { |
| 28 | + // Optimize StatsService calls: getWinnerOfTheDay, getPlayerOfTheDay, getFactionsPlayedByPlayer |
| 29 | + $table->index(['valid', 'best_report', 'created_at'], 'idx_game_reports_valid_best_created'); |
| 30 | + }); |
| 31 | + |
| 32 | + Schema::table('qm_match_players', function (Blueprint $table) { |
| 33 | + // Optimize queries checking if a player is waiting in queue |
| 34 | + $table->index(['player_id', 'waiting'], 'idx_qm_match_players_player_waiting'); |
| 35 | + }); |
| 36 | + |
| 37 | + Schema::table('ip_addresses', function (Blueprint $table) { |
| 38 | + // Optimize lookups by IP address (used for bans / login checks) |
| 39 | + $table->index('address', 'idx_ip_addresses_address'); |
| 40 | + }); |
| 41 | + |
| 42 | + Schema::table('player_active_handles', function (Blueprint $table) { |
| 43 | + // Optimize getPlayerActiveHandle(), used in username-based API calls |
| 44 | + $table->index(['player_id', 'ladder_id', 'created_at'], 'idx_pah_player_ladder_created'); |
| 45 | + }); |
| 46 | + |
| 47 | + Schema::table('bans', function (Blueprint $table) { |
| 48 | + // Optimize ban lookups by user + expiration date |
| 49 | + $table->index(['user_id', 'expires'], 'idx_bans_user_expires'); |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Reverse the migrations. |
| 55 | + */ |
| 56 | + public function down(): void |
| 57 | + { |
| 58 | + Schema::table('players', function (Blueprint $table) { |
| 59 | + $table->dropIndex('idx_players_ladder_username'); |
| 60 | + $table->dropIndex('idx_players_user_id'); |
| 61 | + }); |
| 62 | + |
| 63 | + Schema::table('users', function (Blueprint $table) { |
| 64 | + $table->dropIndex('idx_users_ip_address_id'); |
| 65 | + $table->dropIndex('idx_users_primary_user_id'); |
| 66 | + }); |
| 67 | + |
| 68 | + Schema::table('game_reports', function (Blueprint $table) { |
| 69 | + $table->dropIndex('idx_game_reports_valid_best_created'); |
| 70 | + }); |
| 71 | + |
| 72 | + Schema::table('qm_match_players', function (Blueprint $table) { |
| 73 | + $table->dropIndex('idx_qm_match_players_player_waiting'); |
| 74 | + }); |
| 75 | + |
| 76 | + Schema::table('ip_addresses', function (Blueprint $table) { |
| 77 | + $table->dropIndex('idx_ip_addresses_address'); |
| 78 | + }); |
| 79 | + |
| 80 | + Schema::table('player_active_handles', function (Blueprint $table) { |
| 81 | + $table->dropIndex('idx_pah_player_ladder_created'); |
| 82 | + }); |
| 83 | + |
| 84 | + Schema::table('bans', function (Blueprint $table) { |
| 85 | + $table->dropIndex('idx_bans_user_expires'); |
| 86 | + }); |
| 87 | + } |
| 88 | +}; |
0 commit comments