feat(package): migrator: up to latest

This commit is contained in:
2025-12-18 00:45:09 +00:00
parent 9c25e82c79
commit 01f13723a8
4 changed files with 475 additions and 6 deletions

View File

@@ -0,0 +1,80 @@
#!/bin/dash
HECTIC_NAMESPACE=test-help-and-version
### CASE 1: Help with no arguments
log notice "test case: ${WHITE}help with no arguments"
output=$(migrator 2>&1)
if ! printf '%s' "$output" | grep -q "migrator - Database Migration Tool"; then
log error "test failed: ${WHITE}no help output when no arguments"
exit 1
fi
### CASE 2: Explicit help command
log notice "test case: ${WHITE}explicit help command"
if ! migrator help | grep -q "USAGE:"; then
log error "test failed: ${WHITE}help command doesn't work"
exit 1
fi
### CASE 3: --help flag
log notice "test case: ${WHITE}--help flag"
if ! migrator --help | grep -q "COMMANDS:"; then
log error "test failed: ${WHITE}--help flag doesn't work"
exit 1
fi
### CASE 4: -h flag
log notice "test case: ${WHITE}-h flag"
if ! migrator -h | grep -q "EXAMPLES:"; then
log error "test failed: ${WHITE}-h flag doesn't work"
exit 1
fi
### CASE 5: --version flag
log notice "test case: ${WHITE}--version flag"
version_output=$(migrator --version)
if ! printf '%s' "$version_output" | grep -q "migrator version"; then
log error "test failed: ${WHITE}--version doesn't show version"
exit 1
fi
### CASE 6: -V flag
log notice "test case: ${WHITE}-V flag"
if ! migrator -V | grep -q "0.0.1"; then
log error "test failed: ${WHITE}-V flag doesn't show version"
exit 1
fi
### CASE 7: Help message contains database support info
log notice "test case: ${WHITE}help shows database support"
help_output=$(migrator help)
if ! printf '%s' "$help_output" | grep -q "PostgreSQL"; then
log error "test failed: ${WHITE}help doesn't mention PostgreSQL"
exit 1
fi
if ! printf '%s' "$help_output" | grep -q "SQLite"; then
log error "test failed: ${WHITE}help doesn't mention SQLite"
exit 1
fi
### CASE 8: Help mentions key commands
log notice "test case: ${WHITE}help shows all commands"
for cmd in init migrate create list fetch; do
if ! printf '%s' "$help_output" | grep -qi "$cmd"; then
log error "test failed: ${WHITE}help doesn't mention $cmd command"
exit 1
fi
done
log notice "test passed"

View File

