From 0d2e521d69e4781de8f99ac664d433a7fb237021 Mon Sep 17 00:00:00 2001 From: hailin Date: Mon, 26 Jan 2026 07:46:52 -0800 Subject: [PATCH] fix(nginx): fix admin location try_files path and add multi-tenancy migrations - Fix nginx admin location try_files to use relative path for alias directive - Add database migrations for multi-tenancy support in user-service - Add tenant_id column to users, user_profiles, user_contacts, verification_codes tables - Create migration rollback scripts and documentation Co-Authored-By: Claude Sonnet 4.5 --- nginx/conf.d/default.conf | 2 +- .../20260126_add_multi_tenancy_support.sql | 65 ++++++++++++++++ ...126_add_multi_tenancy_support_rollback.sql | 49 ++++++++++++ .../20260126_add_tenant_id_to_users.sql | 15 ++++ ...260126_add_tenant_id_to_users_rollback.sql | 11 +++ .../database/migrations/README.md | 74 +++++++++++++++++++ 6 files changed, 215 insertions(+), 1 deletion(-) create mode 100644 packages/services/user-service/src/infrastructure/database/migrations/20260126_add_multi_tenancy_support.sql create mode 100644 packages/services/user-service/src/infrastructure/database/migrations/20260126_add_multi_tenancy_support_rollback.sql create mode 100644 packages/services/user-service/src/infrastructure/database/migrations/20260126_add_tenant_id_to_users.sql create mode 100644 packages/services/user-service/src/infrastructure/database/migrations/20260126_add_tenant_id_to_users_rollback.sql create mode 100644 packages/services/user-service/src/infrastructure/database/migrations/README.md diff --git a/nginx/conf.d/default.conf b/nginx/conf.d/default.conf index e73043b..6bc94d3 100644 --- a/nginx/conf.d/default.conf +++ b/nginx/conf.d/default.conf @@ -41,7 +41,7 @@ server { location /admin { alias /usr/share/nginx/html/admin; index index.html; - try_files $uri $uri/ /admin/index.html; + try_files $uri $uri/ /index.html; # 缓存静态资源 location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { diff --git a/packages/services/user-service/src/infrastructure/database/migrations/20260126_add_multi_tenancy_support.sql b/packages/services/user-service/src/infrastructure/database/migrations/20260126_add_multi_tenancy_support.sql new file mode 100644 index 0000000..548512d --- /dev/null +++ b/packages/services/user-service/src/infrastructure/database/migrations/20260126_add_multi_tenancy_support.sql @@ -0,0 +1,65 @@ +-- Migration: Add multi-tenancy support to user service tables +-- Date: 2026-01-26 +-- Description: Add tenant_id column to all user-related tables for multi-tenancy support + +-- ============================================================================ +-- 1. Users table +-- ============================================================================ +ALTER TABLE users +ADD COLUMN IF NOT EXISTS tenant_id uuid; + +CREATE INDEX IF NOT EXISTS idx_users_tenant ON users(tenant_id); +CREATE INDEX IF NOT EXISTS idx_users_tenant_phone ON users(tenant_id, phone); +CREATE INDEX IF NOT EXISTS idx_users_tenant_fingerprint ON users(tenant_id, fingerprint); + +COMMENT ON COLUMN users.tenant_id IS 'Tenant ID for multi-tenancy support'; + +-- ============================================================================ +-- 2. User Profiles table (if exists) +-- ============================================================================ +DO $$ +BEGIN + IF EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'user_profiles') THEN + ALTER TABLE user_profiles + ADD COLUMN IF NOT EXISTS tenant_id uuid; + + CREATE INDEX IF NOT EXISTS idx_user_profiles_tenant ON user_profiles(tenant_id); + CREATE INDEX IF NOT EXISTS idx_user_profiles_user ON user_profiles(user_id); + CREATE INDEX IF NOT EXISTS idx_user_profiles_nationality ON user_profiles(tenant_id, nationality); + + COMMENT ON COLUMN user_profiles.tenant_id IS 'Tenant ID for multi-tenancy support'; + END IF; +END $$; + +-- ============================================================================ +-- 3. User Contacts table (if exists) +-- ============================================================================ +DO $$ +BEGIN + IF EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'user_contacts') THEN + ALTER TABLE user_contacts + ADD COLUMN IF NOT EXISTS tenant_id uuid; + + CREATE INDEX IF NOT EXISTS idx_user_contacts_tenant ON user_contacts(tenant_id); + CREATE INDEX IF NOT EXISTS idx_user_contacts_user ON user_contacts(tenant_id, user_id); + CREATE INDEX IF NOT EXISTS idx_user_contacts_type ON user_contacts(tenant_id, user_id, type); + CREATE INDEX IF NOT EXISTS idx_user_contacts_verified ON user_contacts(tenant_id, is_verified); + + COMMENT ON COLUMN user_contacts.tenant_id IS 'Tenant ID for multi-tenancy support'; + END IF; +END $$; + +-- ============================================================================ +-- 4. Verification Codes table (if exists) +-- ============================================================================ +DO $$ +BEGIN + IF EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'verification_codes') THEN + ALTER TABLE verification_codes + ADD COLUMN IF NOT EXISTS tenant_id uuid; + + CREATE INDEX IF NOT EXISTS idx_verification_codes_tenant ON verification_codes(tenant_id); + + COMMENT ON COLUMN verification_codes.tenant_id IS 'Tenant ID for multi-tenancy support'; + END IF; +END $$; diff --git a/packages/services/user-service/src/infrastructure/database/migrations/20260126_add_multi_tenancy_support_rollback.sql b/packages/services/user-service/src/infrastructure/database/migrations/20260126_add_multi_tenancy_support_rollback.sql new file mode 100644 index 0000000..6f0f974 --- /dev/null +++ b/packages/services/user-service/src/infrastructure/database/migrations/20260126_add_multi_tenancy_support_rollback.sql @@ -0,0 +1,49 @@ +-- Migration Rollback: Remove multi-tenancy support from user service tables +-- Date: 2026-01-26 +-- Description: Rollback tenant_id column from all user-related tables + +-- ============================================================================ +-- 4. Verification Codes table (if exists) +-- ============================================================================ +DO $$ +BEGIN + IF EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'verification_codes') THEN + DROP INDEX IF EXISTS idx_verification_codes_tenant; + ALTER TABLE verification_codes DROP COLUMN IF EXISTS tenant_id; + END IF; +END $$; + +-- ============================================================================ +-- 3. User Contacts table (if exists) +-- ============================================================================ +DO $$ +BEGIN + IF EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'user_contacts') THEN + DROP INDEX IF EXISTS idx_user_contacts_verified; + DROP INDEX IF EXISTS idx_user_contacts_type; + DROP INDEX IF EXISTS idx_user_contacts_user; + DROP INDEX IF EXISTS idx_user_contacts_tenant; + ALTER TABLE user_contacts DROP COLUMN IF EXISTS tenant_id; + END IF; +END $$; + +-- ============================================================================ +-- 2. User Profiles table (if exists) +-- ============================================================================ +DO $$ +BEGIN + IF EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'user_profiles') THEN + DROP INDEX IF EXISTS idx_user_profiles_nationality; + DROP INDEX IF EXISTS idx_user_profiles_user; + DROP INDEX IF EXISTS idx_user_profiles_tenant; + ALTER TABLE user_profiles DROP COLUMN IF EXISTS tenant_id; + END IF; +END $$; + +-- ============================================================================ +-- 1. Users table +-- ============================================================================ +DROP INDEX IF EXISTS idx_users_tenant_fingerprint; +DROP INDEX IF EXISTS idx_users_tenant_phone; +DROP INDEX IF EXISTS idx_users_tenant; +ALTER TABLE users DROP COLUMN IF EXISTS tenant_id; diff --git a/packages/services/user-service/src/infrastructure/database/migrations/20260126_add_tenant_id_to_users.sql b/packages/services/user-service/src/infrastructure/database/migrations/20260126_add_tenant_id_to_users.sql new file mode 100644 index 0000000..998deae --- /dev/null +++ b/packages/services/user-service/src/infrastructure/database/migrations/20260126_add_tenant_id_to_users.sql @@ -0,0 +1,15 @@ +-- Migration: Add tenant_id column to users table +-- Date: 2026-01-26 +-- Description: Add multi-tenancy support to users table + +-- Add tenant_id column +ALTER TABLE users +ADD COLUMN IF NOT EXISTS tenant_id uuid; + +-- Create indexes for multi-tenant queries +CREATE INDEX IF NOT EXISTS idx_users_tenant ON users(tenant_id); +CREATE INDEX IF NOT EXISTS idx_users_tenant_phone ON users(tenant_id, phone); +CREATE INDEX IF NOT EXISTS idx_users_tenant_fingerprint ON users(tenant_id, fingerprint); + +-- Add comment +COMMENT ON COLUMN users.tenant_id IS 'Tenant ID for multi-tenancy support'; diff --git a/packages/services/user-service/src/infrastructure/database/migrations/20260126_add_tenant_id_to_users_rollback.sql b/packages/services/user-service/src/infrastructure/database/migrations/20260126_add_tenant_id_to_users_rollback.sql new file mode 100644 index 0000000..533a7bd --- /dev/null +++ b/packages/services/user-service/src/infrastructure/database/migrations/20260126_add_tenant_id_to_users_rollback.sql @@ -0,0 +1,11 @@ +-- Migration Rollback: Remove tenant_id column from users table +-- Date: 2026-01-26 +-- Description: Rollback multi-tenancy support from users table + +-- Drop indexes +DROP INDEX IF EXISTS idx_users_tenant_fingerprint; +DROP INDEX IF EXISTS idx_users_tenant_phone; +DROP INDEX IF EXISTS idx_users_tenant; + +-- Remove tenant_id column +ALTER TABLE users DROP COLUMN IF EXISTS tenant_id; diff --git a/packages/services/user-service/src/infrastructure/database/migrations/README.md b/packages/services/user-service/src/infrastructure/database/migrations/README.md new file mode 100644 index 0000000..86d832c --- /dev/null +++ b/packages/services/user-service/src/infrastructure/database/migrations/README.md @@ -0,0 +1,74 @@ +# Database Migrations + +This directory contains SQL migration scripts for the User Service database schema. + +## Migration Files + +### 20260126_add_multi_tenancy_support.sql +Adds multi-tenancy support by adding `tenant_id` column to all user-related tables: +- users +- user_profiles +- user_contacts +- verification_codes + +**Rollback**: Use `20260126_add_multi_tenancy_support_rollback.sql` + +## How to Run Migrations + +### Using Docker + +```bash +# Run migration +docker exec iconsulting-postgres psql -U postgres -d iconsulting -f /path/to/migration.sql + +# Or copy file and run +docker cp migration.sql iconsulting-postgres:/tmp/ +docker exec iconsulting-postgres psql -U postgres -d iconsulting -f /tmp/migration.sql +``` + +### Using psql directly + +```bash +psql -U postgres -d iconsulting -f migration.sql +``` + +### From the project root + +```bash +# Run migration on server +ssh user@server "docker exec iconsulting-postgres psql -U postgres -d iconsulting" < migration.sql +``` + +## Rollback Migrations + +To rollback a migration, use the corresponding `*_rollback.sql` file: + +```bash +docker exec iconsulting-postgres psql -U postgres -d iconsulting -f rollback_migration.sql +``` + +## Migration History + +| Date | Migration | Description | Status | +|------|-----------|-------------|--------| +| 2026-01-26 | add_multi_tenancy_support | Added tenant_id to user tables | ✅ Applied | + +## Best Practices + +1. **Always backup before running migrations in production** +2. **Test migrations in development first** +3. **Keep rollback scripts updated** +4. **Document all schema changes** +5. **Use transactions when possible** + +## Verification + +After running a migration, verify the changes: + +```bash +# Check if column was added +docker exec iconsulting-postgres psql -U postgres -d iconsulting -c "\d users" + +# Check indexes +docker exec iconsulting-postgres psql -U postgres -d iconsulting -c "\di idx_users_tenant*" +```