-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: adding migrations scripts for v2
- Loading branch information
Showing
3 changed files
with
74 additions
and
47 deletions.
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 |
---|---|---|
@@ -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 |
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,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 | ||
} |
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 |
---|---|---|
@@ -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" |