Skip to content

Commit 1daf4eb

Browse files
committed
better error handling for database
1 parent ff30540 commit 1daf4eb

File tree

3 files changed

+57
-14
lines changed

3 files changed

+57
-14
lines changed

.github/workflows/backend-ci-cd.yml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,33 @@ jobs:
156156
# Pull latest image
157157
docker pull ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ env.IMAGE_TAG }}
158158
159-
# Stop and remove old container (ignore errors if not running)
159+
# Create docker network if it doesn't exist
160+
docker network create college-network 2>/dev/null || true
161+
162+
# Start PostgreSQL if not running
163+
docker run -d \
164+
--name postgres-db \
165+
--network college-network \
166+
--restart unless-stopped \
167+
-e POSTGRES_DB=collegefinder \
168+
-e POSTGRES_USER=postgres \
169+
-e POSTGRES_PASSWORD=postgres \
170+
-v pgdata:/var/lib/postgresql/data \
171+
-p 5432:5432 \
172+
postgres:16-alpine 2>/dev/null || echo "PostgreSQL already running"
173+
174+
# Stop and remove old backend container (ignore errors if not running)
160175
docker stop ${{ env.CONTAINER_NAME }} 2>/dev/null || true
161176
docker remove ${{ env.CONTAINER_NAME }} 2>/dev/null || true
162177
163-
# Start new container
178+
# Start new backend container
164179
docker run -d \
165180
--name ${{ env.CONTAINER_NAME }} \
166-
--network host \
181+
--network college-network \
167182
--restart unless-stopped \
168183
--env-file ${{ env.DEPLOY_DIR }}/.env \
184+
-e POSTGRES_URL=postgresql+asyncpg://postgres:postgres@postgres-db:5432/collegefinder \
185+
-p 8080:8005 \
169186
${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ env.IMAGE_TAG }}
170187
171188
# Remove dangling images to free disk space

apps/backend/app/__init__.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,41 @@ async def lifespan(app: FastAPI):
2121
from pathlib import Path
2222

2323
backend_dir = Path(__file__).parent.parent # /app in container
24-
result = subprocess.run(
25-
[sys.executable, "-m", "alembic", "upgrade", "head"],
26-
capture_output=True, text=True, cwd=str(backend_dir)
27-
)
28-
if result.returncode != 0:
29-
print(f"[ALEMBIC] Migration warning: {result.stderr}")
30-
else:
31-
print(f"[ALEMBIC] {result.stdout.strip() or 'Migrations up to date'}")
32-
33-
await init_db() # create_all for any tables not covered by migrations
24+
25+
# Try to run migrations, but don't fail if tables already exist
26+
try:
27+
result = subprocess.run(
28+
[sys.executable, "-m", "alembic", "upgrade", "head"],
29+
capture_output=True, text=True, cwd=str(backend_dir)
30+
)
31+
if result.returncode != 0:
32+
# Check if it's just a "tables already exist" error
33+
if "already exists" in result.stderr:
34+
print(f"[ALEMBIC] Tables already exist, marking migrations as applied...")
35+
# Mark the current migration as applied without running it
36+
subprocess.run(
37+
[sys.executable, "-m", "alembic", "stamp", "head"],
38+
capture_output=True, text=True, cwd=str(backend_dir)
39+
)
40+
print(f"[ALEMBIC] Migrations marked as applied")
41+
else:
42+
print(f"[ALEMBIC] Migration warning: {result.stderr}")
43+
else:
44+
print(f"[ALEMBIC] {result.stdout.strip() or 'Migrations up to date'}")
45+
except Exception as e:
46+
print(f"[ALEMBIC] Migration error (continuing anyway): {e}")
47+
48+
try:
49+
await init_db() # create_all for any tables not covered by migrations
50+
except Exception as e:
51+
print(f"[DB] Database initialization error: {e}")
52+
3453
yield
35-
await close_db()
54+
55+
try:
56+
await close_db()
57+
except Exception as e:
58+
print(f"[DB] Database cleanup error: {e}")
3659

3760

3861
def create_app() -> FastAPI:

apps/backend/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
load_dotenv(find_dotenv())
88

9+
# Import the FastAPI app for uvicorn to find
10+
from app import app
11+
912
if __name__ == "__main__":
1013
import uvicorn
1114
uvicorn.run("main:app", port=8005, reload=True)

0 commit comments

Comments
 (0)