From 44bab0fb48eb41d1261043048185075ffc98dd43 Mon Sep 17 00:00:00 2001 From: StNicolay <103897650+StNicolay@users.noreply.github.com> Date: Mon, 26 Sep 2022 22:44:48 +0300 Subject: [PATCH] Created database/prepare.py --- src/database/prepare.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/database/prepare.py diff --git a/src/database/prepare.py b/src/database/prepare.py new file mode 100644 index 0000000..5891c90 --- /dev/null +++ b/src/database/prepare.py @@ -0,0 +1,39 @@ +import mariadb + + +def _create_tables(con: mariadb.Connection) -> None: + cursor = con.cursor() + cursor.execute( + """CREATE TABLE IF NOT EXISTS master_pass (user_id INT, + salt BINARY(64), + PASSWD BINARY(64), + PRIMARY KEY(user_id) + )""" + ) + cursor.execute( + """CREATE TABLE IF NOT EXISTS accounts(user_id INT, + acc_name VARCHAR(255), + salt BINARY(64), + enc_login BINARY(64), + enc_pass BINARY(64), + UNIQUE(acc_name, user_id) + )""" + ) + cursor.close() + + +def _create_index(con: mariadb.Connection) -> None: + cursor = con.cursor() + cursor.execute( + """CREATE INDEX IF NOT EXISTS user_id_to_acc on accounts(user_id) + """ + ) + + +def prepare(host: int, user: str, passwd: str, database: str) -> None: + con: mariadb.Connection = mariadb.connect( + host=host, user=user, password=passwd, database=database + ) + _create_tables(con) + _create_index(con) + con.close()