You are here

function text_update_6001 in Content Construction Kit (CCK) 6.3

Same name and namespace in other branches
  1. 6 modules/text/text.install \text_update_6001()
  2. 6.2 modules/text/text.install \text_update_6001()

Set all columns to accept NULL values and set empty string values in the database to NULL.

Leaving it up to module developers to handle conversion of numbers to NULL values, since there are times when zeros are valid data and times when they should be NULL.

1 call to text_update_6001()
text_update_6003 in modules/text/text.install

File

modules/text/text.install, line 78
Implementation of hook_install().

Code

function text_update_6001(&$sandbox) {
  include_once './' . drupal_get_path('module', 'content') . '/content.install';
  drupal_load('module', 'content');
  $ret = array();
  if (!isset($sandbox['progress'])) {
    if ($abort = content_check_update('text')) {
      return $abort;
    }

    // Get the latest cache values and schema.
    content_clear_type_cache(TRUE, TRUE);
    $types = content_types_install();
    if (empty($types)) {
      return $ret;
    }
    $sandbox['fields'] = array();
    foreach ($types as $type_name => $fields) {
      foreach ($fields as $field) {
        if ($field['type'] == 'text') {
          $sandbox['fields'][] = $field;
        }
      }
    }
    if (empty($sandbox['fields'])) {
      return $ret;
    }
    $sandbox['progress'] = 0;
    $sandbox['visited'] = array();
  }
  $field = $sandbox['fields'][$sandbox['progress']];

  // We only want to process a field once -- if we hit it a second time,
  // that means it's its own table and it should have already been updated.
  if (!in_array($field['field_name'], $sandbox['visited'])) {
    $db_info = content_database_info($field);
    $table = $db_info['table'];
    foreach ($db_info['columns'] as $column => $attributes) {
      $attributes['not null'] = FALSE;
      $column = $attributes['column'];
      db_change_field($ret, $table, $column, $column, $attributes);

      // TODO: errors on text/blob columns: no default value allowed (!)
      db_field_set_no_default($ret, $table, $column);
      if ($attributes['type'] == 'varchar' || $attributes['type'] == 'text') {
        $ret[] = update_sql("UPDATE {" . $table . "} SET " . $column . " = NULL WHERE " . $column . " = ''");
      }
      else {

        // TODO: replace format = 0 with format = NULL ?? Is this right ?
        $ret[] = update_sql("UPDATE {" . $table . "} SET " . $column . " = NULL WHERE " . $column . " = 0");
      }
    }
    $sandbox['visited'][] = $field['field_name'];
  }
  $sandbox['progress']++;
  $ret['#finished'] = $sandbox['progress'] / count($sandbox['fields']);
  return $ret;
}