elder-yudao-pro/deploy/local/initialize-health.sh

42 lines
5.5 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
set -euo pipefail
# shellcheck disable=SC1091
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common.sh"
SQL_DIR="$REPO_DIR/mysql_script/health"
[[ -d "$SQL_DIR" ]] || { echo "缺少健康模块 SQL 目录: $SQL_DIR" >&2; exit 1; }
mysql_query "$ELDER_DB_NAME" 'SELECT 1 FROM system_users LIMIT 1;' >/dev/null
member_exists="$(mysql_query information_schema "SELECT COUNT(*) FROM TABLES WHERE TABLE_SCHEMA='$ELDER_DB_NAME' AND TABLE_NAME='member_user';")"
if [[ "$member_exists" != "0" ]]; then
unique_index="$(mysql_query information_schema "SELECT COUNT(*) FROM STATISTICS WHERE TABLE_SCHEMA='$ELDER_DB_NAME' AND TABLE_NAME='member_user' AND INDEX_NAME='uk_member_mobile_tenant' AND NON_UNIQUE=0;")"
if [[ "$unique_index" != "2" ]]; then
echo '现有 member_user 缺少租户/手机号唯一索引,已停止以避免改写既有表结构。' >&2
exit 1
fi
fi
for file in V000__member_schema.sql V001__health_identity.sql V002__health_menu.sql V003__health_elderly_basic_info.sql V004__health_elderly_social_info.sql V005__health_collection_draft.sql V006__health_social_support.sql V007__health_elderly_collection_remaining.sql V008__health_measurement.sql V009__health_fall_assessment.sql V010__health_assessment_stages.sql V011__health_assessment_reminders.sql V012__health_fitness.sql V013__platform_elderly_baseline.sql V014__platform_elderly_reference_data.sql V015__platform_elderly_menus.sql V016__platform_identity_bridge.sql V017__member_user_password_reset.sql; do
mysql_bin="${MYSQL_BIN:-$(command -v mysql)}"
MYSQL_PWD="$ELDER_DB_PASSWORD" "$mysql_bin" --protocol=TCP --host="$ELDER_DB_HOST" --port="$ELDER_DB_PORT" \
--user="$ELDER_DB_USER" --default-character-set=utf8mb4 "$ELDER_DB_NAME" < "$SQL_DIR/$file"
echo "已应用 $file"
done
health_table_count="$(mysql_query information_schema "SELECT COUNT(*) FROM TABLES WHERE TABLE_SCHEMA='$ELDER_DB_NAME' AND TABLE_NAME IN ('health_identity','health_elderly_profile','health_volunteer_profile','health_family_relation','health_measurement','health_assessment_record','health_assessment_answer','health_screening_record','health_screening_answer','health_assessment_task','health_assessment_reminder','health_assessment_schedule_log','health_fitness_session','health_fitness_result');")"
health_permission_count="$(mysql_query "$ELDER_DB_NAME" "SELECT COUNT(*) FROM system_menu WHERE permission IN ('health:identity:query','health:identity:create','health:identity:update','health:identity:audit','health:identity:update-status','health:family-relation:query','health:family-relation:create','health:family-relation:review','health:family-relation:revoke','health:measurement:query','health:measurement:create','health:assessment:query','health:assessment:create','health:screening:query','health:screening:create','health:fitness:query','health:fitness:create') AND deleted=b'0';")"
basic_info_column_count="$(mysql_query information_schema "SELECT COUNT(*) FROM COLUMNS WHERE TABLE_SCHEMA='$ELDER_DB_NAME' AND ((TABLE_NAME='health_identity' AND COLUMN_NAME IN ('id_card','ethnicity','has_wechat')) OR (TABLE_NAME='health_elderly_profile' AND COLUMN_NAME IN ('collection_step','special_case')));")"
social_column_count="$(mysql_query information_schema "SELECT COUNT(*) FROM COLUMNS WHERE TABLE_SCHEMA='$ELDER_DB_NAME' AND TABLE_NAME='health_elderly_profile' AND COLUMN_NAME IN ('marital_status','native_place','residence_area','residence_address','residence_type','living_status','household_type','education_level','occupation','elderly_university');")"
support_table_count="$(mysql_query information_schema "SELECT COUNT(*) FROM TABLES WHERE TABLE_SCHEMA='$ELDER_DB_NAME' AND TABLE_NAME IN ('health_collection_draft','health_region_altitude','health_elderly_spouse');")"
remaining_column_count="$(mysql_query information_schema "SELECT COUNT(*) FROM COLUMNS WHERE TABLE_SCHEMA='$ELDER_DB_NAME' AND TABLE_NAME='health_elderly_profile' AND COLUMN_NAME IN ('insurance_type','income_range','medical_insurance_number','care_level','disabled','disability_type','disability_reason','chronic_disease_status','chronic_diseases','father_alive','mother_alive','father_death_reason','mother_death_reason','father_death_reason_detail','mother_death_reason_detail','emergency_contacts','collector_id','collector_type','collect_time','collection_completed_time','consent_confirmed','consent_confirm_time','consent_collector_id');")"
if [[ "$health_table_count" != "14" || "$health_permission_count" != "17" || "$basic_info_column_count" != "5" || "$social_column_count" != "10" || "$support_table_count" != "3" || "$remaining_column_count" != "23" ]]; then
echo "健康模块初始化校验失败:表=$health_table_count,权限=$health_permission_count,步骤一字段=$basic_info_column_count,后续采集字段=$remaining_column_count。" >&2
exit 1
fi
echo "健康模块数据库已就绪:14 张健康身份、档案、关系、测量、体适能、初筛、评估与提醒表,17 个权限,完整七步采集及多量表评估已就绪。"
platform_table_count="$(mysql_query information_schema "SELECT COUNT(*) FROM TABLES WHERE TABLE_SCHEMA='$ELDER_DB_NAME' AND TABLE_NAME LIKE 'elderly_%';")"
platform_link_count="$(mysql_query information_schema "SELECT COUNT(*) FROM TABLES WHERE TABLE_SCHEMA='$ELDER_DB_NAME' AND TABLE_NAME='health_platform_profile_link';")"
[[ "$platform_table_count" == "53" && "$platform_link_count" == "1" ]] || { echo "Platform 结构校验失败:$platform_table_count / $platform_link_count" >&2; exit 1; }
echo "Platform 全量业务结构已就绪:53 张业务表及 1 张身份桥表。"