Repository navigation
Expand file tree
/
Copy pathdatabase.py
More file actions
115 lines (100 loc) · 3.64 KB
/
Copy pathdatabase.py
File metadata and controls
115 lines (100 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from sqlalchemy import create_engine, text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from urllib.parse import quote_plus
import logging
from contextlib import contextmanager
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
logger = logging.getLogger(__name__)
try:
# Get the database URL from environment variables
DATABASE_URL = os.getenv("DATABASE_URL")
if not DATABASE_URL:
raise ValueError("DATABASE_URL environment variable not set")
logger.info("Database URL configured")
engine = create_engine(DATABASE_URL)
logger.info("Database engine created")
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
logger.info("Session maker configured")
Base = declarative_base()
logger.info("Base class created")
except Exception as e:
logger.error(f"Error configuring database: {str(e)}")
raise
def get_db():
"""Get database session"""
db = SessionLocal()
try:
yield db
except Exception as e:
logger.error(f"Error in database session: {str(e)}")
db.rollback()
raise
finally:
db.close()
def modify_username_column():
"""Make username column nullable"""
try:
with engine.connect() as conn:
# Check if username column exists
result = conn.execute(text("""
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'users' AND column_name = 'username'
"""))
if result.fetchone():
# Make username column nullable
conn.execute(text("""
ALTER TABLE users
ALTER COLUMN username DROP NOT NULL
"""))
logger.info("Made username column nullable")
conn.commit()
except Exception as e:
logger.error(f"Error modifying username column: {str(e)}")
raise
def add_timestamp_columns():
"""Safely add timestamp columns if they don't exist"""
try:
with engine.connect() as conn:
# Check if created_at exists
result = conn.execute(text("""
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'users' AND column_name = 'created_at'
"""))
if not result.fetchone():
conn.execute(text("""
ALTER TABLE users
ADD COLUMN created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
"""))
logger.info("Added created_at column")
# Check if updated_at exists
result = conn.execute(text("""
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'users' AND column_name = 'updated_at'
"""))
if not result.fetchone():
conn.execute(text("""
ALTER TABLE users
ADD COLUMN updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
"""))
logger.info("Added updated_at column")
conn.commit()
except Exception as e:
logger.error(f"Error adding timestamp columns: {str(e)}")
raise
# Make username column nullable
try:
modify_username_column()
except Exception as e:
logger.error(f"Error in modify_username_column: {str(e)}")
# Add timestamp columns if they don't exist
try:
add_timestamp_columns()
except Exception as e:
logger.error(f"Error in add_timestamp_columns: {str(e)}")