Dockerized everything and added CI/CD deployment

This commit is contained in:
c-d-p
2025-04-22 22:54:31 +02:00
parent bf147af3ef
commit 02d191853b
17 changed files with 434 additions and 71 deletions

View File

@@ -6,7 +6,7 @@ from sqlalchemy.orm import Session
from core.database import Base, get_db
from modules.auth.models import User, UserRole
from modules.auth.dependencies import admin_only
from .tasks import cleardb
router = APIRouter(prefix="/admin", tags=["admin"], dependencies=[Depends(admin_only)])
@@ -27,20 +27,5 @@ def clear_db(payload: ClearDbRequest, db: Annotated[Session, Depends(get_db)]):
'hard'=False: Delete data from tables except users.
"""
hard = payload.hard # Get 'hard' from the payload
if hard:
# ... existing hard clear logic ...
Base.metadata.drop_all(bind=db.get_bind())
Base.metadata.create_all(bind=db.get_bind())
db.commit()
return {"message": "Database reset (HARD)"}
else:
# ... existing soft clear logic ...
tables = Base.metadata.tables.keys()
for table_name in tables:
# delete all tables that isn't the users table
if table_name != "users":
table = Base.metadata.tables[table_name]
print(f"Deleting table: {table_name}")
db.execute(table.delete())
db.commit()
return {"message": "Database cleared"}
cleardb.delay(hard)
return {"message": "Clearing database in the background", "hard": hard}

View File

@@ -0,0 +1,35 @@
from core.celery_app import celery_app
@celery_app.task
def cleardb(hard: bool):
"""
Clear the database based on the 'hard' flag.
'hard'=True: Drop and recreate all tables.
'hard'=False: Delete data from tables except users.
"""
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from core.config import settings
from core.database import Base
engine = create_engine(settings.DB_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
db = SessionLocal()
if hard:
# Drop and recreate all tables
Base.metadata.drop_all(bind=engine)
Base.metadata.create_all(bind=engine)
db.commit()
return {"message": "Database reset (HARD)"}
else:
# Delete data from tables except users
tables = Base.metadata.tables.keys()
for table_name in tables:
# delete all tables that isn't the users table
if table_name != "users":
table = Base.metadata.tables[table_name]
print(f"Deleting table: {table_name}")
db.execute(table.delete())
db.commit()
return {"message": "Database cleared"}