#!/bin/bash name="cibus_postgres" image="postgres" version="latest" ports="5432:5432" db_name="cibus" command=$1 arg_first=$2 already_running=$(docker ps | grep "$name") bin_path="./cibus_bin" env_path="$bin_path/.env" echo "local environment : (command=$command)" function log_out { green="\033[0;32m" no_color="\033[0m" awk \ -v green=$green \ -v nocolor=$no_color \ -v name=$name \ '{ printf "\n%s%s: %s%s", green, name, nocolor, $0; fflush(); }' } function handle_commands { if [[ $command == 'up' ]] then echo "starting local env" echo if [[ -n $already_running ]] then echo "docker image already running" exit 2 fi docker pull postgres:latest docker run \ -p "$ports" \ -d \ --rm \ --env-file "$env_path" \ --name "$name" \ "$image:$version" echo echo "started local env" elif [[ $command == 'down' ]] then echo "stopping local env" echo if [[ -n $already_running ]] then docker stop "$name" > /dev/null echo "stopped and removed: $name" else echo "container: $name is not running" fi echo echo "stopped local env" elif [[ $command == 'logs' ]] then docker logs "$name" | log_out elif [[ $command == 'connect_db' ]] then echo "connecting to db" echo if [[ -z $already_running ]] then echo "container not running cannot connect to db" exit 1 fi docker exec -it "$name" psql --user "$db_name" --db "$db_name" echo echo "exited db" elif [[ $command == 'print_migration' ]] then echo "printing migrations" echo if [[ -z $already_running ]] then echo "container not running cannot check migrations" exit 1 fi echo "database" docker exec -it "$name" psql --user "$db_name" --db "$db_name" -c "select version, description, success from _sqlx_migrations;" echo "sqlx" migration_path="$bin_path/db" (cd "$migration_path" && sqlx migrate info) echo echo "exited db" elif [[ $command == 'add_migration' ]] then echo "adding migration" echo migration_path="$bin_path/db" (cd "$migration_path" && sqlx migrate add "$arg_first") echo echo "added migration" else echo "please provide a valid command (up / down)" exit 1 fi } handle_commands exit 0