Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrations: fix existing migrations scripts #438

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 0 additions & 33 deletions migrations/20221014061654_set_timeout.py

This file was deleted.

94 changes: 89 additions & 5 deletions migrations/20231102101356_user.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
# 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
Migration for User schema after the adoption of fastapi-users for
user management in commit:

api.main: update/add user management routes

"""

name = '20231102101356_user'

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

View workflow job for this annotation

GitHub Actions / Lint

Constant name "name" doesn't conform to UPPER_CASE naming style
dependencies = ['20221014061654_set_timeout']
dependencies = []

def user_upgrade_needed(user):
"""Checks if a DB user passed as a parameter needs to be migrated
with this script.

Parameters:
user: a mongodb document (dict) defining a KernelCI user

Returns:
True if the user needs to be migrated, False otherwise

"""
# The existence of a 'profile' key seems to be enough to detect a
# pre-migration user
if 'profile' in user:

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

View workflow job for this annotation

GitHub Actions / Lint

The if statement can be replaced with 'return bool(test)'

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

View workflow job for this annotation

GitHub Actions / Lint

Unnecessary "else" after "return"
return True
else:
return False


def upgrade(db: "pymongo.database.Database"):

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

View workflow job for this annotation

GitHub Actions / Lint

Argument name "db" doesn't conform to snake_case naming style

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

View workflow job for this annotation

GitHub Actions / Lint

Missing function or method docstring
users = db.user.find()
db.user.drop_indexes()
for user in users:
# Skip users that don't need any changes
if not user_upgrade_needed(user):
continue
# Check if the user is an admin (superuser), remove it from the
# "admin" user group if it is
is_superuser = False
new_groups_list = [g for g in user['profile']['groups']
if g['name'] != 'admin']
if len(new_groups_list) != len(user['profile']['groups']):
is_superuser = True
user['profile']['groups'] = new_groups_list
# User update
db.user.replace_one(
{
"_id": user['_id']
Expand All @@ -23,14 +57,64 @@
"_id": user['_id'],
"email": user['profile']['email'],
"hashed_password": user['profile']['hashed_password'],
"is_active": True,
"is_superuser": False,
"is_active": user['active'],
"is_superuser": is_superuser,
"is_verified": False,
"username": user['profile']['username'],
"groups": user['profile']['groups']
},
)
# Sanity check: check if there are any old-format users in the
# "admin" group. Remove the group if there aren't any
remaining_admins = db.user.count(
{
"groups": {
"$elemMatch": {"name": "admin"}
}
}
)
if remaining_admins == 0:
db.usergroup.delete_one({"name": "admin"})
else:
print("Some old 'admin' users still remain")


def downgrade(db: "pymongo.database.Database"):

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

View workflow job for this annotation

GitHub Actions / Lint

Argument name "db" doesn't conform to snake_case naming style

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

View workflow job for this annotation

GitHub Actions / Lint

Missing function or method docstring
pass
superusers = db.user.find({'is_superuser': True})
if superusers:
# Create the 'admin' group if it doesn't exist
db.usergroup.update_one(
{'name': 'admin'},
{'$setOnInsert': {'name': 'admin'}},
upsert=True
)
admin_group = db.usergroup.find_one({'name': 'admin'})

users = db.user.find()
db.user.drop_indexes()
for user in users:
# Skip users that weren't migrated (unlikely corner case)
if user_upgrade_needed(user):
continue
if user.get('is_superuser') == True:
# Add user to admin group
new_groups_list = [g for g in user['groups']
if g['name'] != 'admin']
new_groups_list.append(admin_group)
user['groups'] = new_groups_list

db.user.replace_one(
{
'_id': user['_id'],
},
{
'_id': user['_id'],
'active': user['is_active'],
'profile': {
'email': user['email'],
'hashed_password': user['hashed_password'],
'username': user['username'],
'groups': user['groups'],
}
}
)
Loading