Changed some issues with the scope of psycopg cursors in some scripts
This commit is contained in:
parent
cde9081197
commit
0012214f9b
3 changed files with 29 additions and 111 deletions
|
@ -11,20 +11,13 @@ 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
|
|
||||||
|
|
||||||
try:
|
|
||||||
|
|
||||||
print(f"Starting archiving to {orders_archive_path.__str__()} and {transactions_archive_path.__str__()}...")
|
|
||||||
|
|
||||||
connection = connect(
|
connection = connect(
|
||||||
user = os.environ["PGDB_USER"],
|
user = os.environ["PGDB_USER"],
|
||||||
password = os.environ["PGDB_PASSWORD"],
|
password = os.environ["PGDB_PASSWORD"],
|
||||||
|
@ -32,44 +25,33 @@ if __name__ == "__main__":
|
||||||
port = os.environ["PGDB_PORT"],
|
port = os.environ["PGDB_PORT"],
|
||||||
database = os.environ["PGDB_DB"]
|
database = os.environ["PGDB_DB"]
|
||||||
)
|
)
|
||||||
|
|
||||||
cur = connection.cursor()
|
cur = connection.cursor()
|
||||||
|
try:
|
||||||
|
print(f"Starting archiving to {orders_archive_path.__str__()} and {transactions_archive_path.__str__()}...")
|
||||||
# # # # #
|
# # # # #
|
||||||
|
|
||||||
# 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)
|
||||||
|
|
|
@ -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,13 +38,7 @@ def execute_sql_statement(cursor:_cursor, connection:_connection, sql_statement)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
exit_code = 1
|
||||||
exit_code = 0
|
|
||||||
|
|
||||||
try:
|
|
||||||
|
|
||||||
log("\nSetting up/upgrading database...")
|
|
||||||
|
|
||||||
conn = connect(
|
conn = connect(
|
||||||
user = os.environ["PGDB_USER"],
|
user = os.environ["PGDB_USER"],
|
||||||
password = os.environ["PGDB_PASSWORD"],
|
password = os.environ["PGDB_PASSWORD"],
|
||||||
|
@ -53,105 +46,72 @@ if __name__ == "__main__":
|
||||||
port = os.environ["PGDB_PORT"],
|
port = os.environ["PGDB_PORT"],
|
||||||
database = os.environ["PGDB_DB"]
|
database = os.environ["PGDB_DB"]
|
||||||
)
|
)
|
||||||
|
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
|
try:
|
||||||
|
log("\nSetting up/upgrading database...")
|
||||||
# # # # #
|
# # # # #
|
||||||
|
|
||||||
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)
|
||||||
|
|
|
@ -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,9 +25,6 @@ def check_file():
|
||||||
|
|
||||||
|
|
||||||
def check_database():
|
def check_database():
|
||||||
|
|
||||||
try:
|
|
||||||
|
|
||||||
connection = connect(
|
connection = connect(
|
||||||
user = environ["PGDB_USER"],
|
user = environ["PGDB_USER"],
|
||||||
password = environ["PGDB_PASSWORD"],
|
password = environ["PGDB_PASSWORD"],
|
||||||
|
@ -37,68 +32,49 @@ def check_database():
|
||||||
port = environ["PGDB_PORT"],
|
port = environ["PGDB_PORT"],
|
||||||
database = environ["PGDB_DB"]
|
database = environ["PGDB_DB"]
|
||||||
)
|
)
|
||||||
|
|
||||||
cur = connection.cursor()
|
cur = connection.cursor()
|
||||||
|
try:
|
||||||
# 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)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue