Improve populating conversation_id field in add_conversation_id_column_to_notifications_table migration

This commit is contained in:
FreeScout
2026-08-30 22:33:22 -07:00
parent 7390179434
commit 5e32a629a3

View File

@@ -22,6 +22,20 @@ class AddConversationIdColumnToNotificationsTable extends Migration
});
// Populate "conversation_id" field for notifications.
try {
// Try to populate "converesation_id" in one query.
if (\Helper::isPgSql()) {
$expression = "(data::json->>'conversation_id')::bigint";
} else {
$expression = "data->>'$.conversation_id'";
}
DatabaseNotification::whereNull('conversation_id')
->where('data', 'like', '%"conversation_id":%')
->update(['conversation_id' => \DB::raw($expression)]);
} catch (\Exception $e) {
// Fallback to row-by-row approach.
// But it may fail on large datasets.
$total = DatabaseNotification::whereNull('conversation_id')
->where('data', 'like', '%"conversation_id":%')
->count();
@@ -41,6 +55,7 @@ class AddConversationIdColumnToNotificationsTable extends Migration
}
}
}
}
/**
* Reverse the migrations.