Overview
Tresa Contafy uses Sequelize CLI to manage database migrations. Migrations allow you to version control your database schema and apply changes incrementally.Prerequisites
- PostgreSQL database created and accessible
DATABASE_URLconfigured in.env- Dependencies installed with
pnpm install
Migration Commands
Run Migrations
Apply all pending migrations to your database:Always run migrations before starting the application for the first time or after pulling schema changes
Check Migration Status
View which migrations have been applied:Undo Last Migration
Rollback the most recent migration:Undo All Migrations
Rollback all migrations (resets database):Creating New Migrations
Generate Migration File
Create a new migration with a descriptive name:src/database/migrations/):
Migration File Structure
Each migration hasup (apply) and down (rollback) methods:
Migration Configuration
Migrations are configured through.sequelizerc in the project root:
Database Configuration
Database connection settings are insrc/database/config.cjs:
Deployment Workflow
Run Migrations
Apply pending migrations:
In production, migrations should be run as part of your CI/CD pipeline before deploying new code
Existing Migrations
Tresa Contafy includes the following migrations:Core Tables (2026-01-03)
Core Tables (2026-01-03)
Initial database schema:
20260103041617-create-users.cjs- User accounts20260103041623-create-profiles.cjs- RFC profiles20260103041628-create-subscriptions.cjs- Subscription management20260103041631-create-payment-events.cjs- Payment event log20260103041636-create-invoices.cjs- Invoice records20260103041640-create-expenses.cjs- Expense records (later renamed)
Authentication (2026-01-03 - 2026-01-17)
Authentication (2026-01-03 - 2026-01-17)
20260103044233-add-email-verification-token.cjs- Email verification20260117000000-add-password-reset-token.cjs- Password reset functionality
Payment Complements (2026-01-16)
Payment Complements (2026-01-16)
20260116090000-create-payment-complements.cjs- Payment complement documents20260116090010-create-payment-complement-items.cjs- Related payment items
Features (2026-01 - 2026-02)
Features (2026-01 - 2026-02)
20260107174900-add-user-profile-fields.cjs- Extended user profile20260114120000-create-discount-codes.cjs- Discount code system20260115020000-add-profile-cascade-delete.cjs- Cascade delete constraints20260118202111-add-trial-used-to-users.cjs- Trial tracking20260120000000-add-tour-fields-to-users.cjs- Onboarding tour20260122000000-add-profile-freeze-fields.cjs- Period freezing20260218000000-add-firebase-uid-to-users.cjs- Google authentication20260216000000-add-rfc-unique-to-profiles.cjs- Unique RFC constraint20260223000000-add-trial-days-to-discount-codes.cjs- Trial period in discounts
SAT Integration (2026-01-21 - 2026-02-06)
SAT Integration (2026-01-21 - 2026-02-06)
20260121000000-create-sat-product-services.cjs- SAT product/service catalog20260121000001-create-sat-search-logs.cjs- SAT search logging20260206140000-profile-regimenes-fiscales-array.cjs- Fiscal regime array20260206150000-create-sat-regimenes-fiscales.cjs- Fiscal regime catalog
Recent Updates (2026-02 - 2026-03)
Recent Updates (2026-02 - 2026-03)
20260205000000-rename-expenses-to-accrued-expenses.cjs- Table rename20260205000001-add-invoice-tax-columns.cjs- Tax breakdown columns20260205000002-create-periods.cjs- Period management20260205000003-create-manual-incomes.cjs- Manual income entries20260205000004-add-payment-date-accrued-expenses.cjs- Payment tracking20260206000000-create-payrolls.cjs- Payroll management20260206120000-create-plugins.cjs- Plugin system20260206130000-create-subscription-plugins.cjs- Plugin subscriptions20260304000001-add-branding-to-users.cjs- Custom branding20260304000002-create-public-report-tokens.cjs- Public report sharing20260304000003-add-sat-download-fiel-to-profiles.cjs- SAT FIEL credentials
Database Seeding
Seed data for development and testing:Run Seeders
Undo Seeders
Available Seeders
20260121000000-seed-sat-product-services.cjs- SAT product/service catalog20260206120000-seed-plugins.cjs- Plugin definitions20260218120000-seed-test-user-profile.cjs- Test user and profile
Best Practices
Backup Before Production Migrations
Always backup your production database before running migrations:
Troubleshooting
Migration fails with 'relation already exists'
Migration fails with 'relation already exists'
Problem: Migration tries to create a table/column that already existsSolution:
DATABASE_URL not defined error
DATABASE_URL not defined error
Problem: Sequelize can’t find database connectionSolution:
- Verify
.envfile exists withDATABASE_URL - Check you’re in the correct directory
- Ensure
dotenvis loaded in config file
SSL connection errors
SSL connection errors
Problem: Database requires SSL but connection failsSolution:
Update
src/database/config.cjs:Next Steps
Production Deployment
Deploy your application to production
Database Setup
Learn about database configuration