Skip to content

Commit

Permalink
Fix #208: add migrations information for MongoDB users
Browse files Browse the repository at this point in the history
  • Loading branch information
frankie567 committed May 29, 2020
1 parent 3fab5e5 commit e850871
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions docs/migration/08_to_10.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit e850871

Please sign in to comment.