Release 15 - Revamp #38

Merged
ChaoticByte merged 27 commits from devel into main 2023-03-26 11:09:31 +00:00
3 changed files with 29 additions and 111 deletions
Showing only changes of commit 0012214f9b - Show all commits

View file

@ -11,65 +11,47 @@ from psycopg2 import connect
# archive (copy & delete) all entries in app_order and app_registertransaction # archive (copy & delete) all entries in app_order and app_registertransaction
timestamp = datetime.now().strftime("%Y-%m-%d-%H%M%S") timestamp = datetime.now().strftime("%Y-%m-%d-%H%M%S")
archive_folder = Path("./archive") archive_folder = Path("./archive")
orders_archive_path = archive_folder / ("orders-archive-" + timestamp + ".csv") orders_archive_path = archive_folder / ("orders-archive-" + timestamp + ".csv")
transactions_archive_path = archive_folder / ("transactions-archive-" + timestamp + ".csv") transactions_archive_path = archive_folder / ("transactions-archive-" + timestamp + ".csv")
if __name__ == "__main__": if __name__ == "__main__":
exit_code = 1
exit_code = 0 connection = connect(
user = os.environ["PGDB_USER"],
password = os.environ["PGDB_PASSWORD"],
host = os.environ["PGDB_HOST"],
port = os.environ["PGDB_PORT"],
database = os.environ["PGDB_DB"]
)
cur = connection.cursor()
try: try:
print(f"Starting archiving to {orders_archive_path.__str__()} and {transactions_archive_path.__str__()}...") print(f"Starting archiving to {orders_archive_path.__str__()} and {transactions_archive_path.__str__()}...")
connection = connect(
user = os.environ["PGDB_USER"],
password = os.environ["PGDB_PASSWORD"],
host = os.environ["PGDB_HOST"],
port = os.environ["PGDB_PORT"],
database = os.environ["PGDB_DB"]
)
cur = connection.cursor()
# # # # # # # # # #
# copy # copy
with orders_archive_path.open("w") as of: with orders_archive_path.open("w") as of:
cur.copy_expert( cur.copy_expert(
"copy (select * from app_order) to STDOUT with csv delimiter ';'", "copy (select * from app_order) to STDOUT with csv delimiter ';'",
of of
) )
with transactions_archive_path.open("w") as tf: with transactions_archive_path.open("w") as tf:
cur.copy_expert( cur.copy_expert(
"copy (select * from app_registertransaction) to STDOUT with csv delimiter ';'", "copy (select * from app_registertransaction) to STDOUT with csv delimiter ';'",
tf tf
) )
# delete # delete
cur.execute("delete from app_order;") cur.execute("delete from app_order;")
cur.execute("delete from app_registertransaction;") cur.execute("delete from app_registertransaction;")
connection.commit() connection.commit()
# # # # # # # # # #
exit_code = 0
print("done.") print("done.")
except (Error, Exception) as err: except (Error, Exception) as err:
connection.rollback() connection.rollback()
print(f"An error occured while upgrading the database at {os.environ['PGDB_HOST']}:\n{err}") print(f"An error occured while upgrading the database at {os.environ['PGDB_HOST']}:\n{err}")
exit_code = 1 exit_code = 1
finally: finally:
cur.close() cur.close()
connection.close() connection.close()
exit(exit_code) exit(exit_code)

View file

