-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7642012
Showing
3 changed files
with
160 additions
and
0 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,42 @@ | ||
name: Build and Publish Docker Image | ||
|
||
on: | ||
push: | ||
branches: | ||
- 'main' | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: write | ||
packages: write | ||
id-token: write | ||
|
||
steps: | ||
- name: Checkout sources | ||
uses: actions/checkout@v3 | ||
|
||
- name: Set up QEMU | ||
uses: docker/setup-qemu-action@v2 | ||
|
||
- name: Set up Docker Buildx | ||
id: buildx | ||
uses: docker/setup-buildx-action@v2 | ||
with: | ||
platforms: linux/amd64,linux/arm64 | ||
|
||
- name: Login to GitHub Container Registry | ||
uses: docker/login-action@v2 | ||
with: | ||
registry: ghcr.io | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Build and Publish | ||
uses: docker/build-push-action@v4 | ||
with: | ||
file: Dockerfile | ||
platforms: linux/arm64, linux/amd64 | ||
push: true | ||
tags: ghcr.io/${{ github.repository }}:latest |
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,9 @@ | ||
FROM alpine | ||
|
||
RUN apk update && apk add postgresql-client bash ncurses | ||
|
||
WORKDIR app | ||
|
||
ADD . . | ||
|
||
CMD bash migrate.sh |
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,109 @@ | ||
#!/bin/bash | ||
|
||
set -o pipefail | ||
|
||
export TERM=ansi | ||
_GREEN=$(tput setaf 2) | ||
_BLUE=$(tput setaf 4) | ||
_MAGENTA=$(tput setaf 5) | ||
_CYAN=$(tput setaf 6) | ||
_RED=$(tput setaf 1) | ||
_YELLOW=$(tput setaf 3) | ||
_RESET=$(tput sgr0) | ||
_BOLD=$(tput bold) | ||
|
||
# Function to print error messages and exit | ||
error_exit() { | ||
printf "[ ${_RED}ERROR${_RESET} ] ${_RED}$1${_RESET}\n" >&2 | ||
exit 1 | ||
} | ||
|
||
section() { | ||
printf "${_RESET}\n" | ||
echo "${_BOLD}${_BLUE}==== $1 ====${_RESET}" | ||
} | ||
|
||
write_ok() { | ||
echo "[$_GREEN OK $_RESET] $1" | ||
} | ||
|
||
write_warn() { | ||
echo "[$_YELLOW WARN $_RESET] $1" | ||
} | ||
|
||
trap 'echo "An error occurred. Exiting..."; exit 1;' ERR | ||
|
||
printf "${_BOLD}${_MAGENTA}" | ||
echo "+-------------------------------------+" | ||
echo "| |" | ||
echo "| Railway Postgres Migration Script |" | ||
echo "| |" | ||
echo "+-------------------------------------+" | ||
printf "${_RESET}\n" | ||
|
||
echo "For more information, see https://docs.railway.app/database/migration" | ||
echo "If you run into any issues, please reach out to us on Discord: https://discord.gg/railway" | ||
printf "${_RESET}\n" | ||
|
||
section "Validating environment variables" | ||
|
||
# Validate that PLUGIN_URL environment variable exists | ||
if [ -z "$PLUGIN_URL" ]; then | ||
error_exit "PLUGIN_URL environment variable is not set." | ||
fi | ||
|
||
write_ok "PLUGIN_URL correctly set" | ||
|
||
# Validate that NEW_URL environment variable exists | ||
if [ -z "$NEW_URL" ]; then | ||
error_exit "NEW_URL environment variable is not set." | ||
fi | ||
|
||
write_ok "NEW_URL correctly set" | ||
|
||
section "Checking if NEW_URL is empty" | ||
|
||
# Query to check if there are any tables in the new database | ||
query="SELECT count(*) FROM information_schema.tables WHERE table_schema NOT IN ('information_schema', 'pg_catalog');" | ||
table_count=$(psql "$NEW_URL" -t -A -c "$query") | ||
|
||
if [[ $table_count -eq 0 ]]; then | ||
if [ -z "$OVERWRITE_DATABASE" ]; then | ||
write_ok "The new database is empty. Proceeding with restore." | ||
fi | ||
else | ||
if [ -z "$OVERWRITE_DATABASE" ]; then | ||
error_exit "The new database is not empty. Aborting migration.\nSet the OVERWRITE_DATABASE environment variable to overwrite the new database." | ||
fi | ||
write_warn "The new database is not empty. Found OVERWRITE_DATABASE environment variable. Proceeding with restore." | ||
fi | ||
|
||
section "Dumping database from PLUGIN_URL" | ||
|
||
# Run pg_dump on the plugin database | ||
dump_file="plugin_dump.sql" | ||
pg_dump -Fc "$PLUGIN_URL" > "$dump_file" || error_exit "Failed to dump database from $PLUGIN_URL." | ||
|
||
write_ok "Successfully saved dump to $dump_file" | ||
|
||
dump_file_size=$(ls -lh "$dump_file" | awk '{print $5}') | ||
echo "Dump file size: $dump_file_size" | ||
|
||
section "Restoring database to NEW_URL" | ||
|
||
# Restore that data to the new database | ||
pg_restore -d "$NEW_URL" "$dump_file" || error_exit "Failed to restore database to $NEW_URL." | ||
|
||
write_ok "Successfully restored database to NEW_URL" | ||
|
||
printf "${_RESET}\n" | ||
printf "${_RESET}\n" | ||
echo "${_BOLD}${_GREEN}Migration completed successfully${_RESET}" | ||
printf "${_RESET}\n" | ||
echo "Next steps..." | ||
echo "1. Update your application's DATABASE_URL environment variable to point to the new database." | ||
echo ' - You can use variable references to do this. For example `${{ Postgres.DATABASE_URL }}`' | ||
echo "2. Verify that your application is working as expected." | ||
echo "3. Remove the legacy plugin and this service from your Railway project." | ||
|
||
printf "${_RESET}\n" |