188 lines
5.9 KiB
Bash
Executable File
188 lines
5.9 KiB
Bash
Executable File
#!/bin/bash
|
|
# ============================================================================
|
|
# Script de Recreación de Base de Datos - Trading Platform
|
|
# ============================================================================
|
|
# Uso: ./recreate-db.sh [--drop] [--seeds]
|
|
#
|
|
# Opciones:
|
|
# --drop Eliminar y recrear la base de datos
|
|
# --seeds Ejecutar archivos de seeds después de DDL
|
|
#
|
|
# Variables de entorno:
|
|
# DB_HOST Host de PostgreSQL (default: localhost)
|
|
# DB_PORT Puerto de PostgreSQL (default: 5432)
|
|
# DB_NAME Nombre de la base de datos (default: trading_platform)
|
|
# DB_USER Usuario de PostgreSQL (default: trading_user)
|
|
# DB_PASSWORD Contraseña (default: trading_dev_2025)
|
|
# ============================================================================
|
|
|
|
set -e
|
|
|
|
# Configuración
|
|
DB_HOST="${DB_HOST:-localhost}"
|
|
DB_PORT="${DB_PORT:-5432}"
|
|
DB_NAME="${DB_NAME:-trading_platform}"
|
|
DB_USER="${DB_USER:-trading_user}"
|
|
DB_PASSWORD="${DB_PASSWORD:-trading_dev_2025}"
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
DDL_DIR="$SCRIPT_DIR/../ddl/schemas"
|
|
SEEDS_DIR="$SCRIPT_DIR/../seeds"
|
|
|
|
# Colores
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m'
|
|
|
|
log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
|
|
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
|
|
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
|
|
log_step() { echo -e "${BLUE}[STEP]${NC} $1"; }
|
|
|
|
# Parse arguments
|
|
DROP_DB=false
|
|
RUN_SEEDS=false
|
|
|
|
for arg in "$@"; do
|
|
case $arg in
|
|
--drop) DROP_DB=true ;;
|
|
--seeds) RUN_SEEDS=true ;;
|
|
--help)
|
|
echo "Uso: ./recreate-db.sh [--drop] [--seeds]"
|
|
echo ""
|
|
echo "Opciones:"
|
|
echo " --drop Eliminar y recrear la base de datos"
|
|
echo " --seeds Ejecutar archivos de seeds después de DDL"
|
|
exit 0
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Export password
|
|
export PGPASSWORD="$DB_PASSWORD"
|
|
|
|
echo ""
|
|
echo "=============================================="
|
|
echo " Trading Platform - Database Recreation"
|
|
echo "=============================================="
|
|
echo " Host: $DB_HOST:$DB_PORT"
|
|
echo " Database: $DB_NAME"
|
|
echo " User: $DB_USER"
|
|
echo " Drop DB: $DROP_DB"
|
|
echo " Run Seeds: $RUN_SEEDS"
|
|
echo "=============================================="
|
|
echo ""
|
|
|
|
# Drop database if requested
|
|
if [ "$DROP_DB" = true ]; then
|
|
log_warn "Eliminando base de datos $DB_NAME..."
|
|
psql -h "$DB_HOST" -p "$DB_PORT" -U postgres -c "DROP DATABASE IF EXISTS $DB_NAME;" 2>/dev/null || true
|
|
psql -h "$DB_HOST" -p "$DB_PORT" -U postgres -c "CREATE DATABASE $DB_NAME OWNER $DB_USER;"
|
|
log_info "Base de datos recreada"
|
|
fi
|
|
|
|
# Crear schemas base
|
|
log_step "Creando schemas base..."
|
|
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -c "
|
|
CREATE SCHEMA IF NOT EXISTS tenants;
|
|
CREATE SCHEMA IF NOT EXISTS users;
|
|
CREATE SCHEMA IF NOT EXISTS auth;
|
|
CREATE SCHEMA IF NOT EXISTS rbac;
|
|
CREATE SCHEMA IF NOT EXISTS teams;
|
|
CREATE SCHEMA IF NOT EXISTS audit;
|
|
CREATE SCHEMA IF NOT EXISTS financial;
|
|
CREATE SCHEMA IF NOT EXISTS products;
|
|
CREATE SCHEMA IF NOT EXISTS vip;
|
|
CREATE SCHEMA IF NOT EXISTS investment;
|
|
CREATE SCHEMA IF NOT EXISTS ml;
|
|
"
|
|
|
|
# Orden de ejecución de DDL
|
|
log_step "Ejecutando archivos DDL..."
|
|
|
|
SCHEMAS=(
|
|
# SaaS Core (debe ir primero por dependencias)
|
|
"tenants/tables/001_tenants.sql"
|
|
"users/tables/001_users.sql"
|
|
"auth/tables/001_sessions.sql"
|
|
"auth/tables/002_tokens.sql"
|
|
# RBAC (después de users)
|
|
"rbac/tables/001_roles.sql"
|
|
"rbac/tables/002_permissions.sql"
|
|
"rbac/tables/003_role_permissions.sql"
|
|
"rbac/tables/004_user_roles.sql"
|
|
# Teams (después de rbac)
|
|
"teams/tables/001_team_members.sql"
|
|
"teams/tables/002_invitations.sql"
|
|
# Audit (independiente)
|
|
"audit/tables/001_audit_logs.sql"
|
|
# Trading Platform
|
|
"financial/001_wallets.sql"
|
|
"financial/002_wallet_transactions.sql"
|
|
"products/001_products.sql"
|
|
"vip/001_vip_system.sql"
|
|
"investment/001_agent_allocations.sql"
|
|
"ml/001_predictions_marketplace.sql"
|
|
"ml/002_predictions.sql"
|
|
"investment/002_add_columns.sql"
|
|
)
|
|
|
|
for schema_file in "${SCHEMAS[@]}"; do
|
|
file_path="$DDL_DIR/$schema_file"
|
|
if [ -f "$file_path" ]; then
|
|
log_info " Ejecutando $schema_file..."
|
|
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -f "$file_path" -q
|
|
else
|
|
log_warn " Archivo no encontrado: $schema_file"
|
|
fi
|
|
done
|
|
|
|
# Execute seeds if requested
|
|
if [ "$RUN_SEEDS" = true ]; then
|
|
log_step "Ejecutando archivos de seeds..."
|
|
|
|
SEEDS=(
|
|
"001_vip_tiers.sql"
|
|
"002_products.sql"
|
|
"003_prediction_packages.sql"
|
|
"004_agent_configs.sql"
|
|
)
|
|
|
|
for seed_file in "${SEEDS[@]}"; do
|
|
file_path="$SEEDS_DIR/$seed_file"
|
|
if [ -f "$file_path" ]; then
|
|
log_info " Ejecutando $seed_file..."
|
|
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -f "$file_path" -q
|
|
else
|
|
log_warn " Archivo no encontrado: $seed_file"
|
|
fi
|
|
done
|
|
fi
|
|
|
|
# Validación
|
|
log_step "Validando creación..."
|
|
|
|
echo ""
|
|
log_info "Schemas creados:"
|
|
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -t -c "
|
|
SELECT schema_name FROM information_schema.schemata
|
|
WHERE schema_name IN ('tenants', 'users', 'auth', 'rbac', 'teams', 'audit', 'financial', 'products', 'vip', 'investment', 'ml')
|
|
ORDER BY schema_name;
|
|
"
|
|
|
|
echo ""
|
|
log_info "Tablas creadas:"
|
|
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -t -c "
|
|
SELECT table_schema || '.' || table_name
|
|
FROM information_schema.tables
|
|
WHERE table_schema IN ('tenants', 'users', 'auth', 'rbac', 'teams', 'audit', 'financial', 'products', 'vip', 'investment', 'ml')
|
|
ORDER BY table_schema, table_name;
|
|
"
|
|
|
|
echo ""
|
|
echo "=============================================="
|
|
log_info "Recreación completada exitosamente"
|
|
echo "=============================================="
|