Skip to main content

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:
  1. Creates a schema_migrations table to track executed migrations
  2. Reads all .sql files from database/migration/
  3. Sorts files alphabetically by name (using numeric prefixes like 000_, 001_, etc.)
  4. Executes each migration that hasn’t been run yet
  5. Records successful migrations in the schema_migrations table
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:
Examples:
  • 000_create_migrations_table.sql
  • 001_create_companies_and_tenants.sql
  • 002_create_roles_and_permissions.sql
  • 003_create_department_designation_level.sql
Always use a numeric prefix to ensure migrations run in the correct order. The system sorts files alphabetically.

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 support
  • tenants - Subscription and plan management
  • Enables pgcrypto extension 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 departments
  • designations - Job titles and positions
  • levels - Employee hierarchy levels
5

004_create_employees.sql

Creates the employee management system:
  • employees - Employee records with authentication
  • Triggers for automatic updated_at timestamp 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 announcements
  • approvals - 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:
You’ll see output like:

Manual Execution

If you need to run migrations separately, you can create a migration-only command:
migrate.go
Run it with:

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 use CREATE 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

The schema_migrations table tracks executed migrations:
Example output:

Troubleshooting

Migration Already Executed

If a migration fails halfway through, it may be recorded as executed even if incomplete. To re-run:
Then restart the application to re-run the migration.

Migration Failed Error

If you see “error executing migration”:
  1. Check the SQL syntax in your migration file
  2. Verify all referenced tables exist (check migration order)
  3. Check PostgreSQL logs for detailed error messages:

File Not Found Error

If migrations aren’t found:
  1. Verify you’re running the application from the project root
  2. Check the database/migration/ directory exists
  3. Ensure .sql files 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:
  1. Option 1: Create a new migration that reverses changes
  2. Option 2: Restore from database backup
  3. Option 3: Manually remove migration record and drop objects
Always backup your database before running migrations in production.

Production Considerations

Backup Before Migrations

Always backup before running migrations:

Test in Staging

Test migrations in a staging environment first:
  1. Restore production data to staging
  2. Run migrations
  3. Verify application functionality
  4. Then deploy to production

Monitor Migration Performance

Large migrations can take time. Monitor progress:

Use Transactions Carefully

Some operations can’t run in transactions (like CREATE INDEX CONCURRENTLY). The current migration system runs each file as a single query, so use multiple files if you need transactional control.

Next Steps

After running migrations:
  1. Verify your database setup
  2. Test the API
  3. Explore the API documentation