-
Notifications
You must be signed in to change notification settings - Fork 15
/
migrate.py
86 lines (69 loc) · 2.61 KB
/
migrate.py
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
import psycopg2
from dotenv import load_dotenv
import os
import sys
import logging
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Load environment variables from .env
load_dotenv()
# Fetch the SUPABASE_DB_URL from environment variables
DATABASE_URL = os.getenv("SUPABASE_DB_URL")
# Debugging: Ensure DATABASE_URL is loaded (avoid printing sensitive information)
if DATABASE_URL:
logging.info("SUPABASE_DB_URL loaded successfully.")
else:
logging.error("SUPABASE_DB_URL is not set. Please check your .env file.")
sys.exit(1) # Exit the script with a non-zero status
# Define the migration SQL
CREATE_TABLE_SQL = """
CREATE TABLE IF NOT EXISTS devcontainers (
id INTEGER NOT NULL,
url VARCHAR,
devcontainer_json TEXT,
devcontainer_url VARCHAR,
repo_context TEXT,
tokens INTEGER,
model TEXT,
embedding TEXT,
generated BOOLEAN,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
PRIMARY KEY (id)
);
"""
def main():
connection = None # Initialize connection variable
try:
# Connect to the PostgreSQL database using the connection string
connection = psycopg2.connect(DATABASE_URL)
connection.autocommit = True # Enable autocommit mode
logging.info("Connection to the database was successful.")
# Create a cursor to execute SQL queries
cursor = connection.cursor()
# Execute the CREATE TABLE statement
cursor.execute(CREATE_TABLE_SQL)
logging.info("devcontainers table created (if it didn't exist already).")
# Optionally, verify the table creation
cursor.execute("""
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public' AND table_type = 'BASE TABLE';
""")
tables = cursor.fetchall()
logging.info(f"Current tables in 'public' schema: {tables}")
# Close the cursor
cursor.close()
except psycopg2.OperationalError as e:
logging.error("OperationalError: Could not connect to the database.")
logging.error("Please check your SUPABASE_DB_URL and ensure the database server is running.")
logging.error(f"Details: {e}")
sys.exit(1) # Exit the script with a non-zero status
except Exception as e:
logging.error(f"Error running migration: {e}")
sys.exit(1) # Exit the script with a non-zero status
finally:
if connection:
connection.close() # Close the connection if it was established
logging.info("Database connection closed.")
if __name__ == "__main__":
main()