Update in segments

This commit is contained in:
Michael 2021-02-21 07:06:20 +00:00
parent d78cf95495
commit d37f3de3e6

View file

@ -422,7 +422,7 @@ class Post
{ {
$affected = 0; $affected = 0;
Logger::debug('Start Update', ['fields' => $fields, 'condition' => $condition, 'uid' => local_user(),'callstack' => System::callstack(10)]); Logger::info('Start Update', ['fields' => $fields, 'condition' => $condition, 'uid' => local_user(),'callstack' => System::callstack(10)]);
// Don't allow changes to fields that are responsible for the relation between the records // Don't allow changes to fields that are responsible for the relation between the records
unset($fields['id']); unset($fields['id']);
@ -442,78 +442,111 @@ class Post
$update_fields = DBStructure::getFieldsForTable('post-user', $fields); $update_fields = DBStructure::getFieldsForTable('post-user', $fields);
if (!empty($update_fields)) { if (!empty($update_fields)) {
$affected_count = 0;
$rows = DBA::selectToArray('post-view', ['post-user-id'], $condition); $rows = DBA::selectToArray('post-view', ['post-user-id'], $condition);
$puids = array_column($rows, 'post-user-id'); $puids = array_column($rows, 'post-user-id');
if (!DBA::update('post-user', $update_fields, ['id' => $puids])) { while (!empty($puids)) {
DBA::rollback(); $segment = array_splice($puids, 0, 100);
Logger::notice('Updating post-user failed', ['fields' => $update_fields, 'condition' => $condition]); if (!DBA::update('post-user', $update_fields, ['id' => $segment])) {
return false; DBA::rollback();
Logger::notice('Updating post-user failed', ['fields' => $update_fields, 'condition' => $condition]);
return false;
}
$affected_count += DBA::affectedRows();
} }
$affected = DBA::affectedRows(); $affected = $affected_count;
} }
$update_fields = DBStructure::getFieldsForTable('post-content', $fields); $update_fields = DBStructure::getFieldsForTable('post-content', $fields);
if (!empty($update_fields)) { if (!empty($update_fields)) {
$affected_count = 0;
$rows = DBA::selectToArray('post-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]); $rows = DBA::selectToArray('post-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]);
$uriids = array_column($rows, 'uri-id'); $uriids = array_column($rows, 'uri-id');
if (!DBA::update('post-content', $update_fields, ['uri-id' => $uriids])) { $elements = $uriids;
DBA::rollback(); while (!empty($elements)) {
Logger::notice('Updating post-content failed', ['fields' => $update_fields, 'condition' => $condition]); $segment = array_splice($elements, 0, 100);
return false; if (!DBA::update('post-content', $update_fields, ['uri-id' => $segment])) {
DBA::rollback();
Logger::notice('Updating post-content failed', ['fields' => $update_fields, 'condition' => $condition]);
return false;
}
$affected_count += DBA::affectedRows();
} }
$affected = max($affected, DBA::affectedRows()); $affected = max($affected, $affected_count);
} }
$update_fields = DBStructure::getFieldsForTable('post', $fields); $update_fields = DBStructure::getFieldsForTable('post', $fields);
if (!empty($update_fields)) { if (!empty($update_fields)) {
$affected_count = 0;
if (empty($uriids)) { if (empty($uriids)) {
$rows = DBA::selectToArray('post-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]); $rows = DBA::selectToArray('post-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]);
$uriids = array_column($rows, 'uri-id'); $uriids = array_column($rows, 'uri-id');
} }
if (!DBA::update('post', $update_fields, ['uri-id' => $uriids])) { $elements = $uriids;
DBA::rollback(); while (!empty($elements)) {
Logger::notice('Updating post failed', ['fields' => $update_fields, 'condition' => $condition]); $segment = array_splice($elements, 0, 100);
return false; if (!DBA::update('post', $update_fields, ['uri-id' => $segment])) {
DBA::rollback();
Logger::notice('Updating post failed', ['fields' => $update_fields, 'condition' => $condition]);
return false;
}
$affected_count += DBA::affectedRows();
} }
$affected = max($affected, DBA::affectedRows()); $affected = max($affected, $affected_count);
} }
$update_fields = Post\DeliveryData::extractFields($fields); $update_fields = Post\DeliveryData::extractFields($fields);
if (!empty($update_fields)) { if (!empty($update_fields)) {
$affected_count = 0;
if (empty($uriids)) { if (empty($uriids)) {
$rows = DBA::selectToArray('post-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]); $rows = DBA::selectToArray('post-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]);
$uriids = array_column($rows, 'uri-id'); $uriids = array_column($rows, 'uri-id');
} }
if (!DBA::update('post-delivery-data', $update_fields, ['uri-id' => $uriids])) { $elements = $uriids;
DBA::rollback(); while (!empty($elements)) {
Logger::notice('Updating post-delivery-data failed', ['fields' => $update_fields, 'condition' => $condition]); $segment = array_splice($elements, 0, 100);
return false; if (!DBA::update('post-delivery-data', $update_fields, ['uri-id' => $segment])) {
DBA::rollback();
Logger::notice('Updating post-delivery-data failed', ['fields' => $update_fields, 'condition' => $condition]);
return false;
}
$affected_count += DBA::affectedRows();
} }
$affected = max($affected, DBA::affectedRows()); $affected = max($affected, $affected_count);
} }
$update_fields = DBStructure::getFieldsForTable('post-thread', $fields); $update_fields = DBStructure::getFieldsForTable('post-thread', $fields);
if (!empty($update_fields)) { if (!empty($update_fields)) {
$affected_count = 0;
$rows = DBA::selectToArray('post-view', ['uri-id'], $thread_condition, ['group_by' => ['uri-id']]); $rows = DBA::selectToArray('post-view', ['uri-id'], $thread_condition, ['group_by' => ['uri-id']]);
$uriids = array_column($rows, 'uri-id'); $uriids = array_column($rows, 'uri-id');
if (!DBA::update('post-thread', $update_fields, ['uri-id' => $uriids])) { while (!empty($uriids)) {
DBA::rollback(); $segment = array_splice($uriids, 0, 100);
Logger::notice('Updating post-thread failed', ['fields' => $update_fields, 'condition' => $condition]); if (!DBA::update('post-thread', $update_fields, ['uri-id' => $segment])) {
return false; DBA::rollback();
Logger::notice('Updating post-thread failed', ['fields' => $update_fields, 'condition' => $condition]);
return false;
}
$affected_count += DBA::affectedRows();
} }
$affected = max($affected, DBA::affectedRows()); $affected = max($affected, $affected_count);
} }
$update_fields = DBStructure::getFieldsForTable('post-thread-user', $fields); $update_fields = DBStructure::getFieldsForTable('post-thread-user', $fields);
if (!empty($update_fields)) { if (!empty($update_fields)) {
$affected_count = 0;
$rows = DBA::selectToArray('post-view', ['post-user-id'], $thread_condition); $rows = DBA::selectToArray('post-view', ['post-user-id'], $thread_condition);
$thread_puids = array_column($rows, 'post-user-id'); $thread_puids = array_column($rows, 'post-user-id');
if (!DBA::update('post-thread-user', $update_fields, ['post-user-id' => $thread_puids])) { while (!empty($thread_puids)) {
DBA::rollback(); $segment = array_splice($thread_puids, 0, 100);
Logger::notice('Updating post-thread-user failed', ['fields' => $update_fields, 'condition' => $condition]); if (!DBA::update('post-thread-user', $update_fields, ['post-user-id' => $segment])) {
return false; DBA::rollback();
Logger::notice('Updating post-thread-user failed', ['fields' => $update_fields, 'condition' => $condition]);
return false;
}
$affected_count += DBA::affectedRows();
} }
$affected = max($affected, DBA::affectedRows()); $affected = max($affected, $affected_count);
} }
DBA::commit(); DBA::commit();