Spaces:
Sleeping
Sleeping
fix: lazy-initialize DB engine in standalone scripts
Browse filesReplace direct imports of `engine` and `SessionLocal` from `app.db.session`
with local instantiation via `get_database_url()` in create_db, insert_data,
and query_db scripts. Prevents DB connection attempts at import time when
environment variables are not yet loaded.
- scripts/create_db.py +3 -1
- scripts/insert_data.py +5 -1
- scripts/query_db.py +5 -1
scripts/create_db.py
CHANGED
|
@@ -7,9 +7,11 @@ from dotenv import load_dotenv
|
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
from sqlalchemy import create_engine, text
|
| 10 |
-
from app.db.session import
|
| 11 |
from app.db.models import Base
|
| 12 |
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def create_database_if_not_exists():
|
| 15 |
db_name = os.getenv("DB_NAME")
|
|
|
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
from sqlalchemy import create_engine, text
|
| 10 |
+
from app.db.session import get_database_url
|
| 11 |
from app.db.models import Base
|
| 12 |
|
| 13 |
+
engine = create_engine(get_database_url())
|
| 14 |
+
|
| 15 |
|
| 16 |
def create_database_if_not_exists():
|
| 17 |
db_name = os.getenv("DB_NAME")
|
scripts/insert_data.py
CHANGED
|
@@ -7,7 +7,11 @@ from dotenv import load_dotenv
|
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
import pandas as pd
|
| 10 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
from app.db.models import Employee
|
| 12 |
from typing import cast, Any
|
| 13 |
|
|
|
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
import pandas as pd
|
| 10 |
+
from sqlalchemy.orm import sessionmaker
|
| 11 |
+
from sqlalchemy import create_engine
|
| 12 |
+
from app.db.session import get_database_url
|
| 13 |
+
|
| 14 |
+
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=create_engine(get_database_url()))
|
| 15 |
from app.db.models import Employee
|
| 16 |
from typing import cast, Any
|
| 17 |
|
scripts/query_db.py
CHANGED
|
@@ -7,7 +7,11 @@ from dotenv import load_dotenv
|
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
import pandas as pd
|
| 10 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
from app.db.models import Employee, Prediction
|
| 12 |
from app.routes.predict import _compute_features
|
| 13 |
from ml_model.loader import load_pipeline
|
|
|
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
import pandas as pd
|
| 10 |
+
from sqlalchemy.orm import sessionmaker
|
| 11 |
+
from sqlalchemy import create_engine
|
| 12 |
+
from app.db.session import get_database_url
|
| 13 |
+
|
| 14 |
+
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=create_engine(get_database_url()))
|
| 15 |
from app.db.models import Employee, Prediction
|
| 16 |
from app.routes.predict import _compute_features
|
| 17 |
from ml_model.loader import load_pipeline
|