-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata_import.py
67 lines (57 loc) · 2.21 KB
/
data_import.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# import csv data into our production database
import csv
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.sql import text
from models.PreviousSession import PreviousSession
from models.UpcomingSession import UpcomingSession
from models.SprintSession import SprintSession
db_engine = create_engine(os.getenv("DATABASE_URL"))
db_session = scoped_session(
sessionmaker(autocommit=False, autoflush=False, bind=db_engine)
)
def import_data(file_name, model_class):
"""
Imports data from a CSV file into a database table using a provided model class.
Args:
file_name (str): Path to the CSV file containing the data to import.
model_class (class): The SQLAlchemy model class representing the database table.
"""
with open(file_name, "r") as file:
rows = csv.DictReader(file)
for row in rows:
session_obj = model_class(**row)
db_session.add(session_obj)
db_session.commit()
def confirm_truncate(tables):
"""
Prompts user for confirmation before truncating tables.
Args:
tables: List of table names to be truncated.
Returns:
bool: True if user confirms truncation, False otherwise.
"""
message = "Are you sure you want to truncate the following tables?\n"
for table in tables:
message += f"- {table}\n"
message += "This will delete all existing data in these tables.\n"
response = input(message + "(y/N): ")
return response.lower() == "y"
def main():
tables = ["previous_sessions", "upcoming_sessions", "sprint_sessions"]
if confirm_truncate(tables):
print("Truncating tables...")
for table in tables:
db_session.execute(text(f"TRUNCATE TABLE {table}"))
db_session.commit()
print("Tables truncated.")
print("Importing data into the database...")
import_data("previous_sessions.csv", PreviousSession)
import_data("upcoming_sessions.csv", UpcomingSession)
import_data("sprint_sessions.csv", SprintSession)
print("Data imported successfully.")
else:
print("Truncation cancelled. Data import aborted.")
if __name__ == "__main__":
main()