-
Notifications
You must be signed in to change notification settings - Fork 22
Perform lightspeed-stack migrations #235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| """ | ||
| This script connects to a PostgreSQL database and performs migrations. | ||
|
|
||
| This is because lightspeed-stack does not currently perform migrations on its own, | ||
| which means the database either has to be created from scratch or migrated like we do here. | ||
|
|
||
| Any migrations added should be idempotent, meaning they can be ran multiple times without | ||
| causing errors or unintended effects. This is because we run this script every time the | ||
| service starts to ensure the database is up to date. | ||
|
|
||
| Currently the migrations are as follows: | ||
|
|
||
| 1. Add a new column `topic_summary` as it was added in lightspeed-stack v0.3.0 | ||
|
|
||
| WARNING: This script assumes that the database is postgres and that the schema used is | ||
| called `lightspeed-stack`. If either of these assumptions are incorrect, the script may fail | ||
| or cause unintended effects. lightspeed-stack could also use sqlite or a different schema | ||
| if configured to do so, but we don't handle those cases here because we don't use them. | ||
| """ | ||
|
|
||
| import os | ||
| import time | ||
| import sys | ||
|
|
||
| import psycopg2 | ||
|
|
||
| for _ in range(30): | ||
| try: | ||
| conn = psycopg2.connect( | ||
| host=os.getenv("ASSISTED_CHAT_POSTGRES_HOST"), | ||
| port=os.getenv("ASSISTED_CHAT_POSTGRES_PORT"), | ||
| dbname=os.getenv("ASSISTED_CHAT_POSTGRES_NAME"), | ||
| user=os.getenv("ASSISTED_CHAT_POSTGRES_USER"), | ||
| password=os.getenv("ASSISTED_CHAT_POSTGRES_PASSWORD"), | ||
| sslmode=os.getenv("LIGHTSPEED_STACK_POSTGRES_SSL_MODE"), | ||
| ) | ||
| break | ||
| except psycopg2.OperationalError as e: | ||
| print("Waiting for Postgres...", e, file=sys.stderr) | ||
| time.sleep(2) | ||
| else: | ||
| sys.exit("Postgres not available after 60s") | ||
|
|
||
|
|
||
| # Ensure the schema even exists, if it doesn't, it's a fresh database and | ||
| # we don't need to run migrations | ||
| with conn.cursor() as cur: | ||
| cur.execute( | ||
| "SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'lightspeed-stack'" | ||
| ) | ||
| if not cur.fetchone(): | ||
| print( | ||
| "Schema 'lightspeed-stack' absent, database probably fresh, skipping migrations" | ||
| ) | ||
| conn.close() | ||
| sys.exit(0) | ||
|
|
||
|
|
||
| cur = conn.cursor() | ||
| cur.execute( | ||
| 'ALTER TABLE "lightspeed-stack"."user_conversation" ADD COLUMN IF NOT EXISTS topic_summary text' | ||
| ) | ||
| conn.commit() | ||
| cur.close() | ||
| conn.close() | ||
omertuc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| print("Migration completed") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,13 +24,25 @@ else | |
| fi | ||
|
|
||
| set -a && source "$PROJECT_ROOT/.env" && set +a | ||
| set -a && source "$PROJECT_ROOT/template-params.dev.env" && set +a | ||
| export LIGHTSPEED_STACK_IMAGE_OVERRIDE="${LIGHTSPEED_STACK_IMAGE_OVERRIDE:-localhost/local-ai-chat-lightspeed-stack-plus-llama-stack}" | ||
|
|
||
| # Validate and export OCM tokens for use in pod configuration | ||
| if ! export_ocm_token; then | ||
| echo "Failed to get OCM tokens. The UI container will not be able to authenticate with OCM." | ||
| exit 1 | ||
| fi | ||
| podman play kube --build=false <(envsubst < "$PROJECT_ROOT"/assisted-chat-pod.yaml) | ||
|
|
||
| # This is conditional because it's super slow for some reason. If the user | ||
| # doesn't have a hostPath volume for pgdata, we don't need it anyway | ||
| if <"$PROJECT_ROOT/assisted-chat-pod.yaml" yq | jq '.spec.volumes[] | select(.name == "pgdata").hostPath != null' --exit-status; then | ||
| # Map the PostgreSQL user (UID 26) inside the container to the current host user | ||
| # This allows the PostgreSQL container to write to host-mounted volumes without permission issues | ||
| POSTGRES_USER_ID=26 | ||
| POSTGRES_GROUP_ID=26 | ||
| podman play kube --build=false --userns=keep-id:uid=$POSTGRES_USER_ID,gid=$POSTGRES_GROUP_ID <(envsubst <"$PROJECT_ROOT"/assisted-chat-pod.yaml) | ||
| else | ||
| podman play kube --build=false <(envsubst <"$PROJECT_ROOT"/assisted-chat-pod.yaml) | ||
| fi | ||
omertuc marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+38
to
+46
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try to replace this with: in the assisted-chat-pod.yaml
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK tracking in #237
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't help |
||
|
|
||
| "$SCRIPT_DIR/logs.sh" | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider security implications of exposing PostgreSQL port.
Exposing PostgreSQL on
hostPort: 5432makes the database accessible from the host network. This is acceptable for local development but ensure this configuration is not used in production or shared environments.🤖 Prompt for AI Agents