Skip to content

Commit

Permalink
wip DB tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TLGINO committed Apr 14, 2023
1 parent 8bc4cd4 commit 8407791
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,89 @@

"""Change AccountsRole primary key to string."""

from alembic import op
import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision = '8f11b75e0995'
down_revision = 'eb9743315a9d'
revision = "8f11b75e0995"
down_revision = "eb9743315a9d"
branch_labels = ()
depends_on = None


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))
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')
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.execute('DROP SEQUENCE accounts_role_id_seq')
op.create_primary_key('pk_accounts_role', 'accounts_role', ['id'])
op.alter_column(
"accounts_role",
"id",
existing_type=sa.Integer,
type_=sa.String(80),
server_default=None,
)
op.execute("DROP SEQUENCE accounts_role_id_seq")
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))
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'])
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)
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
)
# 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)
op.create_primary_key('pk_accounts_role', 'accounts_role', ['id'])
op.alter_column('accounts_role', 'id', existing_type=sa.Integer, autoincrement=True, existing_autoincrement=True, nullable=False)
op.drop_constraint("pk_accounts_role", "accounts_role", type_="primary")
op.alter_column(
"accounts_role", "id", existing_type=sa.String(80), type_=sa.Integer
)
op.create_primary_key("pk_accounts_role", "accounts_role", ["id"])
op.alter_column(
"accounts_role",
"id",
existing_type=sa.Integer,
autoincrement=True,
existing_autoincrement=True,
nullable=False,
)
# Drop new column `is_managed`
op.drop_column('accounts_role', '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'])

op.create_foreign_key(
"fk_accounts_userrole_role_id",
"accounts_userrole",
"accounts_role",
["role_id"],
["id"],
)
7 changes: 5 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 @@ -51,7 +52,7 @@
),
db.Column(
"role_id",
db.Integer(),
db.String(80),
db.ForeignKey("accounts_role.id", name="fk_accounts_userrole_role_id"),
),
)
Expand All @@ -63,7 +64,9 @@ class Role(db.Model, Timestamp, RoleMixin):

__tablename__ = "accounts_role"

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

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

0 comments on commit 8407791

Please sign in to comment.