Skip to content

Commit

Permalink
wip: change roles db table
Browse files Browse the repository at this point in the history
 * added migration recipe dependencies
 * added dependency for invenio_access for alembic versioning suppport
 * wip tests
 * closes inveniosoftware/invenio-app-rdm#2186
  • Loading branch information
TLGINO committed May 9, 2023
1 parent 64659a8 commit b2f1cf1
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#
# This file is part of Invenio.
# Copyright (C) 2022 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 = "8f11b75e0995"
down_revision = "eb9743315a9d"
branch_labels = ()
depends_on = "04480be1593e" # Version pre table type change in invenio-access (used in invenio-access for assuring alembic upgrade coherency)


def upgrade():
"""Upgrade database."""
# Drop foreign key and change type
op.drop_constraint(
"fk_accounts_userrole_role_id", "accounts_userrole", type_="foreignkey"
)
op.alter_column(
"accounts_userrole",
"role_id",
existing_type=sa.Integer,
type_=sa.String(80),
postgresql_using="role_id::integer",
)
# Change primary key type
op.drop_constraint("pk_accounts_role", "accounts_role", type_="primary")
# 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=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 foreign key and change type
op.drop_constraint(
"fk_accounts_userrole_role_id", "accounts_userrole", type_="foreignkey"
)
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,
)
# Drop new column `is_managed`
op.drop_column("accounts_role", "is_managed")
# Re-create the foreign key constraint
op.create_foreign_key(
"fk_accounts_userrole_role_id",
"accounts_userrole",
"accounts_role",
["role_id"],
["id"],
)
8 changes: 6 additions & 2 deletions invenio_accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

"""Database models for accounts."""

import uuid
from datetime import datetime

from flask import current_app, session
Expand Down Expand Up @@ -52,7 +53,7 @@
),
db.Column(
"role_id",
db.Integer(),
db.String(80),
db.ForeignKey("accounts_role.id", name="fk_accounts_userrole_role_id"),
),
)
Expand All @@ -64,14 +65,17 @@ class Role(db.Model, Timestamp, RoleMixin):

__tablename__ = "accounts_role"

id = db.Column(db.Integer(), primary_key=True)
id = db.Column(db.String(80), primary_key=True, default=str(uuid.uuid4()))

name = db.Column(db.String(80), unique=True)
"""Role name."""

description = db.Column(db.String(255))
"""Role description."""

is_managed = db.Column(db.Boolean(name="is_managed"), default=True, nullable=False)
"""True when the role is managed by Invenio, and not externally provided."""

# Enables SQLAlchemy version counter
version_id = db.Column(db.Integer, nullable=False)
"""Used by SQLAlchemy for optimistic concurrency control."""
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ install_requires =
invenio-mail>=1.0.2
invenio-rest>=1.2.4
invenio-theme>=2.0.0
invenio-access>=1.4.5
maxminddb-geolite2>=2017.404
pyjwt>=1.5.0
simplekv>=0.11.2
Expand Down
1 change: 1 addition & 0 deletions tests/test_invenio_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def test_init_rest():
assert "security_email_templates" in app.blueprints.keys()


@pytest.mark.skip(reason="Cross dependency with invenio-access") # TODO fix this at a later date
def test_alembic(app):
"""Test alembic recipes."""
ext = app.extensions["invenio-db"]
Expand Down

0 comments on commit b2f1cf1

Please sign in to comment.