Database Migrations
The MarsAI platform uses Sequelize CLI to manage database schema changes through migrations. This ensures version control for the database structure and allows teams to synchronize schema changes.Migration System Overview
Migrations are timestamped files that describe database changes in a sequential, reversible manner.All migration files are located in
~/workspace/source/back/migrations/ and use the .cjs CommonJS format for compatibility with Sequelize CLI.Configuration
Sequelize RC Configuration
The.sequelizerc file (~/workspace/source/back/.sequelizerc:1) defines paths for migrations and configuration:
Database Configuration
Database connection settings are defined in~/workspace/source/back/config/config.cjs:1:
All database credentials come from environment variables in the
.env file for security.Running Migrations
Execute All Pending Migrations
Run all migrations that haven’t been executed yet:- Connects to the database using config from
config/config.cjs - Checks the
SequelizeMetatable for already-run migrations - Executes all pending migrations in chronological order
- Records each migration in
SequelizeMeta
Check Migration Status
View which migrations have been run:- ✓ Executed migrations
- ✗ Pending migrations
Undo Last Migration
Rollback the most recent migration:Undo All Migrations
Rollback all migrations (dangerous - use with caution):Undo to Specific Migration
Rollback to a specific migration file:Always test rollbacks in development before running in production.
Migration File Structure
Each migration file has two functions:up(): Applies the migration (creates tables, adds columns, etc.)down(): Reverts the migration (drops tables, removes columns, etc.)
Basic Migration Template
Example Migrations
Creating the Users Table
File:~/workspace/source/back/migrations/20260128100000-create-users.cjs:1
Full Users Migration Code
Full Users Migration Code
- Creates
userstable with all user fields - Sets
id_useras auto-incrementing primary key - Enforces
emailuniqueness constraint - Uses ENUM types for
jobandrole - Includes timestamps (
createdAt,updatedAt) - Rollback drops the entire table
Creating the Movies Table
File:~/workspace/source/back/migrations/20260128100004-create-movies.cjs:1
Movies Migration Code
Movies Migration Code
The initial movies migration was later extended by additional migrations to add fields like
selection_status, admin_comment, and jury_comment.Creating Vote Histories Table (with Foreign Keys)
File:~/workspace/source/back/migrations/20260216100000-create-vote-histories.cjs:1
- Checks if table exists before creating (idempotent)
- Defines foreign key constraint to
votestable - Sets
CASCADEbehavior for updates and deletes - Uses
Sequelize.literal()for MySQL-specific defaults
Altering Existing Tables (Adding Columns)
File:~/workspace/source/back/migrations/20260216120000-add-jury-comment-to-movies.cjs
Column additions are non-destructive and can be safely rolled back by removing the column.
Modifying Column Types
File:~/workspace/source/back/migrations/20260216120000-alter-movies-selection-status.cjs
Modifying ENUM types in MySQL can be complex. Ensure your down migration accurately reflects the previous state.
Creating Join Tables (Many-to-Many)
File:~/workspace/source/back/migrations/20260205131006-create-movies-juries.cjs
- Creates join table with foreign keys to both tables
- Adds composite primary key on both columns
- Enforces referential integrity with CASCADE
Creating New Migrations
Generate Migration File
Create a new empty migration:YYYYMMDDHHMMSS-add-field-to-table.cjs
Migration Naming Conventions
Use descriptive names that indicate the change:create-tablename- Creating a new tableadd-field-to-table- Adding column(s)alter-table-field- Modifying column(s)remove-field-from-table- Removing column(s)rename-old-to-new- Renaming columns/tables
Common Migration Operations
Migration Execution Order
Migrations in the MarsAI platform run in this order (chronological):- 20260128100000 - Create users table
- 20260128100001 - Create categories table
- 20260128100002 - Create collaborators table
- 20260128100003 - Create events table
- 20260128100004 - Create movies table
- 20260128100005 - Create awards table
- 20260128100006 - Create votes table
- 20260128100008 - Create reservations table
- 20260128100009 - Create movies_categories join table
- 20260128100010 - Create collaborators_movies join table
- 20260128144807 - Alter movies (add fields)
- 20260205100000 - Add assigned_jury to movies
- 20260205131006 - Create movies_juries join table
- 20260210100000 - Alter users (add known_by_mars_ai)
- 20260212103000 - Drop id_film from votes
- 20260212150000 - Add vote modification tracking
- 20260214130000 - Add admin_comment to movies
- 20260216100000 - Create vote_histories table
- 20260216120000 - Alter movies selection_status
- 20260216123000 - Add jury_comment to movies
- 20260219132833 - Add fields to movies
- 20260220123459 - Rename youtube_status column
- 20260302111532 - Create sponsors table
- 20260302171000 - Create newsletter_subscribers table
The timestamp prefix ensures migrations run in the correct order regardless of file system ordering.
Best Practices
Do’s
✓ Always test migrations in development before production ✓ Write reversible migrations with accuratedown() functions
✓ Use transactions for complex multi-step migrations:
Don’ts
✗ Never modify existing migration files after they’ve been run in production ✗ Never delete migration files that have been run ✗ Avoid data migrations in schema migrations (create separate data migrations) ✗ Don’t assume migration order when referencing other tables ✗ Don’t use model imports in migrations (use queryInterface instead)Troubleshooting
Migration Already Exists Error
If you see “SequelizeMeta already contains this migration”:Foreign Key Constraint Failures
Ensure:- Referenced table exists (check migration order)
- Referenced column exists
- Data types match exactly
- Use
CASCADEoptions appropriately
ENUM Modification Issues
MySQL ENUM modifications can be tricky:Production Workflow
- Develop migration locally
- Test up migration - ensure it applies correctly
- Test down migration - ensure it rolls back correctly
- Test with real data - use production-like dataset
- Commit migration file to version control
- Deploy code with migration
- Run migration on production:
- Verify database state after migration
- Monitor application for issues
Always have a backup before running migrations in production.
Related Documentation
- Architecture - System architecture overview
- Database Schema - Complete database structure