Skip to content

Commit

Permalink
chore: removes the migration of user_profile and user_account for exi…
Browse files Browse the repository at this point in the history
…sting users
  • Loading branch information
arnoldknott committed Feb 5, 2025
1 parent e91b6b3 commit 7d0b5e7
Showing 1 changed file with 46 additions and 44 deletions.
90 changes: 46 additions & 44 deletions backendAPI/src/core/databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,51 +24,53 @@ async def get_async_session() -> AsyncSession:
# 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
pass
# Did not work in production!
# 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()
# 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())
Expand Down

0 comments on commit 7d0b5e7

Please sign in to comment.