Overview
CompanyFlow uses a custom SQL-based migration system. Migrations are stored as.sql files in the database/migration/ directory and are executed automatically when the application starts.
How Migrations Work
The migration system:- Creates a
schema_migrationstable to track executed migrations - Reads all
.sqlfiles fromdatabase/migration/ - Sorts files alphabetically by name (using numeric prefixes like
000_,001_, etc.) - Executes each migration that hasn’t been run yet
- Records successful migrations in the
schema_migrationstable
Migrations run automatically when you start the application with
go run main.go. You don’t need to run them manually.Migration Naming Convention
Migration files follow this naming pattern:000_create_migrations_table.sql001_create_companies_and_tenants.sql002_create_roles_and_permissions.sql003_create_department_designation_level.sql
Included Migrations
CompanyFlow includes the following migrations:001_create_companies_and_tenants.sql
Creates the multi-tenant architecture tables:
companies- Company records with multi-tenant supporttenants- Subscription and plan management- Enables
pgcryptoextension for UUID generation
002_create_roles_and_permissions.sql
Creates the RBAC (Role-Based Access Control) system:
roles- User roles (e.g., Admin, Manager, Employee)permissions- Role permissions and access control
003_create_department_designation_level.sql
Creates organizational structure tables:
departments- Organizational departmentsdesignations- Job titles and positionslevels- Employee hierarchy levels
004_create_employees.sql
Creates the employee management system:
employees- Employee records with authentication- Triggers for automatic
updated_attimestamp updates - Foreign key relationships to departments, designations, levels
005_leave_management.sql
Creates the leave management system:
leaves- Leave request records- Leave types, statuses, and approval workflows
006_memos_and_approvals.sql
Creates internal communication and approval systems:
memos- Internal communications and announcementsapprovals- Approval workflow records
Running Migrations
Automatic Execution
Migrations run automatically when you start the application:Manual Execution
If you need to run migrations separately, you can create a migration-only command:migrate.go
Creating New Migrations
Determine Next Number
Check the highest numbered migration file:If the last file is
007_audit_logs.sql, your next migration should start with 008_.Migration Best Practices
Use CREATE IF NOT EXISTS
Always useCREATE TABLE IF NOT EXISTS to make migrations idempotent:
Add Indexes
Create indexes for foreign keys and frequently queried columns:Include Comments
Document complex schemas with SQL comments:Use Constraints
Add appropriate constraints to maintain data integrity:Handle Foreign Keys Carefully
Consider the impact of deletes:Migration Tracking
Theschema_migrations table tracks executed migrations:
| id | name | executed_at |
|---|---|---|
| uuid | 007_audit_logs.sql | 2026-03-03 10:30:15 |
| uuid | 006_memos_and_approvals.sql | 2026-03-03 10:30:14 |
| uuid | 005_leave_management.sql | 2026-03-03 10:30:13 |
Troubleshooting
Migration Already Executed
If a migration fails halfway through, it may be recorded as executed even if incomplete. To re-run:Migration Failed Error
If you see “error executing migration”:- Check the SQL syntax in your migration file
- Verify all referenced tables exist (check migration order)
- Check PostgreSQL logs for detailed error messages:
File Not Found Error
If migrations aren’t found:- Verify you’re running the application from the project root
- Check the
database/migration/directory exists - Ensure
.sqlfiles have the correct extension (not.txt)
Permission Denied
If you see permission errors:Rollback Strategy
CompanyFlow doesn’t have built-in rollback support. For rollbacks:-
Option 1: Create a new migration that reverses changes
-
Option 2: Restore from database backup
-
Option 3: Manually remove migration record and drop objects
Production Considerations
Backup Before Migrations
Always backup before running migrations:Test in Staging
Test migrations in a staging environment first:- Restore production data to staging
- Run migrations
- Verify application functionality
- Then deploy to production
Monitor Migration Performance
Large migrations can take time. Monitor progress:Use Transactions Carefully
Some operations can’t run in transactions (likeCREATE INDEX CONCURRENTLY). The current migration system runs each file as a single query, so use multiple files if you need transactional control.