Merging two Drupal instances into a single database
I recently had to transfer some tables from a Drupal site that had been developed in it's own database, into an existing Drupal database. In order to do this, the MySQL dump needed to be edited to add table prefixes, lest the existing Drupal install be wiped out.
Editing this by hand was out of the question, but thanks to the emacs replace-regexp command, I was able to change the entire dump file in a matter of minutes.
To initiate the command, one hits ALT-x and types in replace-regexp. First, enter the pattern to find. To add a prefix to all the DROP TABLE commands:
DROP TABLE IF EXISTS `\(.*\)`
The \(.*\) is the regular expression that tells emacs to match everything inside those parenthesis.
After entering the search, one is prompted to enter the replacement pattern. To add a prefix, for example myprefix to the DROP TABLE commands I used this pattern:
DROP TABLE IF EXISTS `myprefix_\1`
The \1 pulls in whatever was matched in the first set of parenthesis in our search pattern.
I repeated a similar search and replace for the other commands that reference a table in a typical Drupal database:
CREATE TABLE `\(.*\)` CREATE TABLE `myprefix_\1` LOCK TABLES `\(.*\)` LOCK TABLES `myprefix_\1` ALTER TABLE `\(.*\)` ALTER TABLE `myprefix_\1` INSERT INTO `\(.*\)` INSERT INTO `myprefix_\1`
After that I was able to import this dump file into the existing Drupal database (without wiping out the other installation) and continue development as a multi-site sharing a single database.

Comments
adjust the sequences table
I did the same some time ago - and wondered why new node ids (andsome other counters) started at 1 again. So I found out that there is a table '{sequences}' in your drupal db. All the sequences had the $db_prefix in their name. After renaming the entries of both '{sequences}' tables everything worked fine.
See http://niebegeg.net/blog/changing-of-the-dbprefix-of-the-drupal-database
Greetings,
Dirk
Is it possible to merge two
Is it possible to merge two drupal instances into one database but without prefixes (creating one site from content of two other sites)?
Merging two Drupal instances
Merging two Drupal instances that already exist (complete with different users and nodes) into a single instance (without prefixes) would be quite difficult via this mysql dump altering method. The reason for this is that each instance would have conflicting primary keys (user ids and node ids for example) that would need to be sorted out.
reply
I'm not much into database architecture and my Drupal knowledge is currently limited to theme development but if what Junphine was possible, that would be awesome.
Post new comment