From c683f69951189a5efb2587fec4041e1f619ae8a2 Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 12 Feb 2026 22:13:38 +0100 Subject: [PATCH] build: adding a setup script --- setup.sh | 151 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100755 setup.sh diff --git a/setup.sh b/setup.sh new file mode 100755 index 0000000..7e79243 --- /dev/null +++ b/setup.sh @@ -0,0 +1,151 @@ +#!/usr/bin/env bash +set -euo pipefail + +DATABASE_USER="tty_dev" +DATABASE_PASS="PasswordCompliqueOuPasMdr!" +DATABASE_NAME="bot_db" +DATABASE_URL="postgres://${DATABASE_USER}:${DATABASE_PASS}@localhost:5432/${DATABASE_NAME}" + +function log_info() { + echo -e "\033[1;34m[INFO]\033[0m $1" +} + +function log_success() { + echo -e "\033[1;32m[SUCCESS]\033[0m $1" +} + +function log_error() { + echo -e "\033[1;31m[ERROR]\033[0m $1" >&2 +} + +function check_command() { + if ! command -v "$1" &> /dev/null; then + log_error "Command not found '$1'" + exit 1 + fi +} + +log_info "Check dependency" +check_command "psql" +check_command "cargo" +check_command "brew" +check_command "sqlx" + +POSTGRES_HOST="localhost" + +if [[ "$(uname)" == "Darwin" ]]; then + POSTGRES_USER=$(whoami) +else + POSTGRES_USER="postgres" +fi + +log_info "PostgreSQL Configuration" + +if [[ "$(uname)" == "Darwin" ]]; then + if ! brew services list | grep -q "postgresql.*started"; then + log_info "Starting PostgreSQL" + brew services start postgresql@15 + sleep 2 + fi +fi + +if ! psql -U "$POSTGRES_USER" -d postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='$DATABASE_USER'" | grep -q 1; then + log_info "PostgreSQL User creation '$DATABASE_USER'" + psql -U "$POSTGRES_USER" -d postgres -c "CREATE USER $DATABASE_USER WITH PASSWORD '$DATABASE_PASS';" || { + log_error "Check if postgres is running / you have access" + exit 1 + } +else + log_info "User '$DATABASE_USER' already exist" +fi + +if ! psql -U "$POSTGRES_USER" -d postgres -tAc "SELECT 1 FROM pg_database WHERE datname='$DATABASE_NAME'" | grep -q 1; then + log_info "'$DATABASE_NAME' Database Creation" + psql -U "$POSTGRES_USER" -d postgres -c "CREATE DATABASE $DATABASE_NAME OWNER $DATABASE_USER;" || { + log_error "Check if postgres is running / you have access" + exit 1 + } +else + log_info "Database '$DATABASE_NAME' already exist" +fi + +log_info "Permission check" +psql -U "$POSTGRES_USER" -d postgres -c "GRANT ALL PRIVILEGES ON DATABASE $DATABASE_NAME TO $DATABASE_USER;" + +log_info "Rust and SQLX configuration" + +if [ ! -f ".env" ]; then + log_info "Creation of the '.env' file" + cat > .env <> .env + fi + log_success "'.env' file updated" +fi + +if [ ! -d ".sqlx" ]; then + log_info "SQLX Installation" + sqlx database setup || { + log_error "Error during the sqlx installation" + exit 1 + } + log_success "SQLX Installed" +else + log_info "SQLX already install / setup" +fi + +log_info "Test migrations" + +mkdir -p migrations + +if [ ! -f "migrations/$(date +%Y%m%d%H%M%S)_create_users_table.sql" ]; then + log_info "Migration test for 'users'" + cat > "migrations/$(date +%Y%m%d%H%M%S)_create_users_table.sql" < /dev/null; then + log_success "Connection successfull" +else + log_error "Connection failed" + exit 1 +fi + +log_success "✅ | Setup Done" +echo "" +echo "Resume:" +echo " - Database User: $DATABASE_USER" +echo " - Database Name: $DATABASE_NAME" +echo " - Database URL: $DATABASE_URL" +echo " - .env : $(pwd)/.env" +echo "" +echo "To connect to DataBase:" +echo " - psql -U $DATABASE_USER -d $DATABASE_NAME" +