You are here

bat_unit.install in Booking and Availability Management Tools for Drupal 7

Same filename and directory in other branches
  1. 8 modules/bat_unit/bat_unit.install

Sets up the base table for our entity and a table to store information about the entity types.

File

modules/bat_unit/bat_unit.install
View source
<?php

/**
 * @file
 * Sets up the base table for our entity and a table to store information about
 * the entity types.
 */

/**
 * Implements hook_install().
 */
function bat_unit_install() {
  bat_unit_install_create_default_unit_bundle();
}

/**
 * Implements hook_schema().
 */
function bat_unit_schema() {
  $schema = array();
  $schema['bat_units'] = array(
    'description' => 'The base table for Units.',
    'fields' => array(
      'unit_id' => array(
        'description' => 'Primary Key: Identifier for a Unit.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'type_id' => array(
        'description' => 'Identifier for this unit\'s type.',
        'type' => 'int',
        'unsigned' => TRUE,
        'default' => NULL,
      ),
      'type' => array(
        'description' => 'The {unit_bundle}.type of this Unit.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'language' => array(
        'description' => 'The language of the Unit.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'name' => array(
        'description' => 'The name of the Unit - a human-readable identifier.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the Unit was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'changed' => array(
        'description' => 'The Unix timestamp when the Unit was most recently saved.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'status' => array(
        'description' => 'Boolean indicating whether the unit is published (visible to non-administrators).',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
      'uid' => array(
        'description' => 'The {users}.uid that created this unit.',
        '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.',
      ),
    ),
    'primary key' => array(
      'unit_id',
    ),
    'indexes' => array(
      'type' => array(
        'type',
      ),
    ),
  );
  $schema['bat_unit_bundle'] = array(
    'description' => 'Stores information about defined unit bundles.',
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique unit bundle identifier.',
      ),
      'type' => array(
        'description' => 'The machine-readable name of this unit bundle.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'label' => array(
        'description' => 'The human-readable name of this unit bundle.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'The weight of this unit bundle in relation to others.',
      ),
      'data' => array(
        'type' => 'text',
        'not null' => FALSE,
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'A serialized array of additional data related to this unit bundle.',
      ),
      'status' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0x1,
        'size' => 'tiny',
        'description' => 'The exportable status of the entity.',
      ),
      'module' => array(
        'description' => 'The name of the providing module if the entity has been defined in code.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array(
      'id',
    ),
    'unique keys' => array(
      'type' => array(
        'type',
      ),
    ),
  );
  $schema['bat_types'] = array(
    'description' => 'The base table for Types.',
    'fields' => array(
      'type_id' => array(
        'description' => 'Primary Key: Identifier for a Type.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'revision_id' => array(
        'description' => 'The current {bat_types_revision}.revision_id version identifier.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'type' => array(
        'description' => 'The {type_bundle}.type of this Type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'language' => array(
        'description' => 'The language of the Type.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'name' => array(
        'description' => 'The name of the Type - a human-readable identifier.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the Type was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'changed' => array(
        'description' => 'The Unix timestamp when the Type was most recently saved.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'status' => array(
        'description' => 'Boolean indicating whether the type is published (visible to non-administrators).',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
      'uid' => array(
        'description' => 'The {users}.uid that created this type.',
        '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.',
      ),
    ),
    'primary key' => array(
      'type_id',
    ),
    'indexes' => array(
      'type' => array(
        'type',
      ),
    ),
  );
  $schema['bat_types_revision'] = array(
    'description' => 'The base table for Types.',
    'fields' => array(
      'type_id' => array(
        'description' => 'Unique Type id (entity id).',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'revision_id' => array(
        'description' => 'The current {bat_types_revision}.revision_id version identifier.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'name' => array(
        'description' => 'The name of this type for this revision',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'revision_uid' => array(
        'description' => 'The {users}.uid that created this Type at this revision.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'log' => array(
        'description' => 'The log entry explaining the changes in this version.',
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'big',
      ),
      'revision_timestamp' => array(
        'description' => 'The Unix timestamp when the Type was most recently saved.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'revision_id',
    ),
  );
  $schema['bat_type_bundle'] = array(
    'description' => 'Stores information about defined type bundles.',
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique type bundle identifier.',
      ),
      'type' => array(
        'description' => 'The machine-readable name of this type bundle.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'label' => array(
        'description' => 'The human-readable name of this type bundle.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'The weight of this type bundle in relation to others.',
      ),
      'data' => array(
        'type' => 'text',
        'not null' => FALSE,
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'A serialized array of additional data related to this type bundle.',
      ),
      'default_event_value_field_ids' => array(
        'type' => 'text',
        'not null' => FALSE,
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'A serialized array that stores this type bundle\'s default event field configuration.',
      ),
      'status' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0x1,
        'size' => 'tiny',
        'description' => 'The exportable status of the entity.',
      ),
      'module' => array(
        'description' => 'The name of the providing module if the entity has been defined in code.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array(
      'id',
    ),
    'unique keys' => array(
      'type' => array(
        'type',
      ),
    ),
  );
  return $schema;
}

/**
 * Create default unit bundle.
 */
function bat_unit_install_create_default_unit_bundle() {
  $default_unit_bundle = bat_unit_bundle_create(array(
    'type' => 'default',
    'label' => 'Default',
  ));
  $default_unit_bundle
    ->save();
}

/**
 * Units should have type_id set to null if saved without a reference
 * to a type_id.
 */
function bat_unit_update_7100() {
  db_change_field('bat_units', 'type_id', 'type_id', array(
    'description' => 'Identifier for this unit\'s type.',
    'type' => 'int',
    'unsigned' => TRUE,
    'default' => NULL,
  ));
}

/**
 * Create table 'bat_types_revision'.
 */
function bat_unit_update_7101() {
  db_create_table('bat_types_revision', drupal_get_schema_unprocessed('bat_unit', 'bat_types_revision'));
  $field = array(
    'description' => 'The current {bat_types_revision}.revision_id version identifier.',
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  );
  db_add_field('bat_types', 'revision_id', $field);
}

/**
 * Add revisions for all existing types.
 */
function bat_unit_update_7102() {
  db_update('bat_types')
    ->expression('revision_id', 'type_id')
    ->execute();
  $types = db_select('bat_types', 'p')
    ->fields('p', array(
    'type_id',
    'name',
    'uid',
    'created',
  ))
    ->execute()
    ->fetchAll();
  foreach ($types as $type) {
    db_insert('bat_types_revision')
      ->fields(array(
      'type_id' => $type->type_id,
      'revision_id' => $type->type_id,
      'name' => $type->name,
      'revision_uid' => $type->uid,
      'revision_timestamp' => $type->created,
      'log' => '',
    ))
      ->execute();
  }
}

Functions

Namesort descending Description
bat_unit_install Implements hook_install().
bat_unit_install_create_default_unit_bundle Create default unit bundle.
bat_unit_schema Implements hook_schema().
bat_unit_update_7100 Units should have type_id set to null if saved without a reference to a type_id.
bat_unit_update_7101 Create table 'bat_types_revision'.
bat_unit_update_7102 Add revisions for all existing types.