commerce_pricelist.install in Commerce Pricelist 7
Same filename and directory in other branches
Install for a Commerce Pricelist - create the base tables for our entities.
File
commerce_pricelist.installView source
<?php
/**
* @file
* Install for a Commerce Pricelist - create the base tables for our entities.
*/
/**
* Implements hook_schema().
*
* @ingroup commerce_pricelist
*/
function commerce_pricelist_schema() {
$schema = array();
$schema['commerce_pricelist_list'] = array(
'description' => 'The base table for our price list entity.',
'fields' => array(
'list_id' => array(
'description' => 'The primary identifier for a list.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'title' => array(
'description' => 'The name of the pricelist.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
),
'created' => array(
'description' => 'The Unix timestamp of the created date.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'changed' => array(
'description' => 'The Unix timestamp of the changed date.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'status' => array(
'description' => 'Boolean indicating whether the price list is active.',
'type' => 'int',
'not null' => TRUE,
'default' => 1,
),
'uid' => array(
'description' => 'The {users}.uid that owns this item; initially, this is the user that created it.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'weight' => array(
'description' => 'The weight of the list.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'data' => array(
'type' => 'blob',
'not null' => FALSE,
'size' => 'big',
'serialize' => TRUE,
'description' => 'A serialized array of additional data.',
),
),
'foreign keys' => array(
'pricelist_author' => array(
'table' => 'users',
'columns' => array(
'uid' => 'uid',
),
),
),
'primary key' => array(
'list_id',
),
);
$schema['commerce_pricelist_item'] = array(
'description' => 'The base table for our price list item entity.',
'fields' => array(
'item_id' => array(
'description' => 'The primary identifier for an item.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'sku' => array(
'description' => 'The unique, human-readable identifier for a product.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
),
'valid_from' => array(
'description' => 'The Unix timestamp of the valid from date.',
'type' => 'int',
'not null' => FALSE,
'default' => 0,
),
'valid_to' => array(
'description' => 'The Unix timestamp of the valid to date.',
'type' => 'int',
'not null' => FALSE,
'default' => COMMERCE_PRICELIST_UNIX_TIME_APOCALYPSE,
),
'quantity' => array(
'type' => 'numeric',
'size' => 'normal',
'not null' => TRUE,
'default' => 0,
'precision' => 10,
'scale' => 2,
),
'price_amount' => array(
'description' => 'The price in commerce standard format without decimals.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'currency_code' => array(
'description' => 'The currency code for the price.',
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
),
'pricelist_id' => array(
'type' => 'int',
'default' => 0,
'description' => 'The List ID.',
'not null' => TRUE,
),
),
'primary key' => array(
'item_id',
),
'indexes' => array(
'sku' => array(
'sku',
),
'pricelist_id' => array(
'pricelist_id',
),
),
'foreign keys' => array(
'item_pricelist' => array(
'table' => 'commerce_pricelist',
'columns' => array(
'pricelist_id' => 'list_id',
),
),
'sku' => array(
'table' => 'commerce_product',
'columns' => array(
'sku' => 'sku',
),
),
),
);
// Support for UUID module.
if (module_exists('uuid')) {
module_load_include('install', 'uuid', 'uuid');
$field = uuid_schema_field_definition();
$schema['commerce_pricelist_item']['fields']['uuid'] = $field;
$schema['commerce_pricelist_item']['indexes']['uuid'] = array(
'uuid',
);
$schema['commerce_pricelist_list']['fields']['uuid'] = $field;
$schema['commerce_pricelist_list']['indexes']['uuid'] = array(
'uuid',
);
}
return $schema;
}
/**
* Implements hook_uninstall().
*/
function commerce_pricelist_enable() {
entity_info_cache_clear();
}
/**
* Implements hook_uninstall().
*
* @ingroup commerce_pricelist
*/
function commerce_pricelist_uninstall() {
}
/**
* Add list_id field to pricelist item table.
*/
function commerce_pricelist_update_7001(&$sandbox) {
if (!db_field_exists('commerce_pricelist_item', 'pricelist_id')) {
$field = array(
'type' => 'int',
'default' => 0,
'description' => 'The List ID.',
'not null' => TRUE,
);
$keys = array(
'foreign keys' => array(
'item_pricelist' => array(
'table' => 'commerce_pricelist',
'columns' => array(
'pricelist_id' => 'list_id',
),
),
),
);
db_drop_unique_key('commerce_pricelist_item', 'item_pricelist');
db_add_field('commerce_pricelist_item', 'pricelist_id', $field, $keys);
db_add_index('commerce_pricelist_item', 'pricelist_id', array(
'pricelist_id',
));
}
}
/**
* Migrate pricelist uuids to ids. Depending on how many items there are this
* may take a while.
*/
function commerce_pricelist_update_7002(&$sandbox) {
if (db_field_exists('commerce_pricelist_item', 'pricelist_uuid')) {
// Prepare query
$query = db_select('commerce_pricelist_item', 'i');
$query
->join('commerce_pricelist_list', 'l', 'i.pricelist_uuid = l.uuid');
$query
->fields('i', array(
'item_id',
))
->fields('l', array(
'list_id',
));
// If this is the first pass through this update function then set some variables.
if (!isset($sandbox['total'])) {
$count_query = $query
->countQuery();
$sandbox['total'] = $count_query
->execute()
->fetchField();
$sandbox['current'] = 0;
}
// How many items should be processed per pass. The higher this number is, the faster your update will
// complete, but the more likely your server will run out of memory or timeout.
$items_per_pass = 20;
// Get the items to process during this pass.
$result = $query
->range($sandbox['current'], $items_per_pass)
->execute();
while ($row = $result
->fetchAssoc()) {
db_update('commerce_pricelist_item')
->fields(array(
'pricelist_id' => $row['list_id'],
))
->condition('item_id', $row['item_id'], '=')
->execute();
// Increment "current" by 1.
$sandbox['current']++;
}
// Lets tell the site admin what we are doing.
drupal_set_message(t('Processing pricelist items, now at @current out of @total', array(
'@current' => $sandbox['current'],
'@total' => $sandbox['total'],
)));
// Set the value for finished. If current == total then finished will be 1, signifying we are done.
$sandbox['#finished'] = $sandbox['current'] / $sandbox['total'];
if ($sandbox['#finished'] === 1) {
drupal_set_message(t('Added list_id value to @items pricelist items.', array(
'@items' => $sandbox['total'],
)));
drupal_set_message(t('It is now safe to drop unused pricelist_uuid column from commerce_pricelist_item table. To do so, uncomment commerce_pricelist_update_7003() and run database updates. Please backup your data first!'));
}
}
}
/**
* Drop pricelist_uuid from pricelist items table and delete items that does
* not belong to any list.
* Commented out to prevent unintentional data loss.
* Uncomment to clean out unused column.
*/
/*
function commerce_pricelist_update_7003(&$sandbox) {
// Drop orphan items.
$num_deleted = db_delete('commerce_pricelist_item')
->condition('pricelist_id', 0)
->execute();
drupal_set_message(t('Deleted @count pricelist items that did not belong to any pricelist.',
array('@count' => $num_deleted)));
// Drop uuid column.
if (db_field_exists('commerce_pricelist_item', 'pricelist_uuid')) {
db_drop_field('commerce_pricelist_item', 'pricelist_uuid');
db_drop_index('commerce_pricelist_item', 'pricelist_uuid');
}
}
*/
/**
* Revert the pricelist rule to use our own custom price setting action.
*/
function commerce_pricelist_update_7004() {
if ($rules_config = rules_config_load('rules_commerce_price_list_set_price')) {
$rules_config
->delete();
}
}
Functions
Name | Description |
---|---|
commerce_pricelist_enable | Implements hook_uninstall(). |
commerce_pricelist_schema | Implements hook_schema(). |
commerce_pricelist_uninstall | Implements hook_uninstall(). |
commerce_pricelist_update_7001 | Add list_id field to pricelist item table. |
commerce_pricelist_update_7002 | Migrate pricelist uuids to ids. Depending on how many items there are this may take a while. |
commerce_pricelist_update_7004 | Revert the pricelist rule to use our own custom price setting action. |