Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trying to migrate 0.8.x ➡️ 1.0.x #208

Closed
galvakojis opened this issue May 27, 2020 · 12 comments
Closed

trying to migrate 0.8.x ➡️ 1.0.x #208

galvakojis opened this issue May 27, 2020 · 12 comments
Labels
documentation Improvements or additions to documentation question Further information is requested

Comments

@galvakojis
Copy link

galvakojis commented May 27, 2020

I am trying to migrate from old version but suggested script for MongoDB

db.getCollection('users').find().forEach(function(user) {
  var uuid = UUID(user.id);
  db.getCollection('users').update({_id: user._id}, [{$set: {id: uuid}}]);
});

gives an error

[thread1] Error: Invalid UUID string: UUID("49caa9b2-a1f1-4459-bc4c-7e5a5d7d88e1") :

so later i created new account and clearly see, that something have changed
image

Update
I managed to update user in DB(?) well I think so, but nothing have changed(still same as showed in first picture)
image
using admin user (which one was created in old version), I can't login, using a new one I can

@galvakojis
Copy link
Author

galvakojis commented May 27, 2020

found the issue
instead of this:
var uuid = UUID(user.id);
i needed to use this:
var uuid = BinData(3, user.id.base64());
and now I again can login to my old user
Maybe you can fix a documentation?

@frankie567
Copy link
Member

It works on my MongoDB version. Which MongoDB version are you using?

@frankie567 frankie567 added question Further information is requested documentation Improvements or additions to documentation labels May 27, 2020
@galvakojis
Copy link
Author

galvakojis commented May 27, 2020

mongo:4.2.3

full example:

db.getCollection('users').find().forEach(function(user) {  
    var uuid = BinData(3, user.id.base64()); 
    db.getCollection('users').update({nick_name: user.nick_name}, [{$set: {id: uuid}}]); 
});

now both are in correct format
image

yeah, also tried to write some usable migration file, but until this moment no success :/
i am using this one
https://github.com/seppevs/migrate-mongo#readme
if you have some ideas, please help me

@frankie567
Copy link
Member

Ok, I get it. Your MongoDB connection is not set to use the new UUID standard, it's using the legacy format (which is still the default). I explain it here in the doc : https://frankie567.github.io/fastapi-users/configuration/databases/mongodb/#setup-database-connection-and-collection

But this is something I definitely should add in the migration documentation. I'll do this.

To solve your problem:

  • If you still have a backup of you DB with string-ID, you should apply the script I provide.
  • If not, you should convert legacy-UUID to standard-UUID. Something like:
db.getCollection('users').find().forEach(function(user) {  
    var uuid = new BinData(4, user.id.base64()); // Notice the 4 instead of 3
    db.getCollection('users').update({_id: user._id}, [{$set: {id: uuid}}]); 
});

I would also recommend that you try Robot 3T as it clearly tells if a field is a UUID or Legacy UUID.

@galvakojis
Copy link
Author

galvakojis commented May 28, 2020

thank you, frankie.
Here I will try to write my steps:
used old backup file to restore DB.
added that missing line from here uuidRepresentation="standard"
https://frankie567.github.io/fastapi-users/configuration/databases/mongodb/#setup-database-connection-and-collection
Well now it is working as before. I can loggin to user again
image

@frankie567
Copy link
Member

Which editor are you using to execute your queries?

@galvakojis
Copy link
Author

galvakojis commented May 28, 2020

All work fine!
Mongo shell.
I will give a try to use Robot 3T

@frankie567
Copy link
Member

Oh, okay, sorry I misread your answer 😃 Glad we worked it out !

Cheers!

@galvakojis
Copy link
Author

Thank you once again, if you find how to same thing in python (not in mongo shell), please share it

@frankie567
Copy link
Member

Sure! Should be something like:

import uuid

import motor.motor_asyncio


async def migrate_uuid():
    client = motor.motor_asyncio.AsyncIOMotorClient(
        DATABASE_URL, uuidRepresentation="standard"
    )
    db = client["database_name"]
    users = db["users"]

    async for user in users.find({}):
        await users.update_one(
            {"_id": user["_id"]},
            {"$set": {"id": uuid.UUID(user["id"])}},
        )

@frankie567
Copy link
Member

I've added this information in the migration documentation. Thank you for raising this point 😃

@galvakojis
Copy link
Author

Hello frankie

Sure! Should be something like:

import uuid

import motor.motor_asyncio


async def migrate_uuid():
    client = motor.motor_asyncio.AsyncIOMotorClient(
        DATABASE_URL, uuidRepresentation="standard"
    )
    db = client["database_name"]
    users = db["users"]

    async for user in users.find({}):
        await users.update_one(
            {"_id": user["_id"]},
            {"$set": {"id": uuid.UUID(user["id"])}},
        )

it returns legacy UUID, not a standard one

JeffreyThijs added a commit to JeffreyThijs/fastapi-users that referenced this issue Nov 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants