diff --git a/docs/migration/08_to_10.md b/docs/migration/08_to_10.md index cdc743cd..7561a4cb 100644 --- a/docs/migration/08_to_10.md +++ b/docs/migration/08_to_10.md @@ -32,6 +32,20 @@ class Model(BaseModel): user_id: UUID4 ``` +#### MongoDB + +To avoid any issues, it's recommended to use the `standard` UUID representation when instantiating the MongoDB client: + +```py +DATABASE_URL = "mongodb://localhost:27017" +client = motor.motor_asyncio.AsyncIOMotorClient( + DATABASE_URL, uuidRepresentation="standard" +) +``` + +This parameter controls how the UUID values will be encoded in the database. By default, it's set to `pythonLegacy` but new applications should consider setting this to `standard` for cross language compatibility. [Read more about this](https://pymongo.readthedocs.io/en/stable/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient). + + ### In database Id. were before stored as strings in the database. You should make a migration to convert string data to UUID data. @@ -72,6 +86,8 @@ ALTER TABLE "user" MODIFY id CHAR(36); #### MongoDB +##### Mongo shell + For MongoDB, we can use a `forEach` iterator to convert the id. for each document: ```js @@ -81,6 +97,28 @@ db.getCollection('users').find().forEach(function(user) { }); ``` +##### Python + +```py +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"])}}, + ) +``` + ## Splitted routers You now have the responsibility to **wire the routers**. FastAPI Users doesn't give a bloated users router anymore.