You are here

function content_update_1003 in Content Construction Kit (CCK) 5

Rename data tables to avoid collision with core node_* tables

File

./content.install, line 407

Code

function content_update_1003() {
  $ret = array();
  include_once './' . drupal_get_path('module', 'content') . '/content.module';
  include_once './' . drupal_get_path('module', 'content') . '/content_admin.inc';
  $rename = array();
  $types = content_types();
  $fields = content_fields();

  // "per content type" tables
  foreach ($types as $type) {
    $old_name = _content_tablename($type['type'], CONTENT_DB_STORAGE_PER_CONTENT_TYPE);
    $new_name = _content_tablename($type['type'], CONTENT_DB_STORAGE_PER_CONTENT_TYPE, 1003);
    if (db_table_exists($old_name)) {
      if (!in_array($old_name, array(
        'node_access',
        'node_comments_statistics',
        'node_counter',
        'node_field',
        'node_field_instance',
        'node_group',
        'node_group_fields',
        'node_revisions',
        'node_type',
      ))) {
        $rename[$old_name] = $new_name;
      }
      else {

        // TODO : what ? create the table ?
      }
    }
  }

  // "per field" tables
  foreach ($fields as $field) {
    $old_name = _content_tablename($field['field_name'], CONTENT_DB_STORAGE_PER_FIELD);
    $new_name = _content_tablename($field['field_name'], CONTENT_DB_STORAGE_PER_FIELD, 1003);
    $old_name = 'node_data_' . $field['field_name'];
    $new_name = 'content_' . $field['field_name'];
    if (db_table_exists($old_name)) {
      $rename[$old_name] = $new_name;
    }
  }

  // rename
  foreach ($rename as $old_name => $new_name) {
    switch ($GLOBALS['db_type']) {
      case 'mysql':
      case 'mysqli':
        $ret[] = update_sql("RENAME TABLE {" . $old_name . "} TO {" . $new_name . "}");
        break;
      case 'pgsql':
        $ret[] = update_sql("ALTER TABLE {" . $old_name . "} RENAME TO {" . $new_name . "}");
        break;
    }
  }
  variable_set('content_schema_version', 1003);
  return $ret;
}