Spaces:
Sleeping
Sleeping
Seth
commited on
Commit
·
27fb040
1
Parent(s):
df2dd5d
update
Browse files- backend/app/database.py +64 -0
- backend/app/main.py +14 -4
backend/app/database.py
CHANGED
|
@@ -111,6 +111,70 @@ def get_direct_psycopg2_connection():
|
|
| 111 |
print(f"Failed to create direct psycopg2 connection: {e}")
|
| 112 |
return None
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
def init_db():
|
| 115 |
"""Initialize database tables"""
|
| 116 |
try:
|
|
|
|
| 111 |
print(f"Failed to create direct psycopg2 connection: {e}")
|
| 112 |
return None
|
| 113 |
|
| 114 |
+
def ensure_default_user():
|
| 115 |
+
"""Ensure a default user (id=1) exists in the database"""
|
| 116 |
+
try:
|
| 117 |
+
conn = get_direct_psycopg2_connection()
|
| 118 |
+
if not conn:
|
| 119 |
+
return 1 # Return default ID if connection fails
|
| 120 |
+
|
| 121 |
+
try:
|
| 122 |
+
cursor = conn.cursor()
|
| 123 |
+
# Check if user with id=1 exists
|
| 124 |
+
cursor.execute("SELECT id FROM users WHERE id = 1")
|
| 125 |
+
if cursor.fetchone():
|
| 126 |
+
cursor.close()
|
| 127 |
+
conn.close()
|
| 128 |
+
return 1
|
| 129 |
+
|
| 130 |
+
# Create default user if it doesn't exist
|
| 131 |
+
# Try with ON CONFLICT first (PostgreSQL/CockroachDB)
|
| 132 |
+
try:
|
| 133 |
+
cursor.execute("""
|
| 134 |
+
INSERT INTO users (id, email, name, created_at)
|
| 135 |
+
VALUES (1, 'default@postgen.app', 'Default User', NOW())
|
| 136 |
+
ON CONFLICT (id) DO NOTHING
|
| 137 |
+
""")
|
| 138 |
+
except Exception:
|
| 139 |
+
# If ON CONFLICT fails, try without it (might be unique constraint on email)
|
| 140 |
+
try:
|
| 141 |
+
cursor.execute("""
|
| 142 |
+
INSERT INTO users (id, email, name, created_at)
|
| 143 |
+
VALUES (1, 'default@postgen.app', 'Default User', NOW())
|
| 144 |
+
""")
|
| 145 |
+
except Exception as insert_error:
|
| 146 |
+
# User might already exist (race condition), check again
|
| 147 |
+
cursor.execute("SELECT id FROM users WHERE id = 1 OR email = 'default@postgen.app' LIMIT 1")
|
| 148 |
+
row = cursor.fetchone()
|
| 149 |
+
if row:
|
| 150 |
+
cursor.close()
|
| 151 |
+
conn.close()
|
| 152 |
+
return row[0]
|
| 153 |
+
# If still fails, re-raise
|
| 154 |
+
raise insert_error
|
| 155 |
+
|
| 156 |
+
conn.commit()
|
| 157 |
+
cursor.close()
|
| 158 |
+
conn.close()
|
| 159 |
+
return 1
|
| 160 |
+
except Exception as e:
|
| 161 |
+
# If everything fails, try to get any existing user or return default
|
| 162 |
+
try:
|
| 163 |
+
cursor.execute("SELECT id FROM users LIMIT 1")
|
| 164 |
+
row = cursor.fetchone()
|
| 165 |
+
cursor.close()
|
| 166 |
+
conn.close()
|
| 167 |
+
if row:
|
| 168 |
+
return row[0]
|
| 169 |
+
except:
|
| 170 |
+
if conn:
|
| 171 |
+
conn.close()
|
| 172 |
+
print(f"Warning: Could not ensure default user: {e}")
|
| 173 |
+
return 1 # Return default ID as fallback
|
| 174 |
+
except Exception as e:
|
| 175 |
+
print(f"Error ensuring default user: {e}")
|
| 176 |
+
return 1 # Return default ID as fallback
|
| 177 |
+
|
| 178 |
def init_db():
|
| 179 |
"""Initialize database tables"""
|
| 180 |
try:
|
backend/app/main.py
CHANGED
|
@@ -16,7 +16,7 @@ from app.schemas import (
|
|
| 16 |
from app.services.canva_service import CanvaService
|
| 17 |
from app.services.linkedin_service import LinkedInService
|
| 18 |
from app.services.ai_service import AIService
|
| 19 |
-
from app.database import init_db, get_db, get_direct_psycopg2_connection
|
| 20 |
from sqlalchemy.orm import Session
|
| 21 |
|
| 22 |
app = FastAPI(title="PostGen API", version="1.0.0")
|
|
@@ -41,6 +41,12 @@ async def startup_event():
|
|
| 41 |
db_initialized = init_db()
|
| 42 |
if db_initialized:
|
| 43 |
print("✓ Database initialized successfully")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
else:
|
| 45 |
print("⚠ Database not available - using mock data")
|
| 46 |
print("⚠ App will function normally with dummy content")
|
|
@@ -186,7 +192,9 @@ async def upload_asset(
|
|
| 186 |
# Save to database (keep dummy content as requested)
|
| 187 |
try:
|
| 188 |
from app.models import Asset
|
| 189 |
-
#
|
|
|
|
|
|
|
| 190 |
db_asset = Asset(
|
| 191 |
name=file.filename, # Keep original filename for display
|
| 192 |
file_path=str(file_path), # Store unique filename on disk
|
|
@@ -194,7 +202,7 @@ async def upload_asset(
|
|
| 194 |
product_category=product_category or "ocr",
|
| 195 |
sub_category=sub_category if sub_category and sub_category != "none" else None,
|
| 196 |
size=file_size,
|
| 197 |
-
user_id=
|
| 198 |
)
|
| 199 |
db.add(db_asset)
|
| 200 |
try:
|
|
@@ -234,6 +242,8 @@ async def upload_asset(
|
|
| 234 |
error_str = str(commit_error)
|
| 235 |
if "Could not determine version" in error_str:
|
| 236 |
# Use direct psycopg2 connection to bypass SQLAlchemy
|
|
|
|
|
|
|
| 237 |
conn = get_direct_psycopg2_connection()
|
| 238 |
if conn:
|
| 239 |
try:
|
|
@@ -249,7 +259,7 @@ async def upload_asset(
|
|
| 249 |
product_category or "ocr",
|
| 250 |
sub_category if sub_category and sub_category != "none" else None,
|
| 251 |
file_size,
|
| 252 |
-
|
| 253 |
))
|
| 254 |
row = cursor.fetchone()
|
| 255 |
conn.commit()
|
|
|
|
| 16 |
from app.services.canva_service import CanvaService
|
| 17 |
from app.services.linkedin_service import LinkedInService
|
| 18 |
from app.services.ai_service import AIService
|
| 19 |
+
from app.database import init_db, get_db, get_direct_psycopg2_connection, ensure_default_user
|
| 20 |
from sqlalchemy.orm import Session
|
| 21 |
|
| 22 |
app = FastAPI(title="PostGen API", version="1.0.0")
|
|
|
|
| 41 |
db_initialized = init_db()
|
| 42 |
if db_initialized:
|
| 43 |
print("✓ Database initialized successfully")
|
| 44 |
+
# Ensure default user exists
|
| 45 |
+
try:
|
| 46 |
+
user_id = ensure_default_user()
|
| 47 |
+
print(f"✓ Default user ready (id={user_id})")
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"⚠ Could not ensure default user: {e}")
|
| 50 |
else:
|
| 51 |
print("⚠ Database not available - using mock data")
|
| 52 |
print("⚠ App will function normally with dummy content")
|
|
|
|
| 192 |
# Save to database (keep dummy content as requested)
|
| 193 |
try:
|
| 194 |
from app.models import Asset
|
| 195 |
+
# Ensure default user exists and get user_id
|
| 196 |
+
user_id = ensure_default_user()
|
| 197 |
+
|
| 198 |
db_asset = Asset(
|
| 199 |
name=file.filename, # Keep original filename for display
|
| 200 |
file_path=str(file_path), # Store unique filename on disk
|
|
|
|
| 202 |
product_category=product_category or "ocr",
|
| 203 |
sub_category=sub_category if sub_category and sub_category != "none" else None,
|
| 204 |
size=file_size,
|
| 205 |
+
user_id=user_id
|
| 206 |
)
|
| 207 |
db.add(db_asset)
|
| 208 |
try:
|
|
|
|
| 242 |
error_str = str(commit_error)
|
| 243 |
if "Could not determine version" in error_str:
|
| 244 |
# Use direct psycopg2 connection to bypass SQLAlchemy
|
| 245 |
+
# Ensure default user exists first
|
| 246 |
+
user_id = ensure_default_user()
|
| 247 |
conn = get_direct_psycopg2_connection()
|
| 248 |
if conn:
|
| 249 |
try:
|
|
|
|
| 259 |
product_category or "ocr",
|
| 260 |
sub_category if sub_category and sub_category != "none" else None,
|
| 261 |
file_size,
|
| 262 |
+
user_id
|
| 263 |
))
|
| 264 |
row = cursor.fetchone()
|
| 265 |
conn.commit()
|