A lightweight database migration utility built with Nushell that executes PostgreSQL migrations using psql. Supports multi-track migrations for ERP environments with core and implementation-specific tracks.
The reason this repository exists:
- Needed a CLI-centric solution
- Evaluated sqlx-cli; however, the project determined they would rigidly limit migrations to a single track
- Evaluated flyway; however, they do not natively support unix sockets
In an enterprise setting like ERP where multiple actors contribute to a resulting installation (core + integrators + customers), maintaining a single migration repository/table/track is not acceptable. Core improvements go into the 'core' track maintained by the core team. Implementation firms place improvements in the 'impl' track. Etc...
You may name any track as you deem appropriate. 'core' and 'impl' are simply offered for reference. You may have as many tracks as you wish.
Generally, 'core' team track migrations are executed first as part of any release. Then following tracks are orchestrated and executed as deemed appropriate.
-
Install dependencies:
- Nushell (v0.80+)
- PostgreSQL with
psqlclient
-
Add to your project:
# Copy src/ directory to your project cp -r src/ /path/to/your/project/migration-tool/ -
Set database connection:
export PGHOST=localhost export PGPORT=5432 export PGDATABASE=your_app_db export PGUSER=postgres export PGPASSWORD=your_password
-
Create migration directories:
mkdir -p migrations/core migrations/impl
-
Run migrations:
nu migration-tool/migrate.nu run migrations/core nu migration-tool/migrate.nu run migrations/impl
Create timestamped SQL files following this naming pattern:
{timestamp}_{track}_{description}.sql
Examples:
migrations/core/20231201120000_core_create_users_table.sql
migrations/impl/20231201130000_impl_add_custom_fields.sql
# Apply migrations in directory
nu migrate.nu run ./migrations/core
# Show migration status
nu migrate.nu status ./migrations/core
# Create new migration
nu migrate.nu add ./migrations/core create_users_table
# Show migration history
nu migrate.nu history ./migrations/core
# Validate migrations without running
nu migrate.nu validate ./migrationsOrganize migrations into separate tracks for different concerns:
migrations/
├── core/ # Base application migrations
├── impl/ # Implementation customizations
└── customer/ # Customer-specific migrations
Each track maintains its own metadata table (migrations_core, migrations_impl, etc.).
See https://github.com/chuckstack/chuck-stack-core for an example project using this migration tool.
Run the test suite:
cd test/
nix-shell --run "nu test-runner.nu --all"For detailed testing information, see test/README.md.
Current version for downstream projects:
- Commit:
e309c1ac019cf7afeb549eebb6367215aaf471cb - SHA256:
sha256-zYB1TcPQY3hK7MR/OyCi2xwpahdjbOc2+F0Qn6L+zCY=
- Update
get-hash.nixwith your target commit - Run
nix-shell get-hash.nix - Copy the correct hash from the error message
- Update this README with the new hash
- Update all downstream consumers (see below)
The following files reference this repository and need updating when the hash changes:
chuck-stack-core/test/shell.nixchuck-stack-core/deploy-local/shell.nixchuck-stack-nixos/nixos/stk-core.nix
To find all references: grep -r "chuck-stack-nushell-psql-migration" --include="*.nix"
- Atomic migrations: All pending migrations execute in a single transaction
- Multi-track support: Separate migration paths for different concerns
- Unix socket support: Full PostgreSQL unix socket compatibility
- Environment isolation: Explicit control of psql environment variables
- Pre-flight validation: Optional Nushell validation scripts (
.nufiles)