fix: use correct property declaration for withinTransaction

The parent Migration class defines $withinTransaction without a type
hint. Setting it without redeclaring (no "bool" type) avoids the PHP
8.4 error: "Type of Migration::$withinTransaction must not be defined"

This is the proper Laravel way to disable transactions for migrations
that need to run CREATE INDEX CONCURRENTLY in PostgreSQL.

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Andras Bacsai 2025-10-10 18:41:33 +02:00
parent e256e765e7
commit 36573ecbf0

View file

@ -6,28 +6,26 @@
return new class extends Migration
{
/**
* Disable transactions for this migration because CREATE INDEX CONCURRENTLY
* cannot run inside a transaction block in PostgreSQL.
*/
public $withinTransaction = false;
/**
* Run the migrations.
*/
public function up(): void
{
try {
// CREATE INDEX CONCURRENTLY cannot run inside a transaction block
// We need to commit any open transaction first
DB::commit();
// Add specific index for type_uuid queries with ordering
DB::unprepared('CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_activity_type_uuid_created_at ON activity_log ((properties->>\'type_uuid\'), created_at DESC)');
// Add specific index for status queries on properties
DB::unprepared('CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_activity_properties_status ON activity_log ((properties->>\'status\'))');
// Begin a new transaction for subsequent migrations
DB::beginTransaction();
} catch (\Exception $e) {
Log::error('Error adding optimized indexes to activity_log: '.$e->getMessage());
// Ensure we have a transaction for subsequent migrations
DB::beginTransaction();
}
}
@ -37,16 +35,10 @@ public function up(): void
public function down(): void
{
try {
// DROP INDEX CONCURRENTLY cannot run inside a transaction block
DB::commit();
DB::unprepared('DROP INDEX CONCURRENTLY IF EXISTS idx_activity_type_uuid_created_at');
DB::unprepared('DROP INDEX CONCURRENTLY IF EXISTS idx_activity_properties_status');
DB::beginTransaction();
} catch (\Exception $e) {
Log::error('Error dropping optimized indexes from activity_log: '.$e->getMessage());
DB::beginTransaction();
}
}
};