-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_db.py
More file actions
56 lines (43 loc) · 1.81 KB
/
Copy pathsetup_db.py
File metadata and controls
56 lines (43 loc) · 1.81 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
import os
import sqlite3
from herald.db.models import Base, engine
def migrate_sqlite(db_url):
db_path = db_url.replace("sqlite:///", "")
if not os.path.exists(db_path):
return
print(f"Migrating {db_path}...")
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Check if domain_scans exists
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='domain_scans'")
if not cursor.fetchone():
return
cursor.execute("PRAGMA table_info(domain_scans)")
columns = [row[1] for row in cursor.fetchall()]
if "lifecycle_state" not in columns:
print("Adding lifecycle_state column...")
cursor.execute("ALTER TABLE domain_scans ADD COLUMN lifecycle_state VARCHAR DEFAULT 'QUEUED'")
if "screenshot_path" not in columns:
print("Adding screenshot_path column...")
cursor.execute("ALTER TABLE domain_scans ADD COLUMN screenshot_path VARCHAR")
if "ocr_text" not in columns:
print("Adding ocr_text column...")
cursor.execute("ALTER TABLE domain_scans ADD COLUMN ocr_text VARCHAR")
if "dns_records" not in columns:
print("Adding dns_records column...")
cursor.execute("ALTER TABLE domain_scans ADD COLUMN dns_records VARCHAR")
conn.commit()
conn.close()
def main():
db_url = os.getenv("DATABASE_URL", "sqlite:///domain_history.db")
# Simple migration for SQLite if it's the backend
if db_url.startswith("sqlite"):
try:
migrate_sqlite(db_url)
except Exception as e:
print(f"Warning during migration: {e}")
print("Ensuring all tables and schema exist...")
Base.metadata.create_all(bind=engine)
print("Database initialization complete.")
if __name__ == "__main__":
main()