CLI & Migrations Guide
Developer console for database migrations and seeding asl well as other tasks using a Laravel-like CLI.
Overview
- Provides a Laravel-like CLI for migrations without requiring the
mysqlbinary. - Uses Node.js
mysql2to connect directly to your database.
Install/Build
- Ensure deps installed:
npm ci - Build:
npm run build
Run
- From the repo root:
./bin/altus migrate- or via npm:
npm run cli -- migrate
Commands
migrateRun outstanding migrationsmigrate:installCreate the migrations table if missingmigrate:statusShow applied and pending migrationsmigrate:rollbackRollback the last batch (default), or by steps/batchmigrate:resetRollback all migrationsmigrate:refreshReset and re-run all migrationsmigrate:freshDrop all tables and re-run migrationsmigrate:upRun a specific migration file (requires--file)migrate:downRollback last migration or a specific file (requires--file)
Options
--path <dir>Directory containing migrations (default:migrations)--database <name>OverrideDB_DATABASE--step [n]Formigrate: put each file in its own batch; forrollback: number of steps--pretendPrint SQL instead of executing--seedRun SQL seeds from<path>/seeds--forceAllow running in production (APP_ENV=production)--file <name>Forup/down: base filename without extension--batch <n>Forrollback: rollback only the specified batch--drop-viewsForfresh: also drop database views
Environment
- Reads
.envautomatically from the repo root. - Uses the same env vars as the app:
DB_HOST,DB_PORT,DB_USERNAME,DB_PASSWORD,DB_DATABASE,DB_SOCKET(optional),APP_ENV/NODE_ENV.
Examples
- Run migrations:
./bin/altus migrate - Show status:
./bin/altus migrate:status - Roll back last batch:
./bin/altus migrate:rollback - Roll back 2 steps:
./bin/altus migrate:rollback --step 2 - Fresh DB (drop all tables) then migrate:
./bin/altus migrate:fresh --force - Run a single file:
./bin/altus migrate:up --file 003_create_analytics_table
Behavior notes
--pretendprints the SQL that would be executed. For file-based SQL the CLI prints the first 120 lines and then shows "... (truncated)" for long files to avoid overwhelming logs.- Seed files are executed from
<path>/seeds/after successful migrations when--seedis provided. Files are executed in natural sort order.
Developer & testing
- The CLI module (
src/cli/index.ts) exports its command helpers so tests can call commands likecmdUpOneandcmdDownOneOrRollbackdirectly. The module guards itsmain()call withif (require.main === module)to avoid starting when imported in tests. - The repository's integration tests already mock
mysql2/promiseand include a test that exercises the CLI helpers; run them withnpm run test:integration.
Seed files and directory layout
Place seed SQL files under the migrations/seeds/ directory. Seed files are executed after migrations when --seed is provided and are run in natural sort order (so prefix filenames with numbers to control ordering).
Recommended layout:
text
project-root/
├─ migrations/
│ ├─ 001_create_users_table.up.sql
│ ├─ 001_create_users_table.down.sql
│ ├─ 002_create_products_table.up.sql
│ ├─ 002_create_products_table.down.sql
│ └─ seeds/
│ ├─ 001_insert_default_roles.sql
│ ├─ 002_insert_sample_users.sql
│ └─ 003_insert_test_products.sqlExample seed file (migrations/seeds/001_insert_default_roles.sql):
sql
-- Insert base roles used by the application
INSERT INTO roles (id, name, created_at, updated_at) VALUES (1, 'admin', NOW(), NOW());
INSERT INTO roles (id, name, created_at, updated_at) VALUES (2, 'user', NOW(), NOW());Example seed file (migrations/seeds/002_insert_sample_users.sql):
sql
-- Insert a sample user for local development only
INSERT INTO users (id, email, name, password_hash, role, is_active, created_at, updated_at)
VALUES (1, 'dev@example.com', 'Developer', '<bcrypt-hash-placeholder>', 'admin', 1, NOW(), NOW());Notes:
- Seeds are meant for bootstrapping local/dev instances. Avoid running production-sensitive seeds in production environments.
- If
--pretendis used, seeds will be printed but not executed.