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:1
000_create_migrations_table.sql
Creates the
schema_migrations table for tracking migrations:2
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
3
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
4
003_create_department_designation_level.sql
Creates organizational structure tables:
departments- Organizational departmentsdesignations- Job titles and positionslevels- Employee hierarchy levels
5
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
6
005_leave_management.sql
Creates the leave management system:
leaves- Leave request records- Leave types, statuses, and approval workflows
7
006_memos_and_approvals.sql
Creates internal communication and approval systems:
memos- Internal communications and announcementsapprovals- Approval workflow records
8
007_audit_logs.sql
Creates the audit logging system:
audit_logs- System activity tracking for compliance
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
1
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_.2
Create Migration File
Create a new
.sql file with a descriptive name:3
Write SQL
Add your SQL statements to the file:
database/migration/008_add_notifications.sql
4
Test Migration
Restart the application to run the new migration:Verify the migration was successful:
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:
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.