Merge Content and Structure Across DBs

Specs

Version
Drupal 7
Tools
Batch Processes
Field API
Created
19 Mär 2016

Summary

Im Idealfall können Sie die Eigenschaften Modul verwenden Struktur zu migrieren, wie CCK Felder, Menüpunkte , Taxonomie Vokabulare oder Systemvariablen , um die Produktionsdatenbank .

Aber wenn Sie auf Umstände setzen in sind, wo dies nicht möglich ist, wird dieser Ausschnitt helfen Ihnen, Ihre Inhalte aus Ihrer Produktion Datenbank migrieren, können Sie Ihre Entwicklungsumgebung . Sie würden dann Ihre Entwicklung Datenbank-Dump und es in die Produktion zu importieren.

Es tut dies ein Drush Befehl in den folgenden Schritten:

  1. Ändern Sie die $ Datenbanken Variable in settings.php für mehrere Datenbanken auf Ihrer Drupal-Website zu ermöglichen. Dann nutzen Sie db_set_active ('db_name') hin und her zu wechseln
  2. Schleife durch eine Reihe von Datenbanken-Tabellen, die Inhalte zu speichern, Taxonomie-Begriffe, webform Einreichungen und verwaltete Dateien
  3. jede dieser Tabellen in der Entwicklung Kürzen
  4. Unter Verwendung eines MySQL-Befehl, verschieben Sie alle Daten aus den Produktions Tabellen in Ihrem Entwicklungstabellen
Drush Command Example
drush --user=1 example-migration

Code

1. settings.php modification

settings.php
MODULE_PATH/sites/default
Modify the $databases variable in your settings.php to allow for multiple databases. You can then use db_set_active('default') and db_set_active('production') to switch back and forth.
          
$databases = array(
  'default' => array(
    'default' => array(
      'database' => 'dev_db_name',
      'username' => 'dev_db_username',
      'password' => 'dev_db_password',
      'host' => 'localhost',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),
  'production' => array(
    'default' => array(
      'database' => 'prod_db_name',
      'username' => 'prod_db_username',
      'password' => 'prod_db_password',
      'host' => 'localhost',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),
);
          
          

2. Drush Command

MODULE_NAME.drush.inc
Add this to one of your modules by putting it in a file called MODULE_NAME.drush.inc. It will be auto discovered by Drush.
          
function MODULE_NAME_drush_command() {

  $items = array();

  $items['example-migration'] = array(
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
  );

  return $items;
}

function drush_MODULE_NAME_example_migration() {

  $tables = array();

  // node
  $tables[] = 'node';
  $tables[] = 'node_access';
  $tables[] = 'node_comment_statistics';
  $tables[] = 'node_counter';
  $tables[] = 'node_revision';

  db_set_active('production'); // use the key from your $databases array in settings.php

  $schema = drupal_get_schema(NULL,TRUE);
  foreach ($schema as $table_name => $data) {
    if ((strpos($table_name, 'ield_data_') !== false) || (strpos($table_name, 'ield_revision_') !== false)) {
      $tables[] = $table_name;
    }
  }

  db_set_active('default');

  // taxonomy
  $tables[] = 'taxonomy_index';
  $tables[] = 'taxonomy_term_data';
  $tables[] = 'taxonomy_term_hierarchy';
  $tables[] = 'taxonomy_menu';

  // webforms
  $tables[] = 'webform';
  $tables[] = 'webform_submissions';
  $tables[] = 'webform_submitted_data';

  // other tables
  $tables[] = 'url_alias';
  $tables[] = 'field_collection_item';
  $tables[] = 'field_collection_revision';
  $tables[] = 'file_managed';
  $tables[] = 'file_usage';

  $batch = array(
    'operations' => array(),
    'title' => t('Processing Batch'),
    'init_message' => t('Process is starting.'),
    'progress_message' => t('Processed @current out of @total.'),
    'error_message' => t('Process has encountered an error.'),
    'file' => drupal_get_path('theme', 'MODULE_NAME') . '/MODULE_NAME.drush.inc',
    'finished' => 'MODULE_NAME_example_migration_batch_finished'
  );

  $start_time = time();

  $batch['progressive'] = FALSE;

  $batch['operations'][] = array('MODULE_NAME_example_migration_content_batch', array($tables, $start_time));

  drush_log(t('Batch process is starting !time', array('!time' => format_date($start_time, 'long'))), 'success');

  batch_set($batch);
  drush_backend_batch_process();

}

function MODULE_NAME_example_migration_content_batch($tables, $start_time, &$context) {

  set_time_limit(9999999);

  foreach ($tables as $key => $table_name) {
    if (db_table_exists($table_name)) {
      $results = db_query("TRUNCATE development_database_name." . $table_name . ";");
      $results = db_query("INSERT INTO development_database_name." . $table_name . " SELECT * FROM production_database_name." . $table_name . ";");
    }
    else {
      drush_log(t('Dev database is missing table !table_name but it exists in production.', array('!table_name' => $table_name)), 'warning');
    }
  }

  $context['results']['start_time'] = $start_time;

}

function MODULE_NAME_example_migration_batch_finished($success, $results, $operations) {
  if ($success) {

    $start_time = $results['start_time'];

    $seconds = time() - $start_time;
    $hours = floor($seconds / 3600);
    $mins = floor(($seconds - ($hours*3600)) / 60);
    $secs = floor($seconds % 60);

    drush_log(t('Job completed in: !hours hours !mins minutes !secs seconds', array('!hours' => $hours, '!mins' => $mins, '!secs' => $secs)), 'success');
  }
  else {
    $error_operation = reset($operations);
    $message = t('An error occurred while processing %error_operation with arguments: @arguments', array('%error_operation' => $error_operation[0], '@arguments' => print_r($error_operation[1], TRUE)));
    drush_log($message, 'error');
  }
}
          
          

Comments

Placeholder: I'll extend node comments with ajax and other functionality here.