DEV Community

Leo Blondel
Leo Blondel

Posted on

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
Enter fullscreen mode Exit fullscreen mode

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)
  })
}
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)