Fix #208: add migrations information for MongoDB users

This commit is contained in:
François Voron
2020-05-29 07:50:43 +02:00
parent 3fab5e5bbb
commit e850871e79

View File

@ -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.