2024-03-03 10:55:59 -06:00
< ? php
2024-03-03 13:07:47 -06:00
declare ( strict_types = 1 );
2024-03-03 10:55:59 -06:00
use Illuminate\Database\Migrations\Migration ;
use Illuminate\Database\QueryException ;
use Illuminate\Database\Schema\Blueprint ;
use Illuminate\Support\Facades\Schema ;
2024-03-03 13:07:47 -06:00
return new class ( ) extends Migration {
2024-03-03 10:55:59 -06:00
private const string QUERY_ERROR = 'Could not execute query (table "%s", field "%s"): %s' ;
2024-04-01 07:01:52 -05:00
private const string EXPL = 'If the index already exists (see error), or if MySQL can\'t do it, this is not an problem. Otherwise, please open a GitHub discussion.' ;
2024-03-03 10:55:59 -06:00
/**
* Run the migrations .
*/
public function up () : void
{
// add missing indices
$set = [
2024-03-03 12:58:51 -06:00
'account_meta' => [ 'account_id' ],
2024-03-03 13:01:35 -06:00
'accounts' => [ 'user_id' , 'user_group_id' , 'account_type_id' ],
'budgets' => [ 'user_id' , 'user_group_id' ],
2024-03-03 12:58:51 -06:00
'journal_meta' => [ 'transaction_journal_id' , 'data' , 'name' ],
'category_transaction_journal' => [ 'transaction_journal_id' ],
2024-03-03 13:01:35 -06:00
'categories' => [ 'user_id' , 'user_group_id' ],
'transaction_groups' => [ 'user_id' , 'user_group_id' ],
'transaction_journals' => [ 'user_id' , 'user_group_id' , 'date' , 'transaction_group_id' , 'transaction_type_id' , 'transaction_currency_id' , 'bill_id' ],
2024-03-03 13:07:47 -06:00
'transactions' => [ 'account_id' , 'transaction_journal_id' , 'transaction_currency_id' , 'foreign_currency_id' ],
2024-03-03 10:55:59 -06:00
];
foreach ( $set as $table => $fields ) {
foreach ( $fields as $field ) {
try {
Schema :: table (
$table ,
static function ( Blueprint $blueprint ) use ( $field ) : void {
$blueprint -> index ( $field );
}
);
} catch ( QueryException $e ) {
app ( 'log' ) -> error ( sprintf ( self :: QUERY_ERROR , $table , $field , $e -> getMessage ()));
app ( 'log' ) -> error ( self :: EXPL );
}
}
}
}
/**
* Reverse the migrations .
*/
2024-03-03 13:07:47 -06:00
public function down () : void {}
2024-03-03 10:55:59 -06:00
};