dump-database-d6.sh 3.3 KB

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