@ -14,7 +14,6 @@ from psycopg2 import errorcodes
# setup or upgrade the database # setup or upgrade the database
def log(s, error=False): def log(s, error=False):
if error: if error:
print(f"{s}", file=sys.stderr) print(f"{s}", file=sys.stderr)
@ -39,119 +38,80 @@ def execute_sql_statement(cursor:_cursor, connection:_connection, sql_statement)
if __name__ == "__main__": if __name__ == "__main__":
exit_code = 1
exit_code = 0 conn = connect(
user = os.environ["PGDB_USER"],
password = os.environ["PGDB_PASSWORD"],
host = os.environ["PGDB_HOST"],
port = os.environ["PGDB_PORT"],
database = os.environ["PGDB_DB"]
)
cur = conn.cursor()
try: try:
log("\nSetting up/upgrading database...") log("\nSetting up/upgrading database...")
conn = connect(
user = os.environ["PGDB_USER"],
password = os.environ["PGDB_PASSWORD"],
host = os.environ["PGDB_HOST"],
port = os.environ["PGDB_PORT"],
database = os.environ["PGDB_DB"]
)
cur = conn.cursor()
# # # # # # # # # #
log("Not deleting register_balance. You can delete it via the Admin Panel (Globals -> register_balance), as it is no more used.") log("Not deleting register_balance. You can delete it via the Admin Panel (Globals -> register_balance), as it is no more used.")
execute_sql_statement(cur, conn, """ execute_sql_statement(cur, conn, """
insert into app_global insert into app_global
values ('global_message', 'Here you can set a global message that will be shown to every user.', 0.0, ''); values ('global_message', 'Here you can set a global message that will be shown to every user.', 0.0, '');
""") """)
execute_sql_statement(cur, conn, """ execute_sql_statement(cur, conn, """
insert into app_global insert into app_global
values ('admin_info', 'Here you can set am infotext that will be displayed on the admin panel.', 0.0, ''); values ('admin_info', 'Here you can set am infotext that will be displayed on the admin panel.', 0.0, '');
""") """)
execute_sql_statement(cur, conn, """ execute_sql_statement(cur, conn, """
create or replace view app_userdeposits_view as create or replace view app_userdeposits_view as
select * from app_registertransaction select * from app_registertransaction
where is_user_deposit = true; where is_user_deposit = true;
""") """)
# # # # # # # # # #
# set app_version in file and database # set app_version in file and database
# database # database
try: try:
cur.execute(""" cur.execute("""
select value from application_info select value from application_info
where key = 'app_version'; where key = 'app_version';
""") """)
result = cur.fetchone() result = cur.fetchone()
if result == None: if result == None:
cur.execute(f""" cur.execute(f"""
insert into application_info values ('app_version', '{os.environ['APP_VERSION']}'); insert into application_info values ('app_version', '{os.environ['APP_VERSION']}');
""") """)
conn.commit() conn.commit()
else: else:
cur.execute(f""" cur.execute(f"""
update application_info set value = '{os.environ['APP_VERSION']}' where key = 'app_version'; update application_info set value = '{os.environ['APP_VERSION']}' where key = 'app_version';
""") """)
conn.commit() conn.commit()
except Error as err: except Error as err:
if err.pgcode == errorcodes.UNDEFINED_TABLE: if err.pgcode == errorcodes.UNDEFINED_TABLE:
try: try:
conn.rollback() conn.rollback()
cur.execute(""" cur.execute("""
create table application_info ( create table application_info (
key varchar(32) primary key, key varchar(32) primary key,
value text value text
); );
""") """)
cur.execute(f""" cur.execute(f"""
insert into application_info values ('app_version', '{os.environ['APP_VERSION']}'); insert into application_info values ('app_version', '{os.environ['APP_VERSION']}');
""") """)
conn.commit() conn.commit()
except Error as err2: except Error as err2:
log(f"An error occurred while setting app_version in table application_info: {err}", error=True) log(f"An error occurred while setting app_version in table application_info: {err}", error=True)
exit_code = 1 exit_code = 1
else: else:
log(f"An error occurred while setting app_version in table application_info: {err}", error=True) log(f"An error occurred while setting app_version in table application_info: {err}", error=True)
exit_code = 1 exit_code = 1
# file # file
Path("./config/db_app_version.txt").write_text(os.environ["APP_VERSION"]) Path("./config/db_app_version.txt").write_text(os.environ["APP_VERSION"])
# done
exit_code = 0
log("done with db setup/upgrade.") log("done with db setup/upgrade.")
except (Error, Exception) as err: except (Error, Exception) as err:
log(f"An error occured while upgrading the database at {os.environ['PGDB_HOST']}:\n{err}", error=True) log(f"An error occured while upgrading the database at {os.environ['PGDB_HOST']}:\n{err}", error=True)
exit_code = 1 exit_code = 1
finally: finally:
cur.close() cur.close()
conn.close() conn.close()
exit(exit_code) exit(exit_code)

View file

@ -15,9 +15,7 @@ from psycopg2 import errorcodes
def check_file(): def check_file():
db_app_version_file = Path("./config/db_app_version.txt") db_app_version_file = Path("./config/db_app_version.txt")
if not db_app_version_file.exists(): if not db_app_version_file.exists():
exit(1) exit(1)
if not db_app_version_file.is_file(): if not db_app_version_file.is_file():
@ -27,78 +25,56 @@ def check_file():
def check_database(): def check_database():
connection = connect(
user = environ["PGDB_USER"],
password = environ["PGDB_PASSWORD"],
host = environ["PGDB_HOST"],
port = environ["PGDB_PORT"],
database = environ["PGDB_DB"]
)
cur = connection.cursor()
try: try:
connection = connect(
user = environ["PGDB_USER"],
password = environ["PGDB_PASSWORD"],
host = environ["PGDB_HOST"],
port = environ["PGDB_PORT"],
database = environ["PGDB_DB"]
)
cur = connection.cursor()
# check application version in db # check application version in db
cur.execute(""" cur.execute("""
select value from application_info select value from application_info
where key = 'app_version'; where key = 'app_version';
""") """)
appinfo_result = list(cur.fetchone())[0] appinfo_result = list(cur.fetchone())[0]
if appinfo_result == None: if appinfo_result == None:
cur.close() cur.close()
connection.close() connection.close()
exit(1) exit(1)
if appinfo_result != environ["APP_VERSION"]: if appinfo_result != environ["APP_VERSION"]:
cur.close() cur.close()
connection.close() connection.close()
exit(1) exit(1)
# check rows in app_global # check rows in app_global
required_rows = [ required_rows = [
"global_message", "global_message",
"admin_info" "admin_info"
] ]
cur.execute(""" cur.execute("""
select name from app_global; select name from app_global;
""") """)
table_global_result = list(cur.fetchall()) table_global_result = list(cur.fetchall())
cur.close() cur.close()
connection.close() connection.close()
existing_rows = [list(row)[0] for row in table_global_result] existing_rows = [list(row)[0] for row in table_global_result]
for r in required_rows: for r in required_rows:
if not r in existing_rows: if not r in existing_rows:
exit(1) exit(1)
except Error: except Error:
cur.close() cur.close()
connection.close() connection.close()
exit(1) exit(1)
except Exception as e: except Exception as e:
print(f"An exception occured: {e}") print(f"An exception occured: {e}")
cur.close() cur.close()
connection.close() connection.close()
exit(1) exit(1)
if __name__ == "__main__": if __name__ == "__main__":
check_file() check_file()
check_database() check_database()
exit(0) exit(0)