From ec45ef065b9e10ba0be1ca104d9e9df2486a512f Mon Sep 17 00:00:00 2001 From: Angela Tran Date: Wed, 3 Aug 2022 02:07:38 +0000 Subject: [PATCH 1/4] fix: make database setup idempotent --- setup.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/setup.py b/setup.py index 2695d964..783df552 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,7 @@ import csv import json +from flask_sqlalchemy import inspect from eligibility_server import app, settings @@ -33,8 +34,13 @@ def save_users(user_id: str, key: str, types: str): if __name__ == "__main__": - print("Creating table...") - app.db.create_all() - print("Table created.") - import_users() - print(app.User.query.count(), "users added.") + inspector = inspect(app.db.engine) + + if inspector.get_table_names(): + print("Tables already exist.") + else: + print("Creating table...") + app.db.create_all() + print("Table created.") + import_users() + print(app.User.query.count(), "users added.") From 03c18c0eaa0b1270b141c85104c3303beecab29b Mon Sep 17 00:00:00 2001 From: Angela Tran Date: Wed, 3 Aug 2022 02:10:18 +0000 Subject: [PATCH 2/4] fix: make database teardown idempotent --- teardown.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/teardown.py b/teardown.py index c35f4d17..4edc0dc8 100644 --- a/teardown.py +++ b/teardown.py @@ -1,8 +1,14 @@ +from flask_sqlalchemy import inspect from eligibility_server import app if __name__ == "__main__": - print(app.User.query.count(), "users to be deleted.") - app.User.query.delete() - app.db.session.commit() - app.db.drop_all() - print("Database dropped.") + inspector = inspect(app.db.engine) + + if inspector.get_table_names(): + print(app.User.query.count(), "users to be deleted.") + app.User.query.delete() + app.db.session.commit() + app.db.drop_all() + print("Database dropped.") + else: + print("Database does not exist.") From 19b575ff495595da68ee8431ee3a9e0ed842c5cd Mon Sep 17 00:00:00 2001 From: Angela Tran Date: Wed, 3 Aug 2022 18:10:46 +0000 Subject: [PATCH 3/4] feat: add check in setup to import data if user table is empty --- setup.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 783df552..f9099644 100644 --- a/setup.py +++ b/setup.py @@ -18,6 +18,8 @@ def import_users(): for user in data: save_users(user[0], user[1], user[2]) + print(app.User.query.count(), "users added.") + def save_users(user_id: str, key: str, types: str): """ @@ -38,9 +40,12 @@ def save_users(user_id: str, key: str, types: str): if inspector.get_table_names(): print("Tables already exist.") + if app.User.query.count() == 0: + import_users() + else: + print("User table already has data.") else: print("Creating table...") app.db.create_all() print("Table created.") import_users() - print(app.User.query.count(), "users added.") From e6b2b90f5dcd887e43bc489748235d7935f7d27d Mon Sep 17 00:00:00 2001 From: Angela Tran Date: Wed, 3 Aug 2022 18:14:17 +0000 Subject: [PATCH 4/4] refactor: use elif when checking file format and print for unsupported --- setup.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index f9099644..992d8ab3 100644 --- a/setup.py +++ b/setup.py @@ -12,11 +12,13 @@ def import_users(): data = json.load(file)["users"] for user in data: save_users(user, data[user][0], str(data[user][1])) - if settings.IMPORT_FILE_FORMAT == "csv": + elif settings.IMPORT_FILE_FORMAT == "csv": with open(settings.IMPORT_FILE_PATH, newline="", encoding="utf-8") as file: data = csv.reader(file, delimiter=";", quotechar="", quoting=csv.QUOTE_NONE) for user in data: save_users(user[0], user[1], user[2]) + else: + print(f"File format {settings.IMPORT_FILE_FORMAT} is not supported.") print(app.User.query.count(), "users added.")