feat: Added database utility functions
This commit is contained in:
@@ -4,16 +4,76 @@ Entrypoint pour lancer l'application en dev.
|
|||||||
Usage:
|
Usage:
|
||||||
python3 -m app
|
python3 -m app
|
||||||
"""
|
"""
|
||||||
|
import argparse
|
||||||
import uvicorn
|
import uvicorn
|
||||||
from app.main import app
|
from app.main import app
|
||||||
from app.core.config import settings
|
from app.core.config import settings
|
||||||
|
|
||||||
|
def build_parser() -> argparse.ArgumentParser:
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="MokPyo API",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Option booléenne (type flag)
|
||||||
|
parser.add_argument(
|
||||||
|
"--init-db",
|
||||||
|
action="store_true",
|
||||||
|
help="Initialiser la base de données. Première exécution uniquement."
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--reset",
|
||||||
|
action="store_true",
|
||||||
|
help="Vide les tables de la base de données (lignes uniquement). Pas besoin de --init-db au prochain lancement."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--clear",
|
||||||
|
action="store_true",
|
||||||
|
help="Vide la base de données (lignes et tables). besoin de --init-db au prochain lancement."
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--host",
|
||||||
|
type=str,
|
||||||
|
default=settings.HOST,
|
||||||
|
help="Host sur lequel l'application va tourner. Override le host de configuration."
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--port",
|
||||||
|
type=int,
|
||||||
|
default=settings.PORT,
|
||||||
|
help="Port sur lequel l'application va tourner. Override le port de configuration."
|
||||||
|
)
|
||||||
|
|
||||||
|
return parser
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
parser = build_parser()
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
if args.init_db:
|
||||||
|
from app.db.utils import init_db
|
||||||
|
init_db()
|
||||||
|
exit()
|
||||||
|
|
||||||
|
if args.reset:
|
||||||
|
from app.db.utils import reset_db
|
||||||
|
reset_db()
|
||||||
|
exit()
|
||||||
|
|
||||||
|
if args.clear:
|
||||||
|
from app.db.utils import clear_db
|
||||||
|
clear_db()
|
||||||
|
exit()
|
||||||
|
|
||||||
uvicorn.run(
|
uvicorn.run(
|
||||||
"app.main:app",
|
"app.main:app",
|
||||||
host=settings.HOST,
|
host=args.host,
|
||||||
port=settings.PORT,
|
port=args.port,
|
||||||
reload=settings.ENV == "dev",
|
reload=settings.ENV == "dev",
|
||||||
log_level=settings.LOG_LEVEL.lower(),
|
log_level=settings.LOG_LEVEL.lower(),
|
||||||
)
|
)
|
||||||
|
|||||||
49
app/db/utils.py
Normal file
49
app/db/utils.py
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
from app.db.engine import engine
|
||||||
|
from app.db.session import SessionLocal
|
||||||
|
from app.db.models import *
|
||||||
|
from app.core.config import settings
|
||||||
|
|
||||||
|
def init_db():
|
||||||
|
try:
|
||||||
|
if settings.DEBUG:
|
||||||
|
print("Creating database tables...")
|
||||||
|
Base.metadata.create_all(bind=engine)
|
||||||
|
if settings.DEBUG:
|
||||||
|
print("Database tables created.")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error creating database tables: {e}")
|
||||||
|
raise
|
||||||
|
finally:
|
||||||
|
if settings.DEBUG:
|
||||||
|
print("Tables :", Base.metadata.tables.keys())
|
||||||
|
|
||||||
|
|
||||||
|
def clear_db():
|
||||||
|
try:
|
||||||
|
if settings.DEBUG:
|
||||||
|
print("Deleting database tables...")
|
||||||
|
Base.metadata.drop_all(bind=engine)
|
||||||
|
if settings.DEBUG:
|
||||||
|
print("Database tables deleted.")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error deleting database tables: {e}")
|
||||||
|
raise
|
||||||
|
|
||||||
|
def reset_db():
|
||||||
|
session = SessionLocal()
|
||||||
|
try:
|
||||||
|
if settings.DEBUG:
|
||||||
|
print("Deleting database rows...")
|
||||||
|
|
||||||
|
for table in reversed(Base.metadata.sorted_tables):
|
||||||
|
session.execute(table.delete())
|
||||||
|
session.commit()
|
||||||
|
|
||||||
|
if settings.DEBUG:
|
||||||
|
print("Database rows deleted.")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error deleting database rows: {e}")
|
||||||
|
raise
|
||||||
|
finally:
|
||||||
|
session.close()
|
||||||
|
|
||||||
Reference in New Issue
Block a user