Changes to db: fixed error in __init__, looking for more precise errors

This commit is contained in:
StNicolay 2022-10-14 17:12:24 +03:00
parent b60579ecd6
commit 85129d8b7a
3 changed files with 11 additions and 8 deletions

View File

@ -1,5 +1,5 @@
import sqlmodel import sqlmodel
import mariadb from sqlalchemy.exc import IntegrityError
from sqlalchemy.future import Engine from sqlalchemy.future import Engine
from . import models from . import models
@ -21,7 +21,7 @@ def add_account(
with sqlmodel.Session(engine) as session: with sqlmodel.Session(engine) as session:
session.add(account) session.add(account)
session.commit() session.commit()
except Exception: except IntegrityError:
return False return False
else: else:
return True return True
@ -34,7 +34,7 @@ def add_master_pass(engine: Engine, user_id: int, salt: bytes, passwd: bytes) ->
with sqlmodel.Session(engine) as session: with sqlmodel.Session(engine) as session:
session.add(master_pass) session.add(master_pass)
session.commit() session.commit()
except Exception: except IntegrityError:
return False return False
else: else:
return True return True

View File

@ -22,7 +22,7 @@ def get_accounts(engine: Engine, user_id: int) -> list[str]:
statement = sqlmodel.select(models.Account).where(models.Account.user_id == user_id) statement = sqlmodel.select(models.Account).where(models.Account.user_id == user_id)
with sqlmodel.Session(engine) as session: with sqlmodel.Session(engine) as session:
result = session.exec(statement) result = session.exec(statement)
return [account.name for account in result] return [account.name for account in result]
def get_account_info( def get_account_info(
@ -34,6 +34,6 @@ def get_account_info(
) )
with sqlmodel.Session(engine) as session: with sqlmodel.Session(engine) as session:
result = session.exec(statement).first() result = session.exec(statement).first()
if result is None: if result is None:
return return
return (result.salt, result.enc_login, result.enc_pass) return (result.salt, result.enc_login, result.enc_pass)

View File

@ -13,4 +13,7 @@ def get_engine(host: str, user: str, passwd: str, db: str) -> Engine:
def prepare(engine: Engine) -> None: def prepare(engine: Engine) -> None:
sqlmodel.SQLModel.metadata.create_all(engine, [models.Account, models.MasterPass]) sqlmodel.SQLModel.metadata.create_all(
engine,
# [models.Account, models.MasterPass]
)