-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* closes inveniosoftware/invenio-app-rdm#2186 * updated cli to pass ids on create role Co-authored-by: jrcastro2 <[email protected]>
- Loading branch information
Showing
8 changed files
with
254 additions
and
69 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
invenio_accounts/alembic/f2522cdd5fcd_change_accountsrole_primary_key_to_string.py
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,102 @@ | ||
# | ||
# This file is part of Invenio. | ||
# Copyright (C) 2023 CERN. | ||
# | ||
# Invenio is free software; you can redistribute it and/or modify it | ||
# under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""Change AccountsRole primary key to string.""" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "f2522cdd5fcd" | ||
down_revision = "eb9743315a9d" | ||
branch_labels = () | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
"""Upgrade database.""" | ||
# Drop primary key and all foreign keys | ||
op.execute("ALTER TABLE accounts_role DROP CONSTRAINT pk_accounts_role CASCADE") | ||
|
||
op.alter_column( | ||
"accounts_userrole", | ||
"role_id", | ||
existing_type=sa.Integer, | ||
type_=sa.String(80), | ||
postgresql_using="role_id::integer", | ||
) | ||
# Change primary key type | ||
# server_default=None will remove the autoincrement | ||
op.alter_column( | ||
"accounts_role", | ||
"id", | ||
existing_type=sa.Integer, | ||
type_=sa.String(80), | ||
server_default=None, | ||
) | ||
op.create_primary_key("pk_accounts_role", "accounts_role", ["id"]) | ||
# Add new column `is_managed` | ||
op.add_column( | ||
"accounts_role", | ||
sa.Column( | ||
"is_managed", sa.Boolean(name="is_managed"), default=True, nullable=True | ||
), | ||
) | ||
op.execute("UPDATE accounts_role SET is_managed = true") | ||
op.alter_column("accounts_role", "is_managed", nullable=False) | ||
|
||
# Re-create the foreign key constraint | ||
op.create_foreign_key( | ||
"fk_accounts_userrole_role_id", | ||
"accounts_userrole", | ||
"accounts_role", | ||
["role_id"], | ||
["id"], | ||
) | ||
|
||
|
||
def downgrade(): | ||
"""Downgrade database.""" | ||
# Drop new column `is_managed` | ||
op.drop_column("accounts_role", "is_managed") | ||
op.execute("ALTER TABLE accounts_role DROP CONSTRAINT pk_accounts_role CASCADE") | ||
|
||
op.alter_column( | ||
"accounts_userrole", | ||
"role_id", | ||
existing_type=sa.String(80), | ||
type_=sa.Integer, | ||
postgresql_using="role_id::integer", | ||
) | ||
# Change primary key type | ||
# op.drop_constraint("pk_accounts_role", "accounts_role", type_="primary") | ||
op.alter_column( | ||
"accounts_role", | ||
"id", | ||
existing_type=sa.String(80), | ||
type_=sa.Integer, | ||
postgresql_using="id::integer", | ||
) | ||
op.create_primary_key("pk_accounts_role", "accounts_role", ["id"]) | ||
op.alter_column( | ||
"accounts_role", | ||
"id", | ||
existing_type=sa.String(80), | ||
type_=sa.Integer, | ||
autoincrement=True, | ||
existing_autoincrement=True, | ||
nullable=False, | ||
) | ||
|
||
# Re-create the foreign key constraint | ||
op.create_foreign_key( | ||
"fk_accounts_userrole_role_id", | ||
"accounts_userrole", | ||
"accounts_role", | ||
["role_id"], | ||
["id"], | ||
) |
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 |
---|---|---|
@@ -1,32 +1,57 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of Invenio. | ||
# Copyright (C) 2022 CERN. | ||
# Copyright (C) 2022-2023 CERN. | ||
# | ||
# Invenio is free software; you can redistribute it and/or modify it | ||
# under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""API objects for Invenio Accounts.""" | ||
|
||
from collections import defaultdict | ||
|
||
class Session: | ||
"""Session object for DB Users change history.""" | ||
|
||
def __init__(self): | ||
"""Constructor.""" | ||
self.updated_users = set() | ||
self.updated_roles = set() | ||
self.deleted_users = set() | ||
self.deleted_roles = set() | ||
|
||
|
||
class DBUsersChangeHistory: | ||
"""DB Users change history storage.""" | ||
|
||
def __init__(self): | ||
"""constructor.""" | ||
# the keys are going to be the sessions, the values are going to be | ||
# the sets of dirty/deleted models | ||
self.updated_users = defaultdict(lambda: list()) | ||
self.updated_roles = defaultdict(lambda: list()) | ||
self.deleted_users = defaultdict(lambda: list()) | ||
self.deleted_roles = defaultdict(lambda: list()) | ||
|
||
def _clear_dirty_sets(self, session): | ||
"""Clear the dirty sets for the given session.""" | ||
"""Constructor.""" | ||
self.sessions = {} | ||
|
||
def _get_session(self, session_id): | ||
"""Returns or creates a session for a concrete session id.""" | ||
return self.sessions.setdefault(session_id, Session()) | ||
|
||
def add_updated_user(self, session_id, user_id): | ||
"""Adds a user to the updated users list.""" | ||
session = self._get_session(session_id) | ||
session.updated_users.add(user_id) | ||
|
||
def add_updated_role(self, session_id, role_id): | ||
"""Adds a role to the updated roles list.""" | ||
session = self._get_session(session_id) | ||
session.updated_roles.add(role_id) | ||
|
||
def add_deleted_user(self, session_id, user_id): | ||
"""Adds a user to the deleted users list.""" | ||
session = self._get_session(session_id) | ||
session.deleted_users.add(user_id) | ||
|
||
def add_deleted_role(self, session_id, role_id): | ||
"""Adds a role to the deleted roles list.""" | ||
session = self._get_session(session_id) | ||
session.deleted_roles.add(role_id) | ||
|
||
def clear_dirty_sets(self, session): | ||
"""Removes session object.""" | ||
sid = id(session) | ||
self.updated_users.pop(sid, None) | ||
self.updated_roles.pop(sid, None) | ||
self.deleted_users.pop(sid, None) | ||
self.deleted_roles.pop(sid, None) | ||
self.sessions.pop(sid, None) |
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
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
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
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
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
Oops, something went wrong.