chore: 集成初始化 SQL 导入到 local-up.sh,导出默认及全量初始化数据

This commit is contained in:
2026-07-13 10:29:44 +08:00
parent 31d4a34ebb
commit 8c4deb5be4
3 changed files with 484 additions and 7 deletions

View File

@@ -19,6 +19,9 @@ CHANNEL_SERVICE_PORT=${CHANNEL_SERVICE_PORT:-8000}
WITH_APPS=""
BUILD_FLAG=""
INIT_DATA=""
INIT_FULL_DATA=""
FORCE_INIT=""
usage() {
cat <<EOF
@@ -27,9 +30,12 @@ Usage: $0 [OPTIONS]
Start local Docker infrastructure (and optionally application services).
Options:
--with-apps Also start mci-server, admin-web and channel-service
--build Force (re)build images before starting services
-h, --help Show this help message and exit
--with-apps Also start mci-server, admin-web and channel-service
--build Force (re)build images before starting services
--init-data Import scripts/init-data.sql after MySQL is healthy
--init-full-data Import scripts/full-init-data.sql after MySQL is healthy
--force-init Skip empty check and import init SQL directly
-h, --help Show this help message and exit
EOF
}
@@ -37,6 +43,9 @@ while [ $# -gt 0 ]; do
case "$1" in
--with-apps) WITH_APPS=1 ;;
--build) BUILD_FLAG="--build" ;;
--init-data) INIT_DATA=1 ;;
--init-full-data) INIT_FULL_DATA=1 ;;
--force-init) FORCE_INIT=1 ;;
-h|--help) usage; exit 0 ;;
*) echo "Unknown option: $1" >&2; usage; exit 1 ;;
esac
@@ -51,7 +60,7 @@ wait_for_healthy() {
echo "Waiting for $service to become healthy..."
while [ "$retry_count" -lt "$max_retries" ]; do
container_name=$(docker compose --profile infra --profile app ps -q "$service" 2>/dev/null | head -n1 || true)
container_name=$(docker compose -p sino-mci --profile infra --profile app ps -q "$service" 2>/dev/null | head -n1 || true)
if [ -n "$container_name" ]; then
local status
@@ -67,17 +76,44 @@ wait_for_healthy() {
done
echo "$service did not become healthy within $((max_retries * 2)) seconds."
docker compose --profile infra --profile app logs --tail=50 "$service"
docker compose -p sino-mci --profile infra --profile app logs --tail=50 "$service"
return 1
}
init_database() {
local sql_file=$1
if [ ! -f "$sql_file" ]; then
echo "Init SQL file not found: $sql_file" >&2
return 1
fi
if [ -z "$FORCE_INIT" ]; then
local tenant_count
tenant_count=$(docker exec -i mci-mysql mysql -uroot -proot -N -s sino_mci -e "SELECT COUNT(*) FROM mci_tenant;" 2>/dev/null || echo "0")
if [ "$tenant_count" -gt 0 ]; then
echo "Database already contains $tenant_count tenant(s), skipping init. Use --force-init to override."
return 0
fi
fi
echo "Initializing database with $sql_file ..."
docker exec -i mci-mysql mysql -uroot -proot sino_mci < "$sql_file"
echo "Database initialized from $sql_file."
}
echo "Starting infrastructure services..."
docker compose --profile infra up -d $BUILD_FLAG
docker compose -p sino-mci --profile infra up -d $BUILD_FLAG
wait_for_healthy mysql 60
wait_for_healthy redis 30
wait_for_healthy rabbitmq 60
if [ -n "$INIT_DATA" ]; then
init_database "scripts/init-data.sql"
elif [ -n "$INIT_FULL_DATA" ]; then
init_database "scripts/full-init-data.sql"
fi
echo "Infrastructure is ready."
echo "MySQL: jdbc:mysql://localhost:${MYSQL_PORT}/${MYSQL_DATABASE:-sino_mci}"
echo "Redis: localhost:${REDIS_PORT}"
@@ -87,7 +123,7 @@ echo "SeaweedFS: http://localhost:${SEAWEEDFS_MASTER_PORT}"
if [ -n "$WITH_APPS" ]; then
echo "Starting application services..."
# Include infra profile so that depends_on conditions can resolve.
docker compose --profile infra --profile app up -d $BUILD_FLAG
docker compose -p sino-mci --profile infra --profile app up -d $BUILD_FLAG
wait_for_healthy mci-server 90
wait_for_healthy channel-service 60