sync user.id and drupal_user.uid
Notes:
@btbonval: Looks like the out of sync is only effecting the first 50 or so users (only 46 are effected and all have rusers.id under 50.
@jywarren: I think the 50 in question were those who signed up for the beta plots2 site before we had syncing set up. This may indeed be messy, but basically I think we have to do something where we "make space" for them to get the correct ruser.id, and if we can't figure it out, just use one high enough to have an acceptable uid and id.
@btbonval: Assigning new uids sounds like a good compromise, but unfortunately the id/uid is an extremely integral number in the database. Changing it can be very difficult to keep straight. At any rate, I think only drupal_user.uid is used across the board. We could probably replace user.id without any far reaching consequences.
@btbonval: We'll have to very carefully, and by hand, reassign those ids not only in the User table but across all tables the user could have potentially touched. Since there are no foreign keys in the database, the only way to know all places a user'd id can appear is to review all the models and find which ones reference DrupalUser, I think.
@btbonval: primary keys aren't meant to be changed in a relational database, ever :(
@btbonval: You know what, it might be worth deleting the User entries for misaligned users in Rails console. The console supports cascade, so we should see if we can delete only when there is nothing to cascade. As long as there is nothing to cascade, we can delete the User entry and then generate a new User via a migration. Since DrupalUser.uid is authoritative, we would want to remove any User where User.id does not correspond to the DrupalUser.uid. For those cases where a cascade is needed ... well let's just hope that never comes up :(
@jywarren: We can probably list which tables depend on uid:
- DrupalNode
- DrupalNodeRevision
- DrupalNodeCommunityTag
- TagSelection
- DrupalComment
- DrupalFile
- Image
- DrupalProfileValue
So if we pick User.id to be de facto standard, we could change foreign keys to start pointing at User.id instead of DrupalUser.uid. That isn't changing primary key values at all.
Once DrupalUser.uid is no longer used by any other table, we could fully deprecate it. We'd migrate any unique information DrupalUser still maintains into User and update any controller code to make full use of User.
This is a fairly large project. The data migration would require a lot of care and a number of steps. The list of tables currently using uid is a good start, but we'll also want to review the code to see where it has hard coded calls to uid.
How about adding offset to the primary keys of either of the tables?
UPDATE parent_tbl SET primary_key_col = primary_key_col + 10000;
UPDATE child_tbl SET foreign_key_col = foreign_key_col + 10000;
Is this probably going to break anything?
Bryan is right that this is a big project that has to be done carefully, but I believe we can do it by not ever changing DrupalUsers.uid -- instead changing User.id which is not depended on for anything currently.
Only the first ~50 entries in User have User.id not matching DrupalUsers.uid. I'm running a query on production now to find out how many, and exactly what they are.
Note: this was heavily revised but it works now; this is for just the first 100 Users, by User.id:
mismatches = []
DrupalUsers.joins("INNER JOIN rusers ON rusers.username = users.name").select('users.uid, users.status, users.name, rusers.id AS rid, rusers.username').where(status: 1).order('rid').limit(100).each do |user|
mismatches << [user.name, user.uid, user.rid] if user.username && user.uid != user.rid
end
It returns 45 users, whether run on the first 100 or 10,000, which is consistent with the idea that it's only the first few Users (format is username, uid, id):
["mathew", 4, 3],
["gonzoearth", 12, 4],
["liz", 7, 5],
["Shannon", 9, 6],
["Adam-Griffith", 3, 7],
["Becki", 46673, 8],
["cfastie", 554, 9],
["ives", 403, 11],
["nadya", 704, 12],
["hagitkeysar", 152, 13],
["patcoyle", 77, 14],
["btbonval", 43229, 15],
["cbreuer", 49682, 16],
["shaief", 151, 17],
["eustatic", 379, 18],
["donblair", 43651, 19],
["itdaniher", 46964, 20],
["Sara", 8, 21],
["RichardCWinkel", 51998, 22],
["stoft", 54025, 23],
["Larzag", 1225, 24],
["xxup", 57263, 25],
["straylight", 43047, 26],
["ebrelsford", 1057, 27],
["LimnoRR", 45309, 28],
["Timoti", 58354, 29],
["danbeavers", 51, 30],
["oscarbrett", 409, 31],
["dusjagr", 46795, 32],
["j0hn", 57430, 33],
["Ioan", 44551, 34],
["jmsaavedra", 46929, 35],
["dorncox", 1205, 37],
["simone", 164, 38],
["james1be2", 56693, 39],
["osbock", 44836, 40],
["bdebaque", 57052, 41],
["veroacosta", 45662, 42],
["rjm", 163, 43],
["brad", 59609, 45],
["virdi", 60478, 46],
["Cindy_ExCites", 1083, 47],
["geobduffy", 60509, 48],
["cversek", 48275, 49],
["Jessica_Helm", 60514, 50]
This is all the DrupalUser.uids:
[4, 12, 7, 9, 3, 46673, 554, 403, 704, 152, 77, 43229, 49682, 151, 379, 43651, 46964, 8, 51998, 54025, 1225, 57263, 43047, 1057, 45309, 58354, 51, 409, 46795, 57430, 44551, 46929, 1205, 164, 56693, 44836, 57052, 45662, 163, 59609, 60478, 1083, 60509, 48275, 60514]
And this is all the User.ids:
[3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 38, 39, 40, 41, 42, 43, 45, 46, 47, 48, 49, 50]
It's spaghetti. But I think that we could manually fix it with a list so short. If we can find who can have their User.id synced without colliding with someone else, we can go one by one.
I like Jitesh's idea of adding a large number. My plan now:
- add 400,000 to each
User.idin the 45 in question - go through each one and re-assign each a
User.idto match theirDrupalUsers.nid.
This'll work because the low-numbered User.ids will now be free.
I'd like to test this with a few users, and manually confirm that all is well before attempting for all 45. I'm thinking:
["cfastie", 554, 9], ["hagitkeysar", 152, 13], ["patcoyle", 77, 14], ["btbonval", 43229, 15],
That is, just increasing the User.id by 400,000, and seeing if that breaks anything.
OK, @btbonval - i changed your User.id record to 900015 instead of 15 (ended up having to use more than 400,000). I also had to change your User.openid_identifier to http://old.publiclab.org/user/900015/identity. It saved OK; can you try it out?
I think the only code used is in /app/models/user_selection.rb, which I'm not sure is used at all?
Hi, @Fastie - would you be willing to let me change your user id as I did for Bryan and you could tell me if anything goes wrong? We're hoping to resolve a major issue in the database leftover from the Drupal days. Any issues would be recoverable.
Sure, good luck.
OK, done. One thing that may not work immediately is a fresh SpectralWorkbench/MapKnitter login. I'd say don't log out, and try logging in in an incognito window to check. Thanks, Chris! I can revert tomorrow morning if it doesn't work out.
I was able to log into publiclab, mapknitter and, spectralworkbench from Chrome incognito windows without incident.
Great. Any other weirdnesses on the publiclab.org site? If not, that's great and we can probably resolve this issue pretty easily.
I didn't notice any unusual behavior at publiclab. I didn't do much except navigate around, start to edit a comment, and change my password.
OK, I'm going to ask a few other people. @ebarry @patcoyle @shaief - could we do the same test on your accounts?
Would you be willing to let me change your user id as I did for Bryan and Chris and you could tell me if anything goes wrong? We're hoping to resolve a major issue in the database leftover from the Drupal days. Any issues would be recoverable, and we haven't had issues so far.
@jywarren - you are more than welcome to test things using my account. Good luck and thanks!
Sure Jeff,
Test as needed with my account.
Thanks, Pat
Sent from my iPhone
On Mar 25, 2016, at 8:09 AM, Jeffrey Warren [email protected] wrote:
OK, I'm going to ask a few other people. @ebarry @patcoyle @shaief - could we do the same test on your accounts?
Would you be willing to let me change your user id as I did for Bryan and Chris and you could tell me if anything goes wrong? We're hoping to resolve a major issue in the database leftover from the Drupal days. Any issues would be recoverable, and we haven't had issues so far.
— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub
Thanks! @shaief and @patcoyle, I just changed yours. So please keep an eye out for issues.
On Fri, Mar 25, 2016 at 12:49 PM, patcoyle [email protected] wrote:
Sure Jeff,
Test as needed with my account.
Thanks, Pat
Sent from my iPhone
On Mar 25, 2016, at 8:09 AM, Jeffrey Warren [email protected] wrote:
OK, I'm going to ask a few other people. @ebarry @patcoyle @shaief - could we do the same test on your accounts?
Would you be willing to let me change your user id as I did for Bryan and Chris and you could tell me if anything goes wrong? We're hoping to resolve a major issue in the database leftover from the Drupal days. Any issues would be recoverable, and we haven't had issues so far.
— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub
— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/publiclab/plots2/issues/227#issuecomment-201359645
Unless @shaief, @patcoyle, @btbonval or @cfastie have noticed any errors on the site that could be related to this, I think we can go ahead and do this process for all 45 users, and close this issue up at last!
Thank Jeff,
I haven't been doing much, but didn't notice any issues.
Pat
On Fri, Apr 8, 2016 at 4:22 PM, Jeffrey Warren [email protected] wrote:
Unless @shaief https://github.com/shaief, @patcoyle https://github.com/patcoyle, @btbonval https://github.com/btbonval or @cfastie have noticed any errors on the site that could be related to this, I think we can go ahead and do this process for all 45 users, and close this issue up at last!
— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/publiclab/plots2/issues/227#issuecomment-207644573
The only issue I have noticed recently is this one about the preview button not working on new comments on research notes. #481