Importing Users from one Drupal site to another Drupal site seems like it should be a lot easer than it is. After all, a basic drupal user account is really just a row in the `users` table, and you could theoretically just copy that row from the old database to the new one.
Well, on our site, we had implemented some actions that take place when a new user registers. URL aliases got created for the user account page, as well as a separate alias for the user edit page. Roles were automatically assigned (roles that did not exist on the old site). In short, we needed everything to happen on the new site just as if that user had just registered.
So we looped through each user row in the old site's database, and called the Drupal user_save() function on it to create the new users. This atomatically triggered every event that was supposed to take place when a new user registered. There was just one hitch... all the users were assigned new user id's (UID) when they were saved!
User ID's had to be kept from the old site to the new one. Not only does it make importing comments and nodes much easier, but it can be important for SEO and bookmarks. If your user knows his profile is at http://mysite.com/user/50, you'd like that link to still work on the new site. So how did we manage to create brand new users, but keep the user id the same?
We fool Drupal's sequences table into automatically assigning the same uid to the new user. Just before the call to user_save(), we execute this query:
db_query("UPDATE sequences SET id = %d WHERE name = 'users_uid'", $old_user_uid -1 );
now when the save is executed, the uid remains the same across both sites, all the actions are applied to new users, and nobody has to explain to a userbase why all their account links are now different.




Latest Comments