You are here

function book_access_update_7201 in Book access 7.2

Update the existing tables from previous versions to match the new schema.

File

./book_access.install, line 243

Code

function book_access_update_7201() {
  $fields = array(
    'grant_view',
    'grant_update',
    'grant_delete',
    'grant_admin_access',
    'grant_add_child',
    'grant_edit_outline',
  );
  $schema = book_access_schema();
  $spec = array(
    'type' => 'int',
    'size' => 'tiny',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  );
  if (!db_table_exists('book_access_author')) {
    db_create_table('book_access_author', $schema['book_access_author']);
  }
  if (!db_table_exists('book_access_role')) {
    if (db_table_exists('book_access')) {
      @db_drop_primary_key('book_access');
      db_rename_table('book_access', 'book_access_role');
      db_add_primary_key('book_access_role', array(
        'nid',
        'rid',
      ));
    }
    else {
      db_create_table('book_access_role', $schema['book_access_role']);
    }
  }
  else {
    @db_drop_primary_key('book_access_role');
    db_change_field('book_access_role', 'rid', 'rid', array(
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
    ));
    foreach ($fields as $field) {
      if (db_field_exists('book_access_role', $field)) {
        db_change_field('book_access_role', $field, $field, $spec);
      }
      else {
        db_add_field('book_access_role', $field, $spec);
      }
    }
    db_add_primary_key('book_access_role', array(
      'nid',
      'rid',
    ));
  }
  if (!db_table_exists('book_access_user')) {
    db_create_table('book_access_user', $schema['book_access_user']);
  }
  else {
    @db_drop_primary_key('book_access_user');
    db_change_field('book_access_user', 'uid', 'uid', array(
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
    ));
    foreach ($fields as $field) {
      if (db_field_exists('book_access_user', $field)) {
        db_change_field('book_access_user', $field, $field, $spec);
      }
      else {
        db_add_field('book_access_user', $field, $spec);
      }
    }
    db_add_primary_key('book_access_user', array(
      'nid',
      'uid',
    ));
  }
  return $ret;
}