forked from langchain-ai/opengpts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Lambda function to trigger initial SQL scripts (#2)
* task: New lambda function * task: Git ignore * task: Git ignore fix * task: Added Lambda function to trigger Postgres setup * fix: Unnecessary file
- Loading branch information
1 parent
216736c
commit 27fdd47
Showing
13 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,3 +62,7 @@ npm-debug.log* | |
yarn-debug.log* | ||
yarn-error.log* | ||
pnpm-debug.log* | ||
|
||
# Temp Lambda files: | ||
backend/lambda_* | ||
lambda_*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
psycopg2-binary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import os | ||
import psycopg2 | ||
from psycopg2 import sql | ||
|
||
def run_migrations(): | ||
# Database connection parameters | ||
db_params = { | ||
"host": os.environ['POSTGRES_HOST'], | ||
"port": os.environ['POSTGRES_PORT'], | ||
"dbname": os.environ['POSTGRES_DB'], | ||
"user": os.environ['POSTGRES_USER'], | ||
"password": os.environ['POSTGRES_PASSWORD'] | ||
} | ||
|
||
# Establish a connection to the database | ||
conn = psycopg2.connect(**db_params) | ||
conn.autocommit = True | ||
cursor = conn.cursor() | ||
|
||
# Path to the migration scripts | ||
migrations_path = 'code/migrations' | ||
|
||
# Execute each migration script in the migrations directory | ||
for root, dirs, files in os.walk(migrations_path): | ||
for filename in sorted(files): | ||
if filename.endswith('.sql'): | ||
print(f"[INFO] Executing {filename}") | ||
file_path = os.path.join(root, filename) | ||
with open(file_path, 'r') as file: | ||
sql_script = file.read() | ||
print(f"[INFO] Running script: {sql_script}") | ||
try: | ||
cursor.execute(sql.SQL(sql_script)) | ||
print(f"[INFO] Executed {filename} successfully.") | ||
|
||
if cursor.description is not None: | ||
results = cursor.fetchall() | ||
for row in results: | ||
print(row) | ||
except Exception as script_error: | ||
print(f"Error executing {filename}: {script_error}") | ||
|
||
cursor.close() | ||
conn.close() | ||
|
||
def lambda_handler(event, context): | ||
try: | ||
run_migrations() | ||
return { | ||
'statusCode': 200, | ||
'body': 'Migrations executed successfully.' | ||
} | ||
except Exception as e: | ||
return { | ||
'statusCode': 500, | ||
'body': f'Error executing migrations: {str(e)}' | ||
} |