Skip to content
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

Init commands to download latest database dump & reseed db from file #731

Draft
wants to merge 8 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .config/commands/docker.justfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ up-reseed *args:
export UPLOADS_PRESEED_URL="https://github.com/MaMpf-HD/mampf-init-data/raw/main/data/uploads.zip"
docker compose rm --stop --force mampf && docker compose up {{args}}

# Starts the dev docker containers and preseeds the database from a local file
[confirm("This will reset all your data in the database locally. Continue? (y/n)")]
up-reseed-from-file preseed_file *args:
#!/usr/bin/env bash
cd {{justfile_directory()}}/docker/development/
export DB_SQL_PRESEED_URL="{{preseed_file}}"
export UPLOADS_PRESEED_URL=""
docker compose rm --stop --force mampf && docker compose up {{args}}

# Removes the development docker containers
@down:
#!/usr/bin/env bash
Expand Down
40 changes: 40 additions & 0 deletions .config/commands/prod.justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[private]
help:
@just --list --justfile {{source_file()}}

# Downloads the latest database dump from the production server
download-db-dump:
#!/usr/bin/env bash
set -e

# User input: proxy jump
echo "To connect to the remote server you might need a proxy jump. Enter the host name (or leave empty):"
read proxy_jump_destination
proxy_jump_cmd="-J $proxy_jump_destination"

# User input for remote server & dump folder
echo "Enter the remote user and host in the format user@host "
read remote_user_host
echo "Enter the path to the folder that contains the database dumps on the remote server, e.g. /a/b/db"
read remote_dump_folder

# Latest file
latest_file=$(ssh $proxy_jump_cmd "$remote_user_host" "ls -t $remote_dump_folder | head -n 1")
if [ -z "$latest_file" ]; then
echo "No files found in the remote folder."
exit 1
fi
echo ""
echo "Latest file found: $latest_file"

# Download file
echo "We will now download this file to the local machine into the folder db/backups/prod/"
echo -n "Are you sure you want to continue? (y/n) "
read confirmation
if [ "$confirmation" != "y" ]; then
echo "Operation cancelled."
exit 1
fi
local_dir={{justfile_directory()}}/db/backups/prod/
mkdir -p "$local_dir"
scp -C $proxy_jump_cmd "$remote_user_host:$remote_dump_folder/$latest_file" "$local_dir"
3 changes: 3 additions & 0 deletions .justfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ mod deps ".config/commands/deps.justfile"
# Some utils, e.g. ERD-generation etc.
mod utils ".config/commands/utils.justfile"

# Commands to interact with the production server
mod prod ".config/commands/prod.justfile"

# Opens the MaMpf wiki in the default browser
wiki:
#!/usr/bin/env bash
Expand Down
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,15 @@
"factorybot",
"helpdesk",
"katex",
"mampf",
"preseed",
"preseeds",
"preselection",
"selectize",
"Timecop",
"turbolinks",
"Unsets",
"uncached",
"Unsets",
"whitespaces"
],
"cSpell.enableFiletypes": [
Expand Down
17 changes: 11 additions & 6 deletions initialize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ check_for_preseeds() {

# Database preseed
if [[ "${DB_SQL_PRESEED_URL}" ]]; then
echo "💾 Found DB preseed at URL: $DB_SQL_PRESEED_URL"
mkdir -pv db/backups/docker_development
wget --content-disposition --directory-prefix=db/backups/docker_development/ --timestamping $DB_SQL_PRESEED_URL
for file in db/backups/docker_development/*.sql; do
[[ $file -nt $latest ]] && latest=$file
done
if [[ -f "${DB_SQL_PRESEED_URL}" ]]; then
echo "💾 Found DB preseed file: $DB_SQL_PRESEED_URL"
latest=$DB_SQL_PRESEED_URL
else
echo "💾 Found DB preseed at URL: $DB_SQL_PRESEED_URL"
mkdir -pv db/backups/docker_development
wget --content-disposition --directory-prefix=db/backups/docker_development/ --timestamping $DB_SQL_PRESEED_URL
for file in db/backups/docker_development/*.sql; do
[[ $file -nt $latest ]] && latest=$file
done
fi

bundle exec rails db:restore pattern=$(echo $latest | rev | cut -d "/" -f1 | rev | cut -d "_" -f1)
bundle exec rails db:migrate
Expand Down
Loading