feat: migrator: colorize help in migrator.sh

This commit is contained in:
2026-02-06 08:38:36 +00:00
parent 683370a71c
commit 043b333654

View File

@@ -331,100 +331,97 @@ init_sql() {
} }
help() { help() {
cat <<'EOF' # shellcheck disable=SC2059
migrator - Database Migration Tool printf "$(cat <<EOF
${BGREEN}Usage:$NC migrator [OPTIONS] COMMAND [ARGS...]
USAGE: migrator - A lightweight database migration tool supporting PostgreSQL and SQLite.
migrator [OPTIONS] COMMAND [ARGS...]
DESCRIPTION:
A lightweight database migration tool supporting PostgreSQL and SQLite.
Tracks migrations in a dedicated table and supports bidirectional migrations. Tracks migrations in a dedicated table and supports bidirectional migrations.
COMMANDS: ${BGREEN}Commands:
init Initialize migration tables in database ${BCYAN}init$NC Initialize migration tables in database
migrate Apply or revert migrations ${BCYAN}migrate$NC Apply or revert migrations
create Create a new migration file ${BCYAN}create$NC Create a new migration file
list List available migrations ${BCYAN}list$NC List available migrations
fetch Fetch migration status from database ${BCYAN}fetch$NC Fetch migration status from database
GLOBAL OPTIONS: ${BGREEN}Global Options:
--db-url URL, -u URL ${BCYAN}--db-url ${CYAN}URL$NC, $BCYAN-u ${CYAN}URL$NC
Database connection URL (required for most commands) Database connection URL (required for most commands)
PostgreSQL: postgresql://user@host/database PostgreSQL: postgresql://user@host/database
SQLite: sqlite:///path/to/file.db or /path/to/file.db SQLite: sqlite:///path/to/file.db or /path/to/file.db
--migration-dir DIR, -d DIR ${BCYAN}--migration-dir ${CYAN}DIR$NC, ${BCYAN}-d ${CYAN}DIR$NC
Directory containing migrations (default: ./migration) Directory containing migrations (default: ./migration)
--inherits TABLE (PostgreSQL only) Parent table for hectic.migration ${BCYAN}--inherits ${CYAN}TABLE$NC (PostgreSQL only) Parent table for hectic.migration
Can be specified multiple times Can be specified multiple times
MIGRATE SUBCOMMANDS: ${BGREEN}Migrate Subcommands:
up [N] Apply next N migrations (default: 1) ${BCYAN}up ${CYAN}[N]$NC Apply next N migrations (default: 1)
up all Apply all pending migrations (same as: up latest) ${BCYAN}up all$NC Apply all pending migrations (same as: up latest)
down [N] Revert last N migrations (default: 1) ${BCYAN}down ${CYAN}[N]$NC Revert last N migrations (default: 1)
to MIGRATION Migrate to specific migration (forward or backward) ${BCYAN}to ${CYAN}MIGRATION$NC Migrate to specific migration (forward or backward)
to latest Migrate to the latest migration (aliases: head, last) ${BCYAN}to latest$NC Migrate to the latest migration (aliases: head, last)
MIGRATE OPTIONS: ${BGREEN}Migrate Options:
--force, -f Force migration despite tree mismatch (not implemented) $BCYAN--force$NC, $BCYAN-f$NC Force migration despite tree mismatch (not implemented)
--set VAR, -v VAR Set psql variable (PostgreSQL only) $BCYAN--set ${CYAN}VAR$NC, $BCYAN-v ${CYAN}VAR$NC Set psql variable (PostgreSQL only)
INIT OPTIONS: ${BGREEN}Init Options:
--dry-run Print initialization SQL without executing $BCYAN--dry-run$NC Print initialization SQL without executing
CREATE OPTIONS: ${BGREEN}Create Options:
--name NAME, -n NAME $BCYAN--name ${CYAN}NAME$NC, $BCYAN-n ${CYAN}NAME$NC
Name for the migration (default: random word) Name for the migration (default: random word)
LIST OPTIONS: ${BGREEN}List Options:
--raw, -r Output raw migration names without validation $BCYAN--raw$NC, $BCYAN-r$NC Output raw migration names without validation
EXAMPLES: ${BGREEN}Examples:
# Initialize migration tracking ${BBLACK}# Initialize migration tracking$NC
migrator --db-url postgresql://user@localhost/mydb init migrator --db-url postgresql://user@localhost/mydb init
# Create a new migration ${BBLACK}# Create a new migration$NC
migrator create --name add-users-table migrator create --name add-users-table
# Apply next migration ${BBLACK}# Apply next migration$NC
migrator -u postgresql://user@localhost/mydb migrate up migrator -u postgresql://user@localhost/mydb migrate up
# Apply next 3 migrations ${BBLACK}# Apply next 3 migrations$NC
migrator -u postgresql://user@localhost/mydb migrate up 3 migrator -u postgresql://user@localhost/mydb migrate up 3
# Apply all pending migrations ${BBLACK}# Apply all pending migrations$NC
migrator -u postgresql://user@localhost/mydb migrate up all migrator -u postgresql://user@localhost/mydb migrate up all
# or: ${BBLACK}# or:$NC
migrator -u postgresql://user@localhost/mydb migrate to latest migrator -u postgresql://user@localhost/mydb migrate to latest
# Revert last migration ${BBLACK}# Revert last migration$NC
migrator -u postgresql://user@localhost/mydb migrate down migrator -u postgresql://user@localhost/mydb migrate down
# Migrate to specific version ${BBLACK}# Migrate to specific version$NC
migrator -u postgresql://user@localhost/mydb migrate to 20231201120000-add-users migrator -u postgresql://user@localhost/mydb migrate to 20231201120000-add-users
# List migrations ${BBLACK}# List migrations$NC
migrator list migrator list
# Use SQLite ${BBLACK}# Use SQLite$NC
migrator --db-url sqlite:///path/to/db.sqlite migrate up migrator --db-url sqlite:///path/to/db.sqlite migrate up
# PostgreSQL with table inheritance ${BBLACK}# PostgreSQL with table inheritance$NC
migrator --inherits audit_log --db-url $DB_URL init migrator --inherits audit_log --db-url $DB_URL init
MIGRATION FILE STRUCTURE: ${BGREEN}Migration File Structure:$NC
migration/ migration/
└── 20231201120000-migration-name/ └── 20231201120000-migration-name/
├── up.sql - Forward migration ├── up.sql - Forward migration
└── down.sql - Rollback migration └── down.sql - Rollback migration
MIGRATION NAMING: ${BGREEN}Migration Naming:$NC
Migrations must follow the format: YYYYMMDDHHMMSS-description Migrations must follow the format: YYYYMMDDHHMMSS-description
Example: 20231201120000-add-users-table Example: 20231201120000-add-users-table
DATABASE SUPPORT: ${BGREEN}Database Support:$NC
PostgreSQL: PostgreSQL:
- Full schema support (hectic.migration) - Full schema support (hectic.migration)
- Domains with regex validation - Domains with regex validation
@@ -438,11 +435,11 @@ DATABASE SUPPORT:
- Trigger-based version control - Trigger-based version control
- File-based databases - File-based databases
ENVIRONMENT VARIABLES: ${BGREEN}Environment Bariables:$NC
MIGRATION_DIR Default migration directory ${BBLACK}MIGRATION_DIR$NC Default migration directory
DB_URL Default database URL (can be overridden with --db-url) ${BBLACK}DB_URL$NC Default database URL (can be overridden with --db-url)
EXIT CODES: ${BGREEN}Exit Codes:$NC
0 Success 0 Success
1 Generic error 1 Generic error
2 Ambiguous arguments or unrelated migration tree 2 Ambiguous arguments or unrelated migration tree
@@ -453,17 +450,15 @@ EXIT CODES:
13 System/database incompatibility 13 System/database incompatibility
127 Required tool not installed (psql or sqlite3) 127 Required tool not installed (psql or sqlite3)
VERSION: ${BGREEN}Version:$NC
0.0.1 0.0.1
AUTHOR: ${BGREEN}More Info:$NC
Created with Nix and POSIX shell
MORE INFO:
Migration files are executed within transactions. Migration files are executed within transactions.
Failed migrations are automatically rolled back. Failed migrations are automatically rolled back.
Migration hashes are tracked to detect tampering. Migration hashes are tracked to detect tampering.
EOF EOF
)" | "$PAGER"
} }
migrate_down() { migrate_down() {
@@ -1058,9 +1053,6 @@ if ! [ "${AS_LIBRARY+x}" ]; then
[ "${SUBCOMMAND+x}" ] || { log error "no subcommand specified. Use 'migrator help' for usage information."; exit 1; } [ "${SUBCOMMAND+x}" ] || { log error "no subcommand specified. Use 'migrator help' for usage information."; exit 1; }
log debug "subcommand: $WHITE$SUBCOMMAND"
log debug "subcommand args: $WHITE$REMAINING_ARS"
eval "set -- $REMAINING_ARS" eval "set -- $REMAINING_ARS"
"$SUBCOMMAND" "$@" "$SUBCOMMAND" "$@"
fi fi