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:
parent
e256e765e7
commit
36573ecbf0
1 changed files with 6 additions and 14 deletions
|
|
@ -6,28 +6,26 @@
|
||||||
|
|
||||||
return new class extends Migration
|
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.
|
* Run the migrations.
|
||||||
*/
|
*/
|
||||||
public function up(): void
|
public function up(): void
|
||||||
{
|
{
|
||||||
try {
|
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
|
// 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)');
|
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
|
// 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\'))');
|
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) {
|
} catch (\Exception $e) {
|
||||||
Log::error('Error adding optimized indexes to activity_log: '.$e->getMessage());
|
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
|
public function down(): void
|
||||||
{
|
{
|
||||||
try {
|
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_type_uuid_created_at');
|
||||||
DB::unprepared('DROP INDEX CONCURRENTLY IF EXISTS idx_activity_properties_status');
|
DB::unprepared('DROP INDEX CONCURRENTLY IF EXISTS idx_activity_properties_status');
|
||||||
|
|
||||||
DB::beginTransaction();
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
Log::error('Error dropping optimized indexes from activity_log: '.$e->getMessage());
|
Log::error('Error dropping optimized indexes from activity_log: '.$e->getMessage());
|
||||||
DB::beginTransaction();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue