Improve populating conversation_id field in add_conversation_id_column_to_notifications_table migration
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user