Squashing AdonisJS Migrations: A Tool I Built

The Problem

After years of development, AdonisJS projects often accumulate hundreds of migration files:

  • ๐ŸŒ Slow fresh deployments (running 100+ migrations)
  • ๐Ÿ“‚ Cluttered migration folders
  • ๐Ÿ” Hard to understand current schema
  • ๐Ÿงน No official way to consolidate like Django’s squashmigrations

The Solution

I built adonis-lucid-migration-squash to solve this!

What it does:

Takes your PostgreSQL schema dump and converts it to a clean, single Knex migration with:

  • โœ… Smart enum detection
  • โœ… Proper foreign keys & constraints
  • โœ… Automated verification
  • โœ… Complete up() and down() methods

Quick Start:

# 1. Dump your production schema
pg_dump -s --no-owner --no-acl > schema.sql

# 2. Convert to Knex migration
python -m pg_to_knex schema.sql baseline_migration.ts

# 3. Archive old migrations, use the baseline!
mv database/migrations/*.ts database/archive/
mv baseline.ts database/migrations/1_baseline.ts

Example Output:
Instead of raw SQL, you get clean Knex code:

export async function up(knex: Knex) {
  await knex.schema.createTable('users', (table) => {
    table.uuid('id').primary()
    table.string('email').notNullable().unique()
    table.enum('role', ['admin', 'user']).defaultTo('user')
    table.timestamps(true, true)
  })
}

When to use it?

โœ… Before major version releases
โœ… When you have 50+ migrations
โœ… Creating a clean foundation
โœ… All environments are on the same version

Check it out!

๐Ÿ”— GitHub Would love to hear feedback from the AdonisJS community! ๐Ÿš€ #adonisjs #nodejs #typescript #database #migrations

Similar Posts