Data migration: Coupon codes does not use paging

When there are millions of coupons codes to be migrated from M1 to M2 it will run out of memory because the UB Data Migration tool is trying to load all coupons into a single collection. It needs to implement paging in order to be able to migrate all the data. Here’s the replaced function which includes paging
 
private function _migrateSalesRuleCoupons($salesRule, $salesRule2, $keepOriginalId)
{
//check has keep original customer ids
$keepCustomerId = UBMigrate::getSetting(6, ‘keep_original_id’);
/**
* Table: salesrule_coupon
*/
$condition = “rule_id = {$salesRule->rule_id}”;
$totalCoupons = Mage1SalesruleCoupon::model()->count($condition);
$numberOfPages = ceil($totalCoupons / $this->limit);
for ($currentPage = 0; $currentPage <= $numberOfPages; $currentPage++) {
$coupons = UBMigrate::getListObjects(‘Mage1SalesruleCoupon’, $condition, $currentPage * $this->limit, $this->limit, “coupon_id ASC”);
if ($coupons) {
foreach ($coupons as $coupon) {
$m2Id = UBMigrate::getM2EntityId(7, ‘salesrule_coupon’, $coupon->coupon_id);
$canReset = UBMigrate::RESET_YES;
if (is_null($m2Id)) {
$coupon2 = Mage2SalesruleCoupon::model()->find(“code = ‘” . addslashes($coupon->code) . “‘”);
if (!$coupon2) {
//add new
$coupon2 = new Mage2SalesruleCoupon();
//fill values
foreach ($coupon2->attributes as $key => $value) {
if (isset($coupon->$key)) {
$coupon2->$key = $coupon->$key;
}
$coupon2->coupon_id = ($keepOriginalId) ? $coupon->coupon_id : null;
//because some entity ids was changed
$coupon2->rule_id = $salesRule2->rule_id;
if (empty($coupon2->expiration_date) || $coupon2->expiration_date === ‘0000-00-00 00:00:00’) {
$coupon2->expiration_date = NULL;
}
if (empty($coupon2->created_at) || $coupon2->created_at === ‘0000-00-00 00:00:00’) {
$coupon2->created_at = date(“Y-m-d H:i:s”);
}
if ($coupon2->usage_per_customer) {
$coupon2->usage_per_customer = UBMigrate::getM2EntityId(6, ‘customer_entity’, $coupon2->usage_per_customer);
}
}
} else {
$canReset = UBMigrate::RESET_NO;
}
} else {
//update
$coupon2 = Mage2SalesruleCoupon::model()->find(“coupon_id = {$m2Id}”);
foreach ($coupon2->attributes as $key => $value) {
if (isset($coupon->$key) AND !in_array($key, array(‘coupon_id’, ‘rule_id’))) {
$coupon2->$key = $coupon->$key;
}
if (!$keepCustomerId && $coupon2->usage_per_customer) {
$coupon2->usage_per_customer = UBMigrate::getM2EntityId(6, ‘customer_entity’, $coupon2->usage_per_customer);
}
if (empty($coupon2->created_at) || $coupon2->created_at === ‘0000-00-00 00:00:00’) {
$coupon2->created_at = date(“Y-m-d H:i:s”);
}
if (empty($coupon2->expiration_date) || $coupon2->expiration_date === ‘0000-00-00 00:00:00’) {
$coupon2->expiration_date = NULL;
}
}
}
//save/update
if ($coupon2->save()) {
if (is_null($m2Id)) {
//update to map log
UBMigrate::log([
‘entity_name’ => $coupon->tableName(),
‘m1_id’ => $coupon->coupon_id,
‘m2_id’ => $coupon2->coupon_id,
‘m2_model_class’ => get_class($coupon2),
‘m2_key_field’ => ‘coupon_id’,
‘can_reset’ => $canReset,
‘step_index’ => $this->stepIndex
]);
}
$this->_traceInfo();
/**
* Table: salesrule_coupon_usage
*/
$couponUsages = Mage1SalesruleCouponUsage::model()->findAll(“coupon_id = {$coupon->coupon_id}”);
if ($couponUsages) {
foreach ($couponUsages as $couponUsage) {
$customerId2 = (!$keepCustomerId) ? UBMigrate::getM2EntityId(6, ‘customer_entity’, $couponUsage->customer_id) : $couponUsage->customer_id;
if ($customerId2) {
$couponUsage2 = Mage2SalesruleCouponUsage::model()->find(“coupon_id = {$coupon2->coupon_id} AND customer_id = {$customerId2}”);
if (!$couponUsage2) {
$couponUsage2 = new Mage2SalesruleCouponUsage();
$couponUsage2->coupon_id = $coupon2->coupon_id;
$couponUsage2->customer_id = $customerId2;
}
$couponUsage2->times_used = $couponUsage->times_used;
if (!$couponUsage2->save()) {
$this->errors[] = get_class($couponUsage2) . “: ” . UBMigrate::getStringErrors($couponUsage2->getErrors());
} else {
$this->_traceInfo();
}
}
}
}
} else {
$this->errors[] = get_class($coupon2) . “: ” . UBMigrate::getStringErrors($coupon2->getErrors());
}
}
}
}
return true;
}




3 answers

Profile photo of Mall Staff 184060.00 $tone November 2, 2020
Public

Hi Danny,
I am sorry for replying to you a little bit late since we’ve just been back from our weekend. 

When there are millions of coupons codes to be migrated from M1 to M2 it will run out of memory because the UB Data Migration tool is trying to load all coupons into a single collection.

Please note that we list the coupon code filtered by one specific sales rule (rule_id) to migrate to M2. 

$coupons = Mage1SalesruleCoupon::model()->findAll("rule_id = {$salesRule->rule_id}");

It seems that the case with a big number of coupons under 1 sales rule is not common. If your specific circumstance comes with too many coupons under a sales rule (with millions of coupons codes), you can apply the tweak code in the function of our module to implement paging as you mentioned.

Thanks for suggestion though.
 
Regards,
Mall.
 

#1
Profile photo of Danny Verkade 20.00 $tone November 2, 2020
Public

@mall, yes, we have rules with > 1.000.000 coupon codes attached to it. It will even run out of memory with a couple of 100K of coupon codes attached to a single rule. This is an easy fix which should be taken into account in the next release. 

#2
Profile photo of Mall Staff 184060.00 $tone November 2, 2020
Public

Hi Danny,

This is an easy fix which should be taken into account in the next release. 

Thanks for sharing the case with us. We will consider handling that case in the next release of our module.

Regards,
Mall.

#3

Please login or Register to Submit Answer

Written By

Comments