Skip to content

Commit

Permalink
migration: add migration for user documents
Browse files Browse the repository at this point in the history
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.
59 changes: 59 additions & 0 deletions migrations/20231102101356_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# SPDX-License-Identifier: LGPL-2.1-or-later

Check warning on line 1 in migrations/20231102101356_user.py

View workflow job for this annotation

GitHub Actions / Lint

Module name "20231102101356_user" doesn't conform to snake_case naming style
#
# 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

0 comments on commit aba6d60

Please sign in to comment.