@@ -0,0 +1,148 @@
#!/bin/dash
HECTIC_NAMESPACE=test-migrate-to-latest
log notice "test case: ${WHITE}migrate to latest migration"
# Create initial schema
psql "$DATABASE_URL" -c 'CREATE TABLE articles (id INTEGER PRIMARY KEY)'
# Initialize migrator
if ! migrator --db-url "$DATABASE_URL" init; then
log error "test failed: ${WHITE}init failed"
exit 1
fi
# Create migrations directory with 4 migrations
mkdir -p migration
for i in 1 2 3 4; do
mig_name="2025010100000${i}-migration-${i}"
mkdir -p "migration/${mig_name}"
echo "ALTER TABLE articles ADD COLUMN col${i} TEXT;" > "migration/${mig_name}/up.sql"
echo "ALTER TABLE articles DROP COLUMN col${i};" > "migration/${mig_name}/down.sql"
done
### CASE 1: migrate up all
log notice "test case: ${WHITE}migrate up all"
if ! migrator --db-url "$DATABASE_URL" migrate up all; then
log error "test failed: ${WHITE}migrate up all failed"
exit 1
fi
# Verify all 4 migrations were applied
applied_count=$(psql -Atc "SELECT COUNT(*) FROM hectic.migration" "$DATABASE_URL")
if [ "$applied_count" != "4" ]; then
log error "test failed: ${WHITE}expected 4 migrations, got $applied_count"
exit 1
fi
# Verify all columns exist
if ! psql -Atc "SELECT col1, col2, col3, col4 FROM articles LIMIT 0" "$DATABASE_URL" >/dev/null 2>&1; then
log error "test failed: ${WHITE}not all columns were added"
exit 1
fi
log info "migrate up all: success"
# Revert all migrations for next test
migrator --db-url "$DATABASE_URL" migrate down 4
### CASE 2: migrate to latest
log notice "test case: ${WHITE}migrate to latest"
if ! migrator --db-url "$DATABASE_URL" migrate to latest; then
log error "test failed: ${WHITE}migrate to latest failed"
exit 1
fi
# Verify all 4 migrations were applied
applied_count=$(psql -Atc "SELECT COUNT(*) FROM hectic.migration" "$DATABASE_URL")
if [ "$applied_count" != "4" ]; then
log error "test failed: ${WHITE}expected 4 migrations, got $applied_count"
exit 1
fi
log info "migrate to latest: success"
# Revert for next test
migrator --db-url "$DATABASE_URL" migrate down 4
### CASE 3: migrate to head (alias)
log notice "test case: ${WHITE}migrate to head (alias)"
if ! migrator --db-url "$DATABASE_URL" migrate to head; then
log error "test failed: ${WHITE}migrate to head failed"
exit 1
fi
applied_count=$(psql -Atc "SELECT COUNT(*) FROM hectic.migration" "$DATABASE_URL")
if [ "$applied_count" != "4" ]; then
log error "test failed: ${WHITE}expected 4 migrations, got $applied_count"
exit 1
fi
log info "migrate to head: success"
# Revert for next test
migrator --db-url "$DATABASE_URL" migrate down 4
### CASE 4: migrate up latest (alias)
log notice "test case: ${WHITE}migrate up latest"
if ! migrator --db-url "$DATABASE_URL" migrate up latest; then
log error "test failed: ${WHITE}migrate up latest failed"
exit 1
fi
applied_count=$(psql -Atc "SELECT COUNT(*) FROM hectic.migration" "$DATABASE_URL")
if [ "$applied_count" != "4" ]; then
log error "test failed: ${WHITE}expected 4 migrations, got $applied_count"
exit 1
fi
log info "migrate up latest: success"
### CASE 5: migrate to latest when already at latest (should be no-op)
log notice "test case: ${WHITE}migrate to latest when already at latest"
if ! migrator --db-url "$DATABASE_URL" migrate to latest; then
log error "test failed: ${WHITE}migrate to latest (no-op) failed"
exit 1
fi
applied_count=$(psql -Atc "SELECT COUNT(*) FROM hectic.migration" "$DATABASE_URL")
if [ "$applied_count" != "4" ]; then
log error "test failed: ${WHITE}expected 4 migrations, got $applied_count"
exit 1
fi
log info "migrate to latest (no-op): success"
### CASE 6: Partial migration then up all
log notice "test case: ${WHITE}partial migration then up all"
# Revert to first migration only
migrator --db-url "$DATABASE_URL" migrate down 3
applied_count=$(psql -Atc "SELECT COUNT(*) FROM hectic.migration" "$DATABASE_URL")
if [ "$applied_count" != "1" ]; then
log error "test failed: ${WHITE}expected 1 migration after down 3, got $applied_count"
exit 1
fi
# Now apply all remaining
if ! migrator --db-url "$DATABASE_URL" migrate up all; then
log error "test failed: ${WHITE}migrate up all from partial state failed"
exit 1
fi
applied_count=$(psql -Atc "SELECT COUNT(*) FROM hectic.migration" "$DATABASE_URL")
if [ "$applied_count" != "4" ]; then
log error "test failed: ${WHITE}expected 4 migrations after up all, got $applied_count"
exit 1
fi
log notice "test passed"

View File

@@ -0,0 +1,72 @@
#!/bin/dash
HECTIC_NAMESPACE=test-sqlite-migrate-to-latest
log notice "test case: ${WHITE}SQLite migrate to latest migration"
# Create SQLite database
SQLITE_DB="$PWD/test.db"
export DB_URL="sqlite://$SQLITE_DB"
# Create initial schema
sqlite3 "$SQLITE_DB" "CREATE TABLE posts (id INTEGER PRIMARY KEY)"
# Initialize migrator
if ! migrator --db-url "$DB_URL" init; then
log error "test failed: ${WHITE}init failed"
exit 1
fi
# Create migrations directory with 3 migrations
mkdir -p migration
for i in 1 2 3; do
mig_name="2025010100000${i}-migration-${i}"
mkdir -p "migration/${mig_name}"
echo "ALTER TABLE posts ADD COLUMN field${i} TEXT;" > "migration/${mig_name}/up.sql"
# Note: SQLite DROP COLUMN requires table recreation before 3.35.0
cat > "migration/${mig_name}/down.sql" <<SQL
-- Simplified: just note the revert in comment
-- In production, this would recreate the table without field${i}
SQL
done
### CASE 1: migrate up all
log notice "test case: ${WHITE}migrate up all (SQLite)"
if ! migrator --db-url "$DB_URL" migrate up all; then
log error "test failed: ${WHITE}migrate up all failed"
exit 1
fi
# Verify all 3 migrations were applied
applied_count=$(sqlite3 "$SQLITE_DB" "SELECT COUNT(*) FROM hectic_migration")
if [ "$applied_count" != "3" ]; then
log error "test failed: ${WHITE}expected 3 migrations, got $applied_count"
exit 1
fi
# Verify all columns exist
if ! sqlite3 "$SQLITE_DB" "SELECT field1, field2, field3 FROM posts LIMIT 0" >/dev/null 2>&1; then
log error "test failed: ${WHITE}not all columns were added"
exit 1
fi
log info "migrate up all: success"
### CASE 2: migrate to latest when already at latest
log notice "test case: ${WHITE}migrate to latest when already at latest (SQLite)"
if ! migrator --db-url "$DB_URL" migrate to latest; then
log error "test failed: ${WHITE}migrate to latest (no-op) failed"
exit 1
fi
applied_count=$(sqlite3 "$SQLITE_DB" "SELECT COUNT(*) FROM hectic_migration")
if [ "$applied_count" != "3" ]; then
log error "test failed: ${WHITE}expected 3 migrations, got $applied_count"
exit 1
fi
log notice "test passed"