forked from beimingwu/beimingwu
feat(backend): separate read and write operations in database
This commit is contained in:
parent
14520165b2
commit
461ff49255
|
@ -79,6 +79,7 @@ _DEFAULT_CONFIG = {
|
|||
# Database config
|
||||
"database": {
|
||||
"type": "sqlalchemy",
|
||||
"url_read": f"sqlite:///{SQLITE_PATH}",
|
||||
"url": f"sqlite:///{SQLITE_PATH}",
|
||||
},
|
||||
# Engine config
|
||||
|
|
|
@ -19,5 +19,5 @@ class Database:
|
|||
):
|
||||
raise NotImplementedError("'install' Method NOT Implemented.")
|
||||
|
||||
def execute(self, sql, params=None):
|
||||
def execute(self, sql, params=None, read_only=False):
|
||||
raise NotImplementedError("'query' Method NOT Implemented.")
|
||||
|
|
|
@ -212,6 +212,9 @@ class SQLAlchemy(Database):
|
|||
|
||||
def __init__(self, config, admin_password):
|
||||
self.url = config["url"]
|
||||
self.url_read = config["url_read"]
|
||||
self.engine = None
|
||||
self.engine_read = None
|
||||
self.install(admin_password)
|
||||
pass
|
||||
|
||||
|
@ -224,6 +227,7 @@ class SQLAlchemy(Database):
|
|||
|
||||
helper.create_database(self.url)
|
||||
self.engine = create_engine(self.url, future=True)
|
||||
self.engine_read = create_engine(self.url_read, future=True)
|
||||
|
||||
# create all tables
|
||||
DeclarativeBase.metadata.create_all(self.engine)
|
||||
|
@ -235,12 +239,17 @@ class SQLAlchemy(Database):
|
|||
|
||||
else:
|
||||
self.engine = create_engine(self.url, future=True)
|
||||
self.engine_read = create_engine(self.url_read, future=True)
|
||||
pass
|
||||
|
||||
def execute(self, sql, params=None, conn=None):
|
||||
def execute(self, sql, params=None, conn=None, read_only=False):
|
||||
if conn is None:
|
||||
if read_only:
|
||||
conn_ = self.engine_read.connect()
|
||||
else:
|
||||
conn_ = self.engine.connect()
|
||||
pass
|
||||
pass
|
||||
else:
|
||||
conn_ = conn
|
||||
pass
|
||||
|
|
|
@ -43,7 +43,7 @@ def check_user_exist_exclude(by, value, exclude, conn):
|
|||
|
||||
|
||||
def get_user_info(by, value):
|
||||
rows = context.database.execute(f"SELECT * FROM tb_user WHERE {by} = :{by}", {by: value})
|
||||
rows = context.database.execute(f"SELECT * FROM tb_user WHERE {by} = :{by}", {by: value}, read_only=True)
|
||||
if len(rows) == 0:
|
||||
return None
|
||||
else:
|
||||
|
@ -242,7 +242,7 @@ def get_learnware_list(by, value, limit=None, page=None, is_verified=None):
|
|||
|
||||
def get_learnware_list_by_user_id(user_id, limit, page):
|
||||
cnt = context.database.execute(
|
||||
"SELECT COUNT(1) FROM tb_user_learnware_relation WHERE user_id = :user_id", {"user_id": user_id}
|
||||
"SELECT COUNT(1) FROM tb_user_learnware_relation WHERE user_id = :user_id", {"user_id": user_id}, read_only=True
|
||||
)[0][0]
|
||||
|
||||
query = """
|
||||
|
@ -287,6 +287,7 @@ def get_verify_log(user_id, learnware_id):
|
|||
rows = context.database.execute(
|
||||
sql,
|
||||
{"user_id": user_id, "learnware_id": learnware_id},
|
||||
read_only=True,
|
||||
)
|
||||
|
||||
if len(rows) == 0:
|
||||
|
@ -328,7 +329,8 @@ def get_learnware_owners(learnware_ids):
|
|||
" JOIN tb_user_learnware_relation tb2 ON tb1.id=tb2.user_id WHERE learnware_id in {0}".format(
|
||||
"(" + ",".join(["'" + learnware_id + "'" for learnware_id in learnware_ids]) + ")"
|
||||
)
|
||||
)
|
||||
),
|
||||
read_only=True,
|
||||
)
|
||||
|
||||
rmap = {r[0]: r[1] for r in rows}
|
||||
|
@ -457,6 +459,7 @@ def get_learnware_timestamps(learnware_ids):
|
|||
"(" + ",".join(["'" + learnware_id + "'" for learnware_id in learnware_ids]) + ")"
|
||||
),
|
||||
# {"learnware_ids": learnware_ids},
|
||||
read_only=True,
|
||||
)
|
||||
result = {r[0]: r[1] for r in result}
|
||||
|
||||
|
@ -506,6 +509,7 @@ def get_learnware_verify_status(learnware_id):
|
|||
result = context.database.execute(
|
||||
"SELECT verify_status FROM tb_user_learnware_relation WHERE learnware_id = :learnware_id",
|
||||
{"learnware_id": learnware_id},
|
||||
read_only=True,
|
||||
)
|
||||
if len(result) == 0:
|
||||
raise RuntimeError(f"learnware_id {learnware_id} not found")
|
||||
|
@ -521,7 +525,9 @@ def create_user_token(user_id, token):
|
|||
|
||||
|
||||
def get_user_tokens(user_id) -> List[str]:
|
||||
result = context.database.execute("SELECT token FROM tb_user_token WHERE user_id = :user_id", {"user_id": user_id})
|
||||
result = context.database.execute(
|
||||
"SELECT token FROM tb_user_token WHERE user_id = :user_id", {"user_id": user_id}, read_only=True
|
||||
)
|
||||
if len(result) == 0:
|
||||
return []
|
||||
|
||||
|
@ -598,7 +604,9 @@ def get_learnware_count_queued_or_processing():
|
|||
|
||||
|
||||
def check_user_admin(user_id):
|
||||
result = context.database.execute("SELECT role FROM tb_user WHERE id = :user_id", {"user_id": user_id})
|
||||
result = context.database.execute(
|
||||
"SELECT role FROM tb_user WHERE id = :user_id", {"user_id": user_id}, read_only=True
|
||||
)
|
||||
|
||||
if len(result) == 0:
|
||||
return False
|
||||
|
|
|
@ -100,15 +100,6 @@ class SearchLearnware(flask_restful.Resource):
|
|||
pass
|
||||
|
||||
print(msg)
|
||||
try:
|
||||
print("=" * 60)
|
||||
if ret not in [None, False]:
|
||||
lis = ret[1]
|
||||
for x in lis:
|
||||
print(x.get_specification().get_semantic_spec()["Name"]["Values"])
|
||||
print("=" * 60)
|
||||
except Exception as err:
|
||||
print(err)
|
||||
|
||||
if not status:
|
||||
return {"code": 41, "msg": msg}, 200
|
||||
|
|
Loading…
Reference in New Issue