You are here

function content_alter_db_analyze in Content Construction Kit (CCK) 6.2

Same name and namespace in other branches
  1. 6.3 includes/content.admin.inc \content_alter_db_analyze()
  2. 6 includes/content.admin.inc \content_alter_db_analyze()

Schema Alter Analyze

Analyze if changes will remove columns or delta values, thus losing data. Do this so we can delete the data and fire the necessary hooks, before we actually alter the schema.

2 calls to content_alter_db_analyze()
content_alter_db_mask in includes/content.admin.inc
Create a mask for the column data that should be deleted in each field.
content_field_edit_form_validate in includes/content.admin.inc
Validate a field's settings.

File

includes/content.admin.inc, line 1379
Administrative interface for content type creation.

Code

function content_alter_db_analyze($previous_field, $new_field) {
  $dropped = array();

  // There is no loss of data if there was no previous data.
  if (empty($previous_field)) {
    return $dropped;
  }

  // Analyze possible data loss from changes in storage type.
  if (!empty($previous_field) && !empty($new_field)) {

    // Changing from multiple to not multiple data, will cause loss of all
    // values greater than zero.
    if ($previous_field['db_storage'] == CONTENT_DB_STORAGE_PER_FIELD && $new_field['db_storage'] == CONTENT_DB_STORAGE_PER_CONTENT_TYPE) {
      $dropped['delta'] = 0;
    }
    elseif (isset($previous_field['multiple']) && isset($new_field['multiple'])) {
      if ($previous_field['multiple'] > $new_field['multiple'] && $new_field['multiple'] > 1) {
        $dropped['delta'] = $new_field['multiple'];
      }
    }
  }

  // Analyze possible data loss from changes in field columns.
  $previous_schema = !empty($previous_field) ? content_table_schema($previous_field) : array(
    'fields' => array(),
  );
  $new_schema = !empty($new_field) ? content_table_schema($new_field) : array(
    'fields' => array(),
  );
  $dropped_columns = array_diff(array_keys($previous_schema['fields']), array_keys($new_schema['fields']));
  if ($dropped_columns) {
    $dropped['columns'] = $dropped_columns;
  }

  //  if (empty($new_schema['fields'])) {
  //    // No new columns, will lose all columns for a field.
  //    foreach ($previous_schema['fields'] as $column => $attributes) {
  //      $dropped['columns'][] = $column;
  //    }
  //  }
  //  else {
  //    // Check both old and new columns to see if we are deleting some columns for a field.
  //    foreach ($previous_schema['fields'] as $column => $attributes) {
  //      if (!isset($new_schema['fields'][$column])) {
  //        $dropped['columns'][] = $column;
  //      }
  //    }
  //  }
  return $dropped;
}