Skip to content

Commit

Permalink
chore: adding migrations scripts for v2
Browse files Browse the repository at this point in the history
  • Loading branch information
witash committed Feb 18, 2025
1 parent 0be1d02 commit e1677e0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 47 deletions.
17 changes: 17 additions & 0 deletions deploy/scripts/upgrade/add-manifest-and-source.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

# Source the migration template
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
source "$SCRIPT_DIR/lib/migration.sh"

# SQL statements to execute
SQL_STATEMENTS=(
"ALTER TABLE _dataemon ADD COLUMN IF NOT EXISTS manifest jsonb;"
"ALTER TABLE couchdb ADD COLUMN IF NOT EXISTS source varchar;"
"CREATE INDEX IF NOT EXISTS source ON couchdb(source);"
)

# Execute each SQL statement
for sql in "${SQL_STATEMENTS[@]}"; do
execute_sql "$sql"
done
53 changes: 53 additions & 0 deletions deploy/scripts/upgrade/lib/migration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash

# Check for required arguments
if [ "$#" -lt 3 ] || { [ "$1" == "kubectl" ] && [ "$#" -ne 4 ]; }; then
echo "Usage: $0 <kubectl|docker> <username> <dbname> [namespace]"
exit 1
fi

# Assign arguments to variables
ENVIRONMENT=$1
USERNAME=$2
DBNAME=$3

# Function to execute SQL statement
execute_sql() {
local sql_statement=$1

if [ "$ENVIRONMENT" == "docker" ]; then
# Find the container ID or name automatically
CONTAINER_ID=$(docker ps --filter "name=postgres" --format "{{.ID}}" | head -n 1)

if [ -z "$CONTAINER_ID" ]; then
echo "No running Postgres container found."
exit 1
fi

# Run the SQL statement using docker exec
docker exec "$CONTAINER_ID" psql -U "$USERNAME" "$DBNAME" -c "$sql_statement"

elif [ "$ENVIRONMENT" == "kubectl" ]; then
# Assign namespace argument
NAMESPACE=$4

# Find the Postgres pod automatically
POD_NAME=$(kubectl get pods -n "$NAMESPACE" -l app.name=postgres -o jsonpath="{.items[0].metadata.name}")

if [ -z "$POD_NAME" ]; then
# Try alternative label
POD_NAME=$(kubectl get pods -n "$NAMESPACE" -l inner.service=postgres -o jsonpath="{.items[0].metadata.name}")
if [ -z "$POD_NAME" ]; then
echo "No running Postgres pod found in namespace $NAMESPACE."
exit 1
fi
fi

# Run the SQL statement using kubectl exec
kubectl -n "$NAMESPACE" exec "$POD_NAME" -- psql -U "$USERNAME" "$DBNAME" -c "$sql_statement"

else
echo "Invalid environment specified. Use 'kubectl' or 'docker'."
exit 1
fi
}
51 changes: 4 additions & 47 deletions deploy/scripts/upgrade/upgrade-84-monitoring.sh
Original file line number Diff line number Diff line change
@@ -1,51 +1,8 @@
#!/bin/bash

# Check for required arguments
if [ "$#" -lt 3 ] || { [ "$1" == "kubectl" ] && [ "$#" -ne 4 ]; }; then
echo "Usage: $0 <kubectl|docker> <username> <dbname> [namespace]"
exit 1
fi
# Source the migration template
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
source "SCRIPT_DIR/lib/migration.sh"

# Assign arguments to variables
ENVIRONMENT=$1
USERNAME=$2
DBNAME=$3
ALTER_STATEMENT='ALTER TABLE v1.couchdb_progress ADD pending integer, ADD updated_at timestamptz';

if [ "$ENVIRONMENT" == "docker" ]; then
# Find the container ID or name automatically
CONTAINER_ID=$(docker ps --filter "name=postgres" --format "{{.ID}}" | head -n 1)

if [ -z "$CONTAINER_ID" ]; then
echo "No running Postgres container found."
exit 1
fi

# Run the ALTER TABLE statement using docker exec
docker exec "$CONTAINER_ID" psql -U "$USERNAME" "$DBNAME" -c "$ALTER_STATEMENT"

elif [ "$ENVIRONMENT" == "kubectl" ]; then
# Assign namespace argument
NAMESPACE=$4

# Find the Postgres pod automatically
POD_NAME=$(kubectl get pods -n "$NAMESPACE" -l app.name=postgres -o jsonpath="{.items[0].metadata.name}")

if [ -z "$POD_NAME" ]; then
# Find the Postgres pod automatically
POD_NAME=$(kubectl get pods -n "$NAMESPACE" -l inner.service=postgres -o jsonpath="{.items[0].metadata.name}")
if [ -z "$POD_NAME" ]; then
echo "No running Postgres pod found in namespace $NAMESPACE."
exit 1
fi
fi

# echo $POD_NAME
# Run the ALTER TABLE statement using kubectl exec
kubectl -n "$NAMESPACE" exec "$POD_NAME" -- psql -U "$USERNAME" "$DBNAME" -c "$ALTER_STATEMENT"

else
echo "Invalid environment specified. Use 'kubectl' or 'docker'."
exit 1
fi

execute_sql "$ALTER_STATEMENT"

0 comments on commit e1677e0

Please sign in to comment.