Skip to content

Commit

Permalink
Merge pull request #198 from arnoldknott/stage
Browse files Browse the repository at this point in the history
Stage
  • Loading branch information
arnoldknott authored Feb 3, 2025
2 parents 43788cd + 7c879c2 commit bb15cfe
Show file tree
Hide file tree
Showing 37 changed files with 2,996 additions and 742 deletions.
1 change: 0 additions & 1 deletion backendAPI/src/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,6 @@ async def _add_one_test_access_policy(
policy: dict, current_user: CurrentUserData = None
):
"""Adds test policies to the database."""

return await add_test_access_policy(policy, current_user)
# async with AccessPolicyCRUD() as crud:
# if current_user is None:
Expand Down
59 changes: 55 additions & 4 deletions backendAPI/src/core/databases.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from sqlalchemy.orm import sessionmaker
from sqlmodel import create_engine
from sqlmodel.ext.asyncio.session import AsyncSession

from core.config import config
from sqlmodel import select
from models.identity import User, UserAccount, UserProfile
from models.access import IdentifierTypeLink, IdentityType

# from sqlmodel import SQLmodel # noqa: F401

Expand All @@ -20,8 +21,58 @@ async def get_async_session() -> AsyncSession:
return async_session()


engine = create_engine(config.POSTGRES_URL.unicode_string())
SynchronSession = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Run extraordinary migrations:
# Comment when propagated all the way into production!
async def run_migrations():
session = await get_async_session()
# async with get_async_session() as session:
response = await session.exec(select(User))
users = response.unique().fetchall()
for user in users:
# # The settings in user account and user profile cannot be changed, before the user is created.
try:
query = select(UserAccount, UserProfile).where(
UserAccount.user_id == user.id, UserProfile.user_id == user.id
)
response = await session.exec(query)
existing_account_and_profile = response.unique().fetchall()
print("=== existing_account_and_profile ===")
print(existing_account_and_profile)
if not existing_account_and_profile:
print("== no account or profile found - trying to generate it ===")
user_account = UserAccount(user_id=user.id)
user_profile = UserProfile(user_id=user.id)
account_type_link = IdentifierTypeLink(
id=user_account.id, type=IdentityType.user_account
)
profile_type_link = IdentifierTypeLink(
id=user_profile.id, type=IdentityType.user_profile
)
user_account = UserAccount.model_validate(user_account)
user_profile = UserProfile.model_validate(user_profile)
user.user_account_id = user_account.id
user.user_profile_id = user_profile.id

session.add(user_account)
session.add(user_profile)
session.add(account_type_link)
session.add(profile_type_link)
session.add(user)
await session.commit()
await session.refresh(user_account)
await session.refresh(user_profile)
await session.refresh(account_type_link)
await session.refresh(profile_type_link)
await session.refresh(user)
except Exception as error:
print("Error in run_migrations:")
print(error)
await session.rollback()
await session.close()


# engine = create_engine(config.POSTGRES_URL.unicode_string())
# SynchronSession = sessionmaker(autocommit=False, autoflush=False, bind=engine)

# # This is handled by __aenter__ and __aexit__ in BaseCRUD
# # in case a session is required elsewhere, use this function!
Expand Down
2 changes: 2 additions & 0 deletions backendAPI/src/core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ class IdentityType(BaseType):

# TBD: consider getting those values programmatically?
user = "User"
user_account = "UserAccount"
user_profile = "UserProfile"
ueber_group = "UeberGroup"
group = "Group"
sub_group = "SubGroup"
Expand Down
Loading

0 comments on commit bb15cfe

Please sign in to comment.