- ✅ Prisma ORM installed (v7.3.0) for TypeScript-based schema management
- ✅ Prisma Schema defined with multi-tenant architecture
- ✅ Version Tracking System with
migration_historytable - ✅ SQL Migration Runner (
database/scripts/migrate.ts) - ✅ Version Checker (
database/scripts/check-version.ts) - ✅ Package Scripts added for migration management
Complete multi-tenant schema with:
- Accounts (tenant isolation)
- Roles & Users (authentication & authorization)
- CRM entities (clients, suppliers, employees, professionals)
- Migration history tracking
SQL migration file created: 20260123_1800_initial_rls_setup.sql
- PostgreSQL extensions (uuid-ossp, pgcrypto)
- Row Level Security policies for all tables
- Helper functions for account isolation
- Automatic triggers for updated_at
- ✅ Migration strategy guide:
database/migrations/README.md - ✅ Version documentation:
database/DATABASE_VERSION.md - ✅ This activation guide:
DATABASE_ACTIVATION_GUIDE.md
Before running migrations, ensure:
-
PostgreSQL 16 is running
- Via Docker:
docker-compose up -d postgres - Or local installation
- Via Docker:
-
Database credentials are configured
- File:
.env(already created) - Variable:
DATABASE_URL
- File:
-
Node.js & pnpm are installed
- Node.js 20.x or 22.x
- pnpm 9.13.2+
# On Ubuntu/Debian
sudo apt-get install postgresql-client
# On macOS
brew install postgresql
# On Windows (included with PostgreSQL installation)cd /W/NEXO/nx_nexo_v0.info/NEXO/nx_nexo_v0.20260115_backend
docker-compose up -d postgresOr if docker-compose file is elsewhere:
# Check for docker-compose files
find . -name "docker-compose*.yml"cd nexo-prj
# Test connection
pnpm prisma db execute --stdin < /dev/null# Create backup before first migration (good practice)
pnpm db:backup initial-setup# This creates the migration file from schema.prisma
pnpm db:migrate:dev --name initial_multi_tenant_setupThis will:
- Create all tables (accounts, roles, users, clients, etc.)
- Set up indexes
- Create migration history
# Apply RLS policies and triggers (with automatic backup)
pnpm db:migrate:safeOr manually:
pnpm db:backup pre-rls-setup
pnpm db:migrateThis will:
- Enable Row Level Security
- Create isolation policies
- Add triggers for timestamps
- Seed default data
# Check all versions and RLS status
pnpm db:versionExpected output should show:
- ✅ Database Schema: 1.0.0
- ✅ PostgreSQL: 16.x
- ✅ Row Level Security: Enabled
- ✅ RLS Policies: 7+
pnpm db:generate# Create backup after successful setup
pnpm db:backup post-initial-setupcd nexo-prj
# Start all backend services
pnpm nx serve auth-service &
pnpm nx serve crm-service &
pnpm nx serve api-gateway &Open apps/crm-service/test-multi-tenant.http in VS Code with REST Client extension:
- Register Account A
- Register Account B
- Login as both accounts (save tokens)
- Create data in both accounts
- Verify isolation (Account A can't see Account B's data)
pnpm db:version # Check database version and status
pnpm db:migrate # Apply SQL migrations
pnpm db:migrate:dev # Create and apply Prisma migration (dev)
pnpm db:migrate:deploy # Apply Prisma migrations (production)
pnpm db:migrate:dry # Preview migrations without applyingpnpm db:studio # Open Prisma Studio GUI
pnpm db:generate # Generate Prisma Client
pnpm db:push # Push schema without migration
pnpm db:reset # Reset database (development only!)pnpm prisma # Run any Prisma command- Edit
prisma/schema.prisma - Create migration:
pnpm db:migrate:dev --name descriptive_name - Test locally
- Commit migration files
- Deploy:
pnpm db:migrate:deploy
- Create SQL file in
database/migrations/sql/ - Name it:
YYYYMMDD_HHMM_description.sql - Add metadata comments:
-- @version: 1.1.0 -- @name: Add Projects Module -- @description: Creates projects table with RLS
- Apply:
pnpm db:migrate
- Backup database first!
- Test on staging environment
- Run migrations during maintenance window
- Verify with:
pnpm db:version
# Check if PostgreSQL is running
docker ps | grep postgres
# Start if not running
cd .. && docker-compose up -d postgres
# Verify connection string in .env
cat nexo-prj/.env# Check what's in the database
pnpm prisma db pull
# If needed, reset (WARNING: deletes all data!)
pnpm db:reset# Check RLS status
pnpm db:version
# Manually verify in psql
psql $DATABASE_URL -c "SELECT tablename, rowsecurity FROM pg_tables WHERE schemaname = 'public';"# Regenerate client
pnpm db:generate
# Or if schema changed
pnpm db:migrate:devTo check current database version:
pnpm db:versionTo view migration history in database:
SELECT version, name, applied_at, success
FROM migration_history
ORDER BY applied_at DESC;Once migrations are complete:
- ✅ Test Multi-Tenant Isolation (use test-multi-tenant.http)
- ✅ Verify RLS enforcement
- ✅ Set up monitoring for database metrics
- ✅ Configure backups (automated)
- ✅ Document any custom changes
- Prisma Documentation
- PostgreSQL RLS Guide
- Project Architecture:
ARCHITECTURE.md - Activation Checklist:
ACTIVATION_CHECKLIST.md - Migration Strategy:
database/migrations/README.md - Version History:
database/DATABASE_VERSION.md
If you encounter issues:
- Check this guide's troubleshooting section
- Review
ACTIVATION_CHECKLIST.md - Run
pnpm db:versionto see status - Check logs in terminal output
- Review Prisma migration history:
prisma/migrations/
After following this guide, verify:
- PostgreSQL is running
- Database connection works
- Prisma migrations applied successfully
- SQL migrations applied successfully
-
pnpm db:versionshows all green checks - RLS is enabled on all tables
- RLS policies are active (7+)
- Prisma Client is generated
- Services can connect to database
- Multi-tenant isolation test passes
Status: Ready for Activation
Last Updated: 2026-01-23
Version: 1.0.0