-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from jadmsaadaot/SUBMIT-11
Add a User entity and table and make AccountUser it's child
- Loading branch information
Showing
38 changed files
with
528 additions
and
307 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
submit-api/migrations/versions/075df7d4f03b_add_user_type.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,53 @@ | ||
""" Add user type column | ||
Revision ID: 075df7d4f03b | ||
Revises: 8e3b41ce9e50 | ||
Create Date: 2024-10-30 16:15:35.557115 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '075df7d4f03b' | ||
down_revision = '8e3b41ce9e50' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
usertype_enum = sa.Enum('PROPONENT', 'STAFF', name='usertype') | ||
usertype_enum.create(op.get_bind(), checkfirst=True) | ||
|
||
with op.batch_alter_table('users', schema=None) as batch_op: | ||
batch_op.add_column(sa.Column('type', usertype_enum, nullable=True)) | ||
|
||
# get list of user_ids from account_users | ||
conn = op.get_bind() | ||
result = conn.execute(sa.text("SELECT user_id FROM account_users")).fetchall() | ||
user_ids = [row[0] for row in result] | ||
|
||
# update user type to PROPONENT for all users in account_users, pgsql | ||
op.execute(f"UPDATE users SET type = 'PROPONENT' WHERE id IN ({','.join(map(str, user_ids))})") | ||
|
||
# update user type to STAFF for all users not in account_users, pgsql | ||
op.execute(f"UPDATE users SET type = 'STAFF' WHERE id NOT IN ({','.join(map(str, user_ids))})") | ||
|
||
with op.batch_alter_table('users', schema=None) as batch_op: | ||
batch_op.alter_column('type', nullable=False) | ||
|
||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
with op.batch_alter_table('users', schema=None) as batch_op: | ||
batch_op.drop_column('type') | ||
|
||
# Drop the enum type | ||
usertype_enum = sa.Enum('PROPONENT', 'STAFF', name='usertype') | ||
usertype_enum.drop(op.get_bind(), checkfirst=True) | ||
|
||
# ### end Alembic commands ### |
46 changes: 46 additions & 0 deletions
46
submit-api/migrations/versions/20a2b81fe5f7_add_users_table.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,46 @@ | ||
""" add user table | ||
Revision ID: 20a2b81fe5f7 | ||
Revises: e1e1b81ad5c8 | ||
Create Date: 2024-10-30 12:56:44.220088 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '20a2b81fe5f7' | ||
down_revision = 'e1e1b81ad5c8' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('users', | ||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), | ||
sa.Column('auth_guid', sa.String(), nullable=False), | ||
sa.Column('created_date', sa.DateTime(), nullable=False), | ||
sa.Column('updated_date', sa.DateTime(), nullable=True), | ||
sa.Column('created_by', sa.String(length=50), nullable=True), | ||
sa.Column('updated_by', sa.String(length=50), nullable=True), | ||
sa.PrimaryKeyConstraint('id'), | ||
sa.UniqueConstraint('auth_guid') | ||
) | ||
with op.batch_alter_table('users', schema=None) as batch_op: | ||
batch_op.create_index('ix_users_auth_guid', ['auth_guid'], unique=True) | ||
|
||
# populate auth_guid, created_Date, updated_date, created_by, updated_by from account_users | ||
op.execute("INSERT INTO users (auth_guid, created_date, updated_date, created_by, updated_by) SELECT auth_guid, created_date, updated_date, created_by, updated_by FROM account_users") | ||
|
||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
with op.batch_alter_table('users', schema=None) as batch_op: | ||
batch_op.drop_index('ix_users_auth_guid') | ||
|
||
op.drop_table('users') | ||
# ### end Alembic commands ### |
31 changes: 31 additions & 0 deletions
31
submit-api/migrations/versions/8e3b41ce9e50_drop_authguid_from_account_user.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,31 @@ | ||
"""Drop auth_guid from account_users | ||
Revision ID: 8e3b41ce9e50 | ||
Revises: cba5ebbb4a74 | ||
Create Date: 2024-10-30 16:06:23.630182 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '8e3b41ce9e50' | ||
down_revision = 'cba5ebbb4a74' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_column('account_users', 'auth_guid') | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column('account_users', sa.Column('auth_guid', sa.String(), nullable=True)) | ||
op.execute("UPDATE account_users au SET auth_guid = u.auth_guid FROM users u WHERE u.id = au.user_id") | ||
op.create_unique_constraint('unique_account_users_auth_guid', 'account_users', ['auth_guid']) | ||
op.alter_column('account_users', 'auth_guid', existing_type=sa.String(), nullable=False) | ||
# ### end Alembic commands ### |
30 changes: 30 additions & 0 deletions
30
submit-api/migrations/versions/cba5ebbb4a74_add_user_id_to_account_users.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,30 @@ | ||
"""Add user_id to account_users | ||
Revision ID: cba5ebbb4a74 | ||
Revises: 20a2b81fe5f7 | ||
Create Date: 2024-10-30 12:58:53.607737 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'cba5ebbb4a74' | ||
down_revision = '20a2b81fe5f7' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# add user_id to account_users and populate it then make it foreign key | ||
op.add_column('account_users', sa.Column('user_id', sa.Integer(), nullable=True)) | ||
op.execute("UPDATE account_users au SET user_id = u.id FROM users u WHERE u.auth_guid = au.auth_guid") | ||
op.alter_column('account_users', 'user_id', existing_type=sa.Integer(), nullable=False) | ||
op.create_foreign_key('fk_account_users_user_id_users', 'account_users', 'users', ['user_id'], ['id']) | ||
|
||
|
||
def downgrade(): | ||
# remove user_id from account_users | ||
op.drop_constraint('fk_account_users_user_id_users', 'account_users', type_='foreignkey') | ||
op.drop_column('account_users', 'user_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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
"""User model class. | ||
Manages the user | ||
""" | ||
from __future__ import annotations | ||
|
||
import enum | ||
|
||
from sqlalchemy import Column, Enum, Index | ||
|
||
from .base_model import BaseModel | ||
from .db import db | ||
|
||
|
||
class UserType(enum.Enum): | ||
"""Enum for user type.""" | ||
|
||
PROPONENT = 'PROPONENT' | ||
STAFF = 'STAFF' | ||
|
||
|
||
class User(BaseModel): | ||
"""Definition of the User entity.""" | ||
|
||
__tablename__ = 'users' | ||
|
||
id = Column(db.Integer, primary_key=True, autoincrement=True) | ||
auth_guid = Column(db.String(), nullable=False, unique=True) | ||
type = Column(Enum(UserType), nullable=False) | ||
account_user = db.relationship('AccountUser', uselist=False, back_populates='user') | ||
|
||
__table_args__ = ( | ||
Index('ix_users_auth_guid', 'auth_guid', unique=True), | ||
) | ||
|
||
@classmethod | ||
def create_user(cls, data, session=None) -> User: | ||
"""Create user.""" | ||
account_user = User( | ||
auth_guid=data.get('auth_guid', None), | ||
type=data.get('type', None) | ||
) | ||
if session: | ||
session.add(account_user) | ||
session.commit() | ||
else: | ||
account_user.save() | ||
return account_user | ||
|
||
@classmethod | ||
def get_by_guid(cls, _guid): | ||
"""Get user by guid.""" | ||
return cls.query.filter(cls.auth_guid == _guid).first() |
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.