-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migration: add migration for user documents
Since the `User` schema has been changed after `fastapi-users` integration, add a migration to update all the existing documents accordingly. Signed-off-by: Jeny Sadadia <[email protected]>
- Loading branch information
Jeny Sadadia
committed
Nov 3, 2023
1 parent
3cc1fc0
commit aba6d60
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# SPDX-License-Identifier: LGPL-2.1-or-later | ||
# | ||
# Copyright (C) 2023 Collabora Limited | ||
# Author: Jeny Sadadia <[email protected]> | ||
|
||
""" | ||
Migration for User schema | ||
""" | ||
|
||
name = '20231102101356_user' | ||
dependencies = ['20221014061654_set_timeout'] | ||
|
||
|
||
def upgrade(db: "pymongo.database.Database"): | ||
users = db.user.find() | ||
for user in users: | ||
new_document = { | ||
"_id": user['_id'], | ||
"email": user['profile']['email'], | ||
"hashed_password": user['profile']['hashed_password'], | ||
"is_active": 1, | ||
"is_superuser": 0, | ||
"is_verified": 0, | ||
"username": user['profile']['username'], | ||
"groups": user['profile']['groups'] | ||
} | ||
# delete old document | ||
db.user.delete_one({"_id": user['_id']}) | ||
|
||
# insert new document | ||
db.user.insert(new_document) | ||
print(db.user.find_one({"_id": user['_id']})) | ||
# db.user.replace_one( | ||
# { | ||
# "_id": user['_id'] | ||
# }, | ||
# { | ||
# "_id": user['_id'], | ||
# "email": user['profile']['email'], | ||
# "hashed_password": user['profile']['hashed_password'], | ||
# "is_active": 1, | ||
# "is_superuser": 0, | ||
# "is_verified": 0, | ||
# "username": user['profile']['username'], | ||
# "groups": user['profile']['groups'] | ||
# }, | ||
# ) | ||
# db.user.update_one( | ||
# { | ||
# "_id": user['_id'] | ||
# }, | ||
# { | ||
# "$unset": {"profile": "", "active": ""} | ||
# } | ||
# ) | ||
|
||
|
||
def downgrade(db: "pymongo.database.Database"): | ||
pass |