-
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
268 additions
and
27 deletions.
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
invenio_accounts/alembic/f2522cdd5fcd_change_accountsrole_primary_key_to_.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,134 @@ | ||
# | ||
# This file is part of Invenio. | ||
# Copyright (C) 2016-2018 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 | ||
from sqlalchemy import inspect | ||
|
||
_dependant_modules_with_revisions = [("invenio_access", "04480be1593e"), ("invenio_communities", "fbe746957cfc")] | ||
|
||
|
||
def _get_dependant_revisions(): | ||
"""Computes the revisions that this recipe depends on, based on the installed modules. | ||
There are some revisions in some modules that need this recipe to be executed after some other revisions, due to | ||
the relation that some tables have with the primary key of the Role table declared in this module. This "hack" is | ||
to avoid direct dependency of this module with other ones that are not really required. | ||
""" | ||
dependant_revisions = [] | ||
for module_name, revision_id in _dependant_modules_with_revisions: | ||
try: | ||
__import__(module_name) | ||
dependant_revisions.append(revision_id) | ||
except ImportError as err: | ||
pass | ||
return tuple(dependant_revisions) | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = "f2522cdd5fcd" | ||
down_revision = "eb9743315a9d" | ||
branch_labels = () | ||
depends_on = _get_dependant_revisions() | ||
|
||
|
||
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 column_exists(table_name, column_name): | ||
"""Checks if a column exists in a table.""" | ||
bind = op.get_context().bind | ||
insp = inspect(bind) | ||
columns = insp.get_columns(table_name) | ||
return any(c["name"] == column_name for c in columns) | ||
|
||
|
||
def downgrade(): | ||
"""Downgrade database.""" | ||
# We check for the existence of the column because if it's not there it means that this downgrade was already | ||
# executed by invenio-access | ||
if column_exists("accounts_role", "is_managed"): | ||
# 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
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
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