annotate core/scripts/dump-database-d6.sh @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 #!/usr/bin/env php
Chris@0 2 <?php
Chris@0 3
Chris@0 4 /**
Chris@0 5 * Dump a Drupal 6 database into a Drupal 7 PHP script to test the upgrade
Chris@0 6 * process.
Chris@0 7 *
Chris@0 8 * Run this script at the root of an existing Drupal 6 installation.
Chris@0 9 *
Chris@0 10 * The output of this script is a PHP script that can be ran inside Drupal 7
Chris@0 11 * and recreates the Drupal 6 database as dumped. Transient data from cache
Chris@0 12 * session and watchdog tables are not recorded.
Chris@0 13 */
Chris@0 14
Chris@0 15 // Define default settings.
Chris@0 16 $cmd = 'index.php';
Chris@0 17 $_SERVER['HTTP_HOST'] = 'default';
Chris@0 18 $_SERVER['PHP_SELF'] = '/index.php';
Chris@0 19 $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
Chris@0 20 $_SERVER['SERVER_SOFTWARE'] = NULL;
Chris@0 21 $_SERVER['REQUEST_METHOD'] = 'GET';
Chris@0 22 $_SERVER['QUERY_STRING'] = '';
Chris@0 23 $_SERVER['PHP_SELF'] = $_SERVER['REQUEST_URI'] = '/';
Chris@0 24 $_SERVER['HTTP_USER_AGENT'] = 'console';
Chris@0 25
Chris@0 26 // Bootstrap Drupal.
Chris@0 27 include_once './includes/bootstrap.inc';
Chris@0 28 drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
Chris@0 29
Chris@0 30 // Include the utility drupal_var_export() function.
Chris@0 31 include_once __DIR__ . '/../includes/utility.inc';
Chris@0 32
Chris@0 33 // Output the PHP header.
Chris@0 34 $output = <<<ENDOFHEADER
Chris@0 35 <?php
Chris@0 36
Chris@0 37 /**
Chris@0 38 * @file
Chris@0 39 * Filled installation of Drupal 6.17, for test purposes.
Chris@0 40 *
Chris@0 41 * This file was generated by the dump-database-d6.sh tool, from an
Chris@0 42 * installation of Drupal 6, filled with data using the generate-d6-content.sh
Chris@0 43 * tool. It has the following modules installed:
Chris@0 44
Chris@0 45 ENDOFHEADER;
Chris@0 46
Chris@0 47 foreach (\Drupal::moduleHandler()->getModuleList() as $module => $filename) {
Chris@0 48 $output .= " * - $module\n";
Chris@0 49 }
Chris@0 50 $output .= " */\n\n";
Chris@0 51
Chris@0 52 // Get the current schema, order it by table name.
Chris@0 53 $schema = drupal_get_schema();
Chris@0 54 ksort($schema);
Chris@0 55
Chris@0 56 // Override the field type of the filename primary key to bypass the
Chris@0 57 // InnoDB 191 character limitation.
Chris@0 58 if (isset($schema['system']['primary key']) && $schema['system']['primary key'] == 'filename' && isset($schema['system']['fields']['filename']['type']) && $schema['system']['fields']['filename']['type'] == 'varchar') {
Chris@0 59 $schema['system']['fields']['filename']['type'] = 'varchar_ascii';
Chris@0 60 }
Chris@0 61 // Export all the tables in the schema.
Chris@0 62 foreach ($schema as $table => $data) {
Chris@0 63 // Remove descriptions to save time and code.
Chris@0 64 unset($data['description']);
Chris@0 65 foreach ($data['fields'] as &$field) {
Chris@0 66 unset($field['description']);
Chris@0 67 }
Chris@0 68
Chris@0 69 // Dump the table structure.
Chris@0 70 $output .= "db_create_table('" . $table . "', " . drupal_var_export($data) . ");\n";
Chris@0 71
Chris@0 72 // Don't output values for those tables.
Chris@0 73 if (substr($table, 0, 5) == 'cache' || $table == 'sessions' || $table == 'watchdog') {
Chris@0 74 $output .= "\n";
Chris@0 75 continue;
Chris@0 76 }
Chris@0 77
Chris@0 78 // Prepare the export of values.
Chris@0 79 $result = db_query('SELECT * FROM {'. $table .'}');
Chris@0 80 $insert = '';
Chris@0 81 while ($record = db_fetch_array($result)) {
Chris@0 82 // users.uid is a serial and inserting 0 into a serial can break MySQL.
Chris@0 83 // So record uid + 1 instead of uid for every uid and once all records
Chris@0 84 // are in place, fix them up.
Chris@0 85 if ($table == 'users') {
Chris@0 86 $record['uid']++;
Chris@0 87 }
Chris@0 88 $insert .= '->values('. drupal_var_export($record) .")\n";
Chris@0 89 }
Chris@0 90
Chris@0 91 // Dump the values if there are some.
Chris@0 92 if ($insert) {
Chris@0 93 $output .= "db_insert('". $table . "')->fields(". drupal_var_export(array_keys($data['fields'])) .")\n";
Chris@0 94 $output .= $insert;
Chris@0 95 $output .= "->execute();\n";
Chris@0 96 }
Chris@0 97
Chris@0 98 // Add the statement fixing the serial in the user table.
Chris@0 99 if ($table == 'users') {
Chris@0 100 $output .= "db_query('UPDATE {users} SET uid = uid - 1');\n";
Chris@0 101 }
Chris@0 102
Chris@0 103 $output .= "\n";
Chris@0 104 }
Chris@0 105
Chris@0 106 print $output;