-
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
217 additions
and
25 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
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,102 @@ | ||
# | ||
# 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 | ||
|
||
# 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
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