function commerce_purchase_order_update_7001 in Commerce Purchase Order 7
Add transaction_id field and make it a primary key.
File
- ./
commerce_purchase_order.install, line 51
Code
function commerce_purchase_order_update_7001() {
$spec = array(
'description' => 'The {commerce_payment_transaction}.transaction_id.',
'type' => 'int',
'unsigned' => TRUE,
'default' => NULL,
);
db_add_field('commerce_purchase_order', 'transaction_id', $spec);
// Try connecting existing PO's with transactions
$result = db_select('commerce_payment_transaction', 't')
->fields('t')
->condition('payment_method', 'commerce_purchase_order', 'LIKE')
->execute();
while ($transaction = $result
->fetchAssoc()) {
$message_variables = unserialize($transaction['message_variables']);
if (isset($message_variables['@po_number'])) {
$transaction_id = $transaction['transaction_id'];
$order_id = $transaction['order_id'];
$po_number = $message_variables['@po_number'];
db_update('commerce_purchase_order')
->fields(array(
'transaction_id' => $transaction_id,
))
->condition('po_number', $po_number, '=')
->condition('order_id', $order_id, '=')
->execute();
}
}
// Delete remaining Purchase Orders (without referencing transactions).
// Needed in order to use transaction_id's as primary keys.
// Normally the number should always be 0.
$num_deleted = db_delete('commerce_purchase_order')
->condition('transaction_id', NULL, 'IS')
->execute();
// Primary key should be transaction ID.
db_drop_primary_key('commerce_purchase_order');
db_add_primary_key('commerce_purchase_order', array(
'transaction_id',
));
// Maintain backward compatibility: order_id-po_number should be unique.
db_add_unique_key('commerce_purchase_order', 'order_po', array(
'order_id',
'po_number',
));
if ($num_deleted > 0) {
return t('@num orphaned PO records have been deleted from commerce_purchase_order table.', array(
'@num' => $num_deleted,
));
}
}