Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: idempotent setup and teardown #104

Merged
merged 4 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import csv
import json

from flask_sqlalchemy import inspect
from eligibility_server import app, settings


Expand All @@ -11,11 +12,15 @@ 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.")


def save_users(user_id: str, key: str, types: str):
Expand All @@ -33,8 +38,16 @@ 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.")
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()
16 changes: 11 additions & 5 deletions teardown.py
Original file line number Diff line number Diff line change
@@ -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.")