Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added: existing order re-indexing to migration #517

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/controllers/FilesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function actionCreateExportZip()
return $this->asFailure($this->getErrorMessage(implode('\n', $errors)));
}

Craft::$app->getElements()->saveElement($order, true, true, true);
Craft::$app->getElements()->saveElement($order);
$transaction->commit();

return $this->asSuccess(null, ['translatedFiles' => $zipDest]);
Expand Down
10 changes: 5 additions & 5 deletions src/controllers/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ public function actionSaveOrder()
$order->wordCount = array_sum($wordCounts);

// Manual Translation will make orders 'in progress' status after creation
$success = Craft::$app->getElements()->saveElement($order, true, true, true);
$success = Craft::$app->getElements()->saveElement($order);

if (!$success) {
Translations::$plugin->logHelper->log('[' . __METHOD__ . '] Couldn’t save the order', Constants::LOG_LEVEL_ERROR);
Expand Down Expand Up @@ -617,7 +617,7 @@ public function actionSaveOrder()
$order->status = Constants::ORDER_STATUS_NEW;
$order->dateOrdered = new DateTime();

$success = Craft::$app->getElements()->saveElement($order, true, true, true);
$success = Craft::$app->getElements()->saveElement($order);

if (! $success) {
Translations::$plugin->logHelper->log('[' . __METHOD__ . '] Couldn’t save the order', Constants::LOG_LEVEL_ERROR);
Expand Down Expand Up @@ -1185,7 +1185,7 @@ public function actionUpdateOrderFilesSource()
}
}

Craft::$app->getElements()->saveElement($order, true, true, false);
Craft::$app->getElements()->saveElement($order);
$transaction->commit();
} catch (\Exception $e) {
$transaction->rollBack();
Expand Down Expand Up @@ -1229,7 +1229,7 @@ public function actionAcceptQuote()

$order->status = Constants::ORDER_STATUS_NEW;
$order->logActivity("Order quote accepted");
Craft::$app->getElements()->saveElement($order, true, true, false);
Craft::$app->getElements()->saveElement($order);

return $this->asSuccess($this->getSuccessMessage("Quote approve request sent"));
}
Expand Down Expand Up @@ -1266,7 +1266,7 @@ public function actionDeclineQuote()

$order->status = Constants::ORDER_STATUS_GETTING_QUOTE;
$order->logActivity("Order quote declined");
Craft::$app->getElements()->saveElement($order, true, true, false);
Craft::$app->getElements()->saveElement($order);

return $this->asSuccess($this->getSuccessMessage("Quote decline request sent"));
}
Expand Down
41 changes: 41 additions & 0 deletions src/migrations/m240829_054957_drop_commerce_draft_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace acclaro\translations\migrations;

use Craft;
use craft\db\Migration;
use acclaro\translations\elements\Order;

/**
* m240829_054957_drop_commerce_draft_table migration.
Expand All @@ -20,6 +22,45 @@ public function safeUp(): bool

echo "Done dropping translations_commercedrafts table...\n";

echo "Re-indexing all existing orders...\n";

$batchSize = 100;
$offset = 0;
$totalProcessed = 0;

while (true) {
$orders = Order::find()
->limit($batchSize)
->offset($offset)
->all();

if (empty($orders)) {
break;
}

foreach ($orders as $order) {
try {
$order = Craft::$app->getElements()->getElementById($order->id, null, $order->sourceSite);
if ($order) {
$order->resaving = true;
if (!Craft::$app->getElements()->saveElement($order)) {
Craft::error('Failed to save order ID: ' . $order->id, __METHOD__);
}
}
} catch (\Throwable $e) {
Craft::error('Error re-saving order ID: ' . $order->id . '. Error: ' . $e->getMessage(), __METHOD__);
throw $e;
}
}

$totalProcessed += count($orders);
$offset += $batchSize;

echo "Processed $totalProcessed orders so far...\n";
}

echo "Done re-indexing all orders.\n";

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/repository/DraftRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function applyTranslationDraft($fileId, $file='', $draft='')
$canonical = $draft->getCanonical();
$draft->setFieldValues($canonical->getFieldValues());
// Let's try saving the element prior to applying draft
if (!Craft::$app->getElements()->saveElement($draft, true, true, true)) {
if (!Craft::$app->getElements()->saveElement($draft)) {
throw new InvalidElementException($draft);
}

Expand Down