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 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-26 07:46:52 -08:00
parent 481c13b67d
commit 0d2e521d69
6 changed files with 215 additions and 1 deletions

View File

@ -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)$ {

View File

@ -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 $$;

View File

@ -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;

View File

@ -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';

View File

@ -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;

View File

@ -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*"
```