Simple musings of a Drupal developer.

Drupal User Import

By krs · December 17, 2008 · 0 Comments · 665 Views

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.

Filed in:
Tagged with: , , ,

Post New Comment

If you are already an OnSugar member, or would like to receive email alerts as new comments are made, please login or register for OnSugar. Or connect with your Facebook account: .
The content of this field is kept private and will not be shown publicly.

About Me

Drupal.org username: kscheirer

subscribe

Drupal Cred

Drupal Camp Los Angeles 2009 - August 8-9th

Archive

November 2009
SMTWTFS
1234567
891011121314
15161718192021
22232425262728
2930 
August 2009
SMTWTFS
 1
2345678
9101112131415
16171819202122
23242526272829
3031 
July 2009
SMTWTFS
 1234
567891011
12131415161718
19202122232425
262728293031