You are here

function date_api_update_7000 in Date 7.2

Same name and namespace in other branches
  1. 7.3 date_api/date_api.install \date_api_update_7000()

Move old date format to new date format tables,and delete the old tables.

Insert only values that don't already exist in the new tables, in case new version of those custom values have already been created.

File

date_api/date_api.install, line 126
Install, update and uninstall functions for the date_api module.

Code

function date_api_update_7000() {

  // Move format data from the old 'date_format_types' table to the new
  // 'date_format_type' table.
  if (db_table_exists('date_format_types')) {

    // Find all the custom entries in the D6 table.
    $result = db_select('date_format_types', 'old_formats')
      ->fields('old_formats', array(
      'type',
      'title',
      'locked',
    ))
      ->condition('locked', 0)
      ->execute()
      ->fetchAll(PDO::FETCH_ASSOC);

    // Iterate over all the old values.
    foreach ($result as $row) {

      // See if this value already exists in the new table
      // (it might have been added manually before this update got run).
      $count = db_select('date_format_type', 'new_formats')
        ->condition('type', $row['type'])
        ->countQuery()
        ->execute()
        ->fetchField();

      // If the value is missing, insert it.
      // Do nothing if it already exists, assume the value in the
      // new table trumps the old values.
      if (empty($count)) {
        db_insert('date_format_type')
          ->fields(array(
          'type' => $row['type'],
          'title' => $row['title'],
          'locked' => $row['locked'],
        ))
          ->execute();
      }
    }

    // Drop the old table.
    db_drop_table('date_format_types');
  }

  // Move format data from the old 'date_formats' table (which was renamed to
  // 'd6_date_formats') to the new 'date_formats' table.
  if (db_table_exists('d6_date_formats')) {

    // Find all the custom entries in the D6 table.
    $result = db_select('d6_date_formats', 'old_formats')
      ->fields('old_formats', array(
      'format',
      'type',
      'locked',
    ))
      ->condition('type', 'custom')
      ->execute()
      ->fetchAll(PDO::FETCH_ASSOC);

    // Iterate over all the old values.
    foreach ($result as $row) {

      // See if this value already exists in the new table (it might have been
      // added manually before this update got run).
      $count = db_select('date_formats', 'new_formats')
        ->condition('format', $row['format'])
        ->condition('type', $row['type'])
        ->countQuery()
        ->execute()
        ->fetchField();

      // If the value is missing, insert it. Do nothing if it already exists,
      // assume the value in the new table trumps the old values.
      if (empty($count)) {
        db_insert('date_formats')
          ->fields(array(
          'format' => $row['format'],
          'type' => $row['type'],
          'locked' => $row['locked'],
        ))
          ->execute();
      }
    }

    // Drop the old table.
    db_drop_table('d6_date_formats');
  }

  // Move format data from the old 'date_format_locale' table (which was renamed
  // to 'd6_date_format_locale') to the new 'date_format_locale' table.
  if (db_table_exists('d6_date_format_locale')) {

    // Find all the custom entries in the D6 table.
    $result = db_select('d6_date_format_locale', 'old_formats')
      ->fields('old_formats', array(
      'format',
      'type',
      'language',
    ))
      ->condition('type', 'custom')
      ->execute()
      ->fetchAll(PDO::FETCH_ASSOC);

    // Iterate over all the old values.
    foreach ($result as $row) {

      // See if this value already exists in the new table (it might have been
      // added manually before this update got run).
      $count = db_select('date_format_locale', 'new_formats')
        ->condition('format', $row['format'])
        ->condition('type', $row['type'])
        ->condition('language', $row['language'])
        ->countQuery()
        ->execute()
        ->fetchField();

      // If the value is missing, insert it.
      // Do nothing if it already exists, assume the value in the
      // new table trumps the old values.
      if (empty($count)) {
        db_insert('date_format_locale')
          ->fields(array(
          'format' => $row['format'],
          'type' => $row['type'],
          'language' => $row['language'],
        ))
          ->execute();
      }
    }

    // Drop the old table.
    db_drop_table('d6_date_format_locale');
  }
}