mirror of
https://github.com/R0m1k3/Priceflow.git
synced 2026-10-12 01:39:25 +02:00
fix: Improve database migrations - create table if missing
- Create search_sites table if it doesn't exist - Add price_selector column only if table exists but column doesn't - Fixes 400 Bad Request when creating search sites
This commit is contained in:
1 file changed
+32
-10
+32
-10
@@ -25,21 +25,43 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def run_migrations():
|
||||
"""Run database migrations for missing columns"""
|
||||
migrations = [
|
||||
# Add price_selector to search_sites if missing
|
||||
("search_sites", "price_selector", "ALTER TABLE search_sites ADD COLUMN price_selector VARCHAR(512)"),
|
||||
]
|
||||
|
||||
"""Run database migrations for missing tables and columns"""
|
||||
with engine.connect() as conn:
|
||||
for table, column, sql in migrations:
|
||||
# 1. Create search_sites table if it doesn't exist
|
||||
result = conn.execute(text(
|
||||
"SELECT 1 FROM information_schema.tables WHERE table_name = 'search_sites'"
|
||||
))
|
||||
if not result.fetchone():
|
||||
logger.info("Creating search_sites table...")
|
||||
conn.execute(text("""
|
||||
CREATE TABLE search_sites (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
domain VARCHAR(512) UNIQUE NOT NULL,
|
||||
logo_url VARCHAR(1024),
|
||||
category VARCHAR(255),
|
||||
is_active BOOLEAN DEFAULT TRUE,
|
||||
priority INTEGER DEFAULT 0,
|
||||
requires_js BOOLEAN DEFAULT FALSE,
|
||||
price_selector VARCHAR(512),
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
"""))
|
||||
conn.execute(text("CREATE INDEX IF NOT EXISTS idx_search_sites_domain ON search_sites(domain)"))
|
||||
conn.execute(text("CREATE INDEX IF NOT EXISTS idx_search_sites_is_active ON search_sites(is_active)"))
|
||||
conn.commit()
|
||||
logger.info("search_sites table created")
|
||||
else:
|
||||
# 2. Add price_selector column if missing
|
||||
result = conn.execute(text(
|
||||
f"SELECT 1 FROM information_schema.columns WHERE table_name = '{table}' AND column_name = '{column}'"
|
||||
"SELECT 1 FROM information_schema.columns WHERE table_name = 'search_sites' AND column_name = 'price_selector'"
|
||||
))
|
||||
if not result.fetchone():
|
||||
logger.info(f"Adding missing column: {table}.{column}")
|
||||
conn.execute(text(sql))
|
||||
logger.info("Adding price_selector column to search_sites...")
|
||||
conn.execute(text("ALTER TABLE search_sites ADD COLUMN price_selector VARCHAR(512)"))
|
||||
conn.commit()
|
||||
logger.info("price_selector column added")
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
|
||||
Reference in new issue
Block a user