Seth commited on
Commit
144794a
·
1 Parent(s): 4a4a980
Files changed (1) hide show
  1. backend/app/database.py +55 -9
backend/app/database.py CHANGED
@@ -1,7 +1,8 @@
1
  import os
2
- from sqlalchemy import create_engine, text
3
  from sqlalchemy.orm import sessionmaker, declarative_base
4
  from sqlalchemy.pool import NullPool
 
5
 
6
  # Get database URL from environment variable
7
  # Default to SQLite for local development if not set
@@ -29,11 +30,32 @@ if DATABASE_URL.startswith("postgresql://") or DATABASE_URL.startswith("postgres
29
  DATABASE_URL = DATABASE_URL.replace("sslmode=verify-full", "sslmode=require")
30
  print("⚠ Certificate file not found, using sslmode=require instead of verify-full")
31
 
 
 
 
 
 
 
 
 
 
 
32
  engine = create_engine(
33
  DATABASE_URL,
34
  poolclass=NullPool, # CockroachDB works better with NullPool
35
- echo=False # Set to True for SQL query debugging
 
36
  )
 
 
 
 
 
 
 
 
 
 
37
  else:
38
  # SQLite for local development
39
  engine = create_engine(
@@ -57,14 +79,38 @@ def init_db():
57
  """Initialize database tables"""
58
  try:
59
  from app.models import User, Integration, Asset, Post, Campaign
60
- # Test connection first
61
- with engine.connect() as conn:
62
- conn.execute(text("SELECT 1"))
63
- conn.commit()
64
- # Create tables if connection successful
 
 
 
 
 
 
 
 
 
 
 
65
  Base.metadata.create_all(bind=engine)
66
  return True
67
  except Exception as e:
68
- print(f"Database connection failed: {e}")
69
- return False
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
 
1
  import os
2
+ from sqlalchemy import create_engine, text, event
3
  from sqlalchemy.orm import sessionmaker, declarative_base
4
  from sqlalchemy.pool import NullPool
5
+ from sqlalchemy.dialects import postgresql
6
 
7
  # Get database URL from environment variable
8
  # Default to SQLite for local development if not set
 
30
  DATABASE_URL = DATABASE_URL.replace("sslmode=verify-full", "sslmode=require")
31
  print("⚠ Certificate file not found, using sslmode=require instead of verify-full")
32
 
33
+ # Configure engine for CockroachDB compatibility
34
+ # CockroachDB uses a different version string format, so we need to handle it
35
+ connect_args = {}
36
+ is_cockroach = "cockroachlabs" in DATABASE_URL.lower()
37
+
38
+ if is_cockroach:
39
+ # For CockroachDB, bypass version string parsing by setting version info
40
+ # This prevents SQLAlchemy from trying to parse CockroachDB's version string
41
+ connect_args["server_version"] = "12.0"
42
+
43
  engine = create_engine(
44
  DATABASE_URL,
45
  poolclass=NullPool, # CockroachDB works better with NullPool
46
+ echo=False, # Set to True for SQL query debugging
47
+ connect_args=connect_args
48
  )
49
+
50
+ # Override version detection for CockroachDB at the dialect level
51
+ if is_cockroach:
52
+ @event.listens_for(engine, "connect", insert=True)
53
+ def receive_connect(dbapi_conn, connection_record):
54
+ # Set version info before SQLAlchemy tries to parse it
55
+ # This happens at connection time
56
+ if hasattr(connection_record, 'info'):
57
+ connection_record.info["server_version_info"] = (12, 0)
58
+ connection_record.info["server_version"] = "12.0"
59
  else:
60
  # SQLite for local development
61
  engine = create_engine(
 
79
  """Initialize database tables"""
80
  try:
81
  from app.models import User, Integration, Asset, Post, Campaign
82
+
83
+ # Test connection and create tables
84
+ # For CockroachDB, version parsing may fail but connection still works
85
+ try:
86
+ with engine.connect() as conn:
87
+ conn.execute(text("SELECT 1"))
88
+ conn.commit()
89
+ except Exception as conn_error:
90
+ error_str = str(conn_error)
91
+ # If it's a version parsing error for CockroachDB, connection still works
92
+ # We can proceed with table creation
93
+ if "Could not determine version" not in error_str:
94
+ # Real connection error, not just version parsing
95
+ raise conn_error
96
+
97
+ # Create tables - this should work even if version parsing failed
98
  Base.metadata.create_all(bind=engine)
99
  return True
100
  except Exception as e:
101
+ error_str = str(e)
102
+ # Check if it's just a version parsing error (non-fatal for CockroachDB)
103
+ if "Could not determine version" in error_str:
104
+ # Version parsing failed, but try to create tables anyway
105
+ # CockroachDB connection works, just version string format is different
106
+ try:
107
+ Base.metadata.create_all(bind=engine)
108
+ print("✓ CockroachDB connected and tables created (version parsing issue ignored)")
109
+ return True
110
+ except Exception as e2:
111
+ print(f"Database table creation failed: {e2}")
112
+ return False
113
+ else:
114
+ print(f"Database connection failed: {e}")
115
+ return False
116