Skip to content

🚚 migrate.sh

The migrate.sh script simplifies the management of database migrations using Alembic by providing various commands for creating, upgrading, and managing migrations. It automates common tasks such as revision creation, database upgrades, and rollback operations.


Operations

1. Base Setup

  • Script Directory: Determines the script's directory and navigates to the project root.
  • Base Script: Loads utility functions from scripts/base.sh.
  • Environment Variables: Loads environment variables from the .env file if it exists.
  • Dependency Check: Ensures that alembic is installed; if not, the script exits with an error message.
  • Source Directory Navigation: Changes to the src directory where migrations are managed.

Functions

1. _createRevision()

  • Purpose: Creates a new Alembic migration revision with an autogenerated schema and a custom message.
  • Usage:
./migrate.sh create "Your migration message"
  • Default Message: "New migration."

2. _upgradeMigration()

  • Purpose: Upgrades the database to the latest revision or a specified target revision.
  • Usage:
./migrate.sh upgrade
./migrate.sh upgrade <revision>
  • Default Target: head (latest revision).

3. _downgradeMigration()

  • Purpose: Downgrades the database to the specified revision.
  • Usage:
./migrate.sh downgrade
./migrate.sh downgrade <revision>
  • Default Target: -1 (previous revision).

4. _showHistory()

  • Purpose: Displays the history of all migrations.
  • Usage:
./migrate.sh history

5. _showCurrent()

  • Purpose: Shows the current migration revision applied to the database.
  • Usage:
./migrate.sh current

6. _checkChanges()

  • Purpose: Validates if there are any uncommitted migration changes.
  • Usage:
./migrate.sh check

7. _showHeads()

  • Purpose: Displays the heads of the migration branches.
  • Usage:
./migrate.sh heads

The script supports the following commands:

Command Alias Description Example
create new, rev, revision Creates a new migration revision. ./migrate.sh create "Add new table"
upgrade up Upgrades the database to the latest or specified revision. ./migrate.sh upgrade head
downgrade down Downgrades the database to the previous or specified revision. ./migrate.sh downgrade -1
history hist Displays migration history. ./migrate.sh history
current cur, now Shows the current applied migration. ./migrate.sh current
check validate Validates if there are pending changes. ./migrate.sh check
heads head Displays the heads of the migration branches. ./migrate.sh heads

Usage

To execute migrate.sh, run one of the following commands:

# Create a new migration revision:
./migrate.sh create "Add new column"

# Upgrade the database to the latest revision:
./migrate.sh upgrade

# Downgrade the database to the previous revision:
./migrate.sh downgrade

# Show migration history:
./migrate.sh history

# Show the current migration:
./migrate.sh current

# Check for migration changes:
./migrate.sh check

Error Handling

  • The script exits with an error if:
    • Alembic is not installed ('alembic' not found or not installed).
    • Invalid command or missing arguments.

For further customization, update the base.sh script or modify environment variables in the .env file.