Skip to content

Commit c3c0398

Browse files
committed
feat: add PostgreSQL availability checks and environment variables for dual-write and sync services
1 parent fed055c commit c3c0398

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ jobs:
1414
ALLOWED_HOSTS: "localhost,127.0.0.1"
1515
MONGODB_URI: mongodb://db:27017
1616
DB_NAME: todo-app
17+
POSTGRES_SYNC_ENABLED: "False"
18+
DUAL_WRITE_ENABLED: "False"
1719
GOOGLE_OAUTH_CLIENT_ID: "test-client-id"
1820
GOOGLE_OAUTH_CLIENT_SECRET: "test-client-secret"
1921
GOOGLE_OAUTH_REDIRECT_URI: "http://localhost:8000/v1/auth/google/callback"

todo/services/dual_write_service.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ class DualWriteService:
4444

4545
def __init__(self):
4646
self.sync_failures = []
47+
try:
48+
from django.db import connection
49+
50+
connection.ensure_connection()
51+
self.postgres_available = True
52+
except Exception:
53+
self.postgres_available = False
4754

4855
def create_document(self, collection_name: str, data: Dict[str, Any], mongo_id: str) -> bool:
4956
"""
@@ -58,6 +65,11 @@ def create_document(self, collection_name: str, data: Dict[str, Any], mongo_id:
5865
bool: True if both writes succeeded, False otherwise
5966
"""
6067
try:
68+
# Check if PostgreSQL is available
69+
if not self.postgres_available:
70+
logger.debug("PostgreSQL not available, skipping Postgres sync")
71+
return True
72+
6173
# First, write to MongoDB (this should already be done by the calling code)
6274
# Then, write to Postgres
6375
postgres_model = self._get_postgres_model(collection_name)

todo/services/postgres_sync_service.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ def sync_all_tables(self) -> bool:
3030
if not self.enabled:
3131
logger.info("PostgreSQL sync is disabled, skipping")
3232
return True
33+
try:
34+
from django.db import connection
35+
36+
connection.ensure_connection()
37+
except Exception as e:
38+
logger.warning(f"PostgreSQL not available, skipping sync: {str(e)}")
39+
return True
3340

3441
logger.info("Starting PostgreSQL table synchronization for labels and roles")
3542
logger.info(f"PostgreSQL sync enabled: {self.enabled}")
@@ -151,7 +158,11 @@ def _sync_labels_table(self) -> bool:
151158
for label in labels:
152159
try:
153160
# Check if label already exists in PostgreSQL
154-
from todo.models.postgres.label import PostgresLabel
161+
try:
162+
from todo.models.postgres.label import PostgresLabel
163+
except Exception:
164+
logger.warning("PostgreSQL models not available, skipping sync")
165+
return False
155166

156167
existing = PostgresLabel.objects.filter(mongo_id=str(label["_id"])).first()
157168
if existing:
@@ -203,7 +214,11 @@ def _sync_roles_table(self) -> bool:
203214
synced_count = 0
204215
for role in roles:
205216
try:
206-
from todo.models.postgres.role import PostgresRole
217+
try:
218+
from todo.models.postgres.role import PostgresRole
219+
except Exception:
220+
logger.warning("PostgreSQL models not available, skipping sync")
221+
return False
207222

208223
existing = PostgresRole.objects.filter(mongo_id=str(role["_id"])).first()
209224
if existing:

todo_project/settings/test.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from .base import *
2+
import os
23

34
DUAL_WRITE_ENABLED = False
5+
POSTGRES_SYNC_ENABLED = False
46

57
# Remove PostgreSQL database configuration for tests
6-
# This prevents Django from trying to connect to PostgreSQL
78
DATABASES = {}
89

9-
# Use MongoDB only for tests
10-
# The tests will use testcontainers to spin up their own MongoDB instance
10+
11+
os.environ.setdefault("POSTGRES_SYNC_ENABLED", "False")
12+
os.environ.setdefault("DUAL_WRITE_ENABLED", "False")

0 commit comments

Comments
 (0)