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
timestamp = datetime.now().strftime("%Y-%m-%d-%H%M%S")
archive_folder = Path("./archive")
orders_archive_path = archive_folder / ("orders-archive-" + timestamp + ".csv")
transactions_archive_path = archive_folder / ("transactions-archive-" + timestamp + ".csv")
if __name__ == "__main__":
exit_code = 0
exit_code = 1
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:
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
with orders_archive_path.open("w") as of:
cur.copy_expert(
"copy (select * from app_order) to STDOUT with csv delimiter ';'",
of
)
with transactions_archive_path.open("w") as tf:
cur.copy_expert(
"copy (select * from app_registertransaction) to STDOUT with csv delimiter ';'",
tf
)
# delete
cur.execute("delete from app_order;")
cur.execute("delete from app_registertransaction;")
connection.commit()
# # # # #
exit_code = 0
print("done.")
except (Error, Exception) as err:
connection.rollback()
print(f"An error occured while upgrading the database at {os.environ['PGDB_HOST']}:\n{err}")
exit_code = 1
finally:
cur.close()
connection.close()
exit(exit_code)

View file

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

View file

@ -15,9 +15,7 @@ from psycopg2 import errorcodes
def check_file():
db_app_version_file = Path("./config/db_app_version.txt")
if not db_app_version_file.exists():
exit(1)
if not db_app_version_file.is_file():
@ -27,78 +25,56 @@ def check_file():
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:
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
cur.execute("""
select value from application_info
where key = 'app_version';
""")
appinfo_result = list(cur.fetchone())[0]
if appinfo_result == None:
cur.close()
connection.close()
exit(1)
if appinfo_result != environ["APP_VERSION"]:
cur.close()
connection.close()
exit(1)
# check rows in app_global
required_rows = [
"global_message",
"admin_info"
]
cur.execute("""
select name from app_global;
""")
table_global_result = list(cur.fetchall())
cur.close()
connection.close()
existing_rows = [list(row)[0] for row in table_global_result]
for r in required_rows:
if not r in existing_rows:
exit(1)
except Error:
cur.close()
connection.close()
exit(1)
except Exception as e:
print(f"An exception occured: {e}")
cur.close()
connection.close()
exit(1)
if __name__ == "__main__":
check_file()
check_database()
exit(0)