Last active
October 12, 2023 00:31
-
-
Save davidbarratt/0b662a2726e0a1738f8856027973741b to your computer and use it in GitHub Desktop.
Migrate Drupal SQLite to MySQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Drupal\Core\Database\Database; | |
$sqlite = Database::getConnection('default'); | |
$mysql = Database::getConnection('mysql'); | |
$schema = $sqlite->query("SELECT name FROM sqlite_schema WHERE type ='table' AND name NOT LIKE 'sqlite_%'")->fetchCol(); | |
$tables = [ | |
'key_value_expire', | |
'old_5deae5_url_alias', | |
'old_274b3ataxonomy_term_data', | |
'old_274b3ataxonomy_term_field_data', | |
'old_274b3ataxonomy_term__parent', | |
'migrate_map_my_wordpress_tags', | |
'migrate_map_my_wordpress_content_post', | |
'migrate_map_my_wordpress_content_page', | |
'migrate_map_my_wordpress_comment_post', | |
'config_import', | |
'config_export', | |
'cache_discovery', | |
'cache_bootstrap', | |
'cachetags', | |
'cache_config', | |
'cache_default', | |
'cache_entity', | |
'cache_container', | |
'cache_data', | |
'cache_render', | |
'cache_menu', | |
'cache_dynamic_page_cache', | |
'cache_page', | |
]; | |
$skip = array_combine($tables, $tables); | |
/** | |
* MySQL will autoincrement a zero value which breaks the user migration. | |
* | |
* @see {@link https://github.com/drupal/drupal/blob/6efb70cd568335e9b292cde6530cc21c2931c448/core/modules/user/src/UserStorage.php#L29-L30 UserStorage.php} | |
*/ | |
$sql_mode = $mysql->query("SELECT @@sql_mode;")->fetchField(); | |
$mysql->query("SET sql_mode = '$sql_mode,NO_AUTO_VALUE_ON_ZERO'"); | |
foreach ($schema as $table) { | |
if (array_key_exists($table, $skip)) { | |
print "Skipping {$table}\n"; | |
continue; | |
} | |
$rows = $sqlite->query("SELECT * FROM {$table}"); | |
print "Migrating {$table}\n"; | |
foreach ($rows as $row) { | |
$insert = $mysql->insert($table); | |
$insert->fields((array) $row); | |
$insert->execute(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment