Restructure the monorepo into clear top-level directories: - backend/: Go module root (cmd, internal, pkg, configs, migrations, docs/swagger, scripts, tests, go.mod, Makefile, .air.toml) - deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd - docs/: project documentation + reports/ (moved from repo root) - AGENTS.md: new AI coding-agent guide at repo root Update all references to the new layout: - Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root) - docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile, env_file ../../.env, volume mounts ../../backend:/app - deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile - CI: working-directory: backend for go commands, file deploy/docker/Dockerfile, coverage path backend/coverage.out, health_check backend/scripts/ - backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../ - README: architecture tree, quickstart, config paths updated Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh, gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to preserve history. Build, vet, SQLite tests, and docker compose config verified.
117 lines
3.8 KiB
Bash
Executable File
117 lines
3.8 KiB
Bash
Executable File
#!/bin/bash
|
|
# GoChat Database Seed Script
|
|
# Reference: Chatwoot db:seed pattern — creates super admin + test account + test inbox
|
|
#
|
|
# Usage:
|
|
# ./scripts/seed.sh # Seed with defaults
|
|
# ./scripts/seed.sh --clean # Clean all data before seeding (DANGEROUS in production)
|
|
#
|
|
# Environment variables:
|
|
# GOCHAT_SEED_ADMIN_EMAIL — admin user email (default: admin@gochat.local)
|
|
# GOCHAT_SEED_ADMIN_PASSWORD — admin user password (default: changeme)
|
|
# GOCHAT_SEED_ADMIN_NAME — admin display name (default: Super Admin)
|
|
# GOCHAT_SEED_ACCOUNT_NAME — test account name (default: Test Account)
|
|
# GOCHAT_SEED_INBOX_NAME — test inbox name (default: Test Website Inbox)
|
|
|
|
set -euo pipefail
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
|
|
|
|
# --- Defaults ---
|
|
ADMIN_EMAIL="${GOCHAT_SEED_ADMIN_EMAIL:-admin@gochat.local}"
|
|
ADMIN_PASSWORD="${GOCHAT_SEED_ADMIN_PASSWORD:-changeme}"
|
|
ADMIN_NAME="${GOCHAT_SEED_ADMIN_NAME:-Super Admin}"
|
|
ACCOUNT_NAME="${GOCHAT_SEED_ACCOUNT_NAME:-Test Account}"
|
|
INBOX_NAME="${GOCHAT_SEED_INBOX_NAME:-Test Website Inbox}"
|
|
ENV="${GOCHAT_ENV:-development}"
|
|
|
|
# --- Colors ---
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m' # No Color
|
|
|
|
log_info() { echo -e "${GREEN}[SEED]${NC} $1"; }
|
|
log_warn() { echo -e "${YELLOW}[SEED]${NC} $1"; }
|
|
log_error() { echo -e "${RED}[SEED]${NC} $1"; }
|
|
|
|
# --- Clean mode ---
|
|
if [[ "${1:-}" == "--clean" ]]; then
|
|
if [[ "$ENV" == "production" ]]; then
|
|
log_error "Cannot use --clean in production environment!"
|
|
exit 1
|
|
fi
|
|
log_warn "Cleaning all data before seeding..."
|
|
# Truncate tables in safe order (preserve migrations/schema)
|
|
psql_cmd="psql -h ${POSTGRES_HOST:-localhost} -p ${POSTGRES_PORT:-5432} -U ${POSTGRES_USER:-gochat} -d ${POSTGRES_DB:-gochat_dev}"
|
|
$psql_cmd -c "
|
|
TRUNCATE TABLE
|
|
messages,
|
|
conversations,
|
|
contacts,
|
|
contact_inboxes,
|
|
inboxes,
|
|
inbox_members,
|
|
users,
|
|
accounts,
|
|
attachments,
|
|
reporting_events,
|
|
reporting_events_rollups,
|
|
dashboard_apps,
|
|
captain_assistants,
|
|
captain_documents,
|
|
captain_scenarios,
|
|
captain_custom_tools,
|
|
notifications,
|
|
notification_preferences
|
|
CASCADE;
|
|
"
|
|
log_info "All data cleaned."
|
|
fi
|
|
|
|
# --- Seed via GoChat CLI ---
|
|
# The seed command is built into the GoChat binary to avoid raw SQL maintenance.
|
|
# It creates:
|
|
# 1. Super Admin user (role=super_admin)
|
|
# 2. Test Account owned by the admin
|
|
# 3. Test Website Inbox under the account
|
|
# 4. Agent user (role=agent) in the account
|
|
|
|
BINARY="${PROJECT_ROOT}/bin/gochat"
|
|
|
|
if [[ ! -f "$BINARY" ]]; then
|
|
log_warn "GoChat binary not found at $BINARY, building..."
|
|
cd "$PROJECT_ROOT"
|
|
make build
|
|
fi
|
|
|
|
log_info "Seeding GoChat database (env=$ENV)..."
|
|
log_info " Admin: $ADMIN_EMAIL / $ADMIN_PASSWORD"
|
|
log_info " Account: $ACCOUNT_NAME"
|
|
log_info " Inbox: $INBOX_NAME"
|
|
|
|
cd "$PROJECT_ROOT"
|
|
GOCHAT_ENV="$ENV" \
|
|
GOCHAT_SEED_ADMIN_EMAIL="$ADMIN_EMAIL" \
|
|
GOCHAT_SEED_ADMIN_PASSWORD="$ADMIN_PASSWORD" \
|
|
GOCHAT_SEED_ADMIN_NAME="$ADMIN_NAME" \
|
|
GOCHAT_SEED_ACCOUNT_NAME="$ACCOUNT_NAME" \
|
|
GOCHAT_SEED_INBOX_NAME="$INBOX_NAME" \
|
|
"$BINARY" seed
|
|
|
|
if [[ $? -eq 0 ]]; then
|
|
log_info "Seeding completed successfully!"
|
|
log_info ""
|
|
log_info "Created resources:"
|
|
log_info " ✅ Super Admin: $ADMIN_EMAIL"
|
|
log_info " ✅ Account: $ACCOUNT_NAME"
|
|
log_info " ✅ Inbox: $INBOX_NAME (web_widget channel)"
|
|
log_info ""
|
|
log_info "You can now log in at: http://localhost:3000"
|
|
log_info " Email: $ADMIN_EMAIL"
|
|
log_info " Password: $ADMIN_PASSWORD"
|
|
else
|
|
log_error "Seeding failed! Check the logs above."
|
|
exit 1
|
|
fi |