feat: migrator: +multifiles migrations
This commit is contained in:
@@ -0,0 +1 @@
|
||||
DROP TABLE items;
|
||||
@@ -0,0 +1,4 @@
|
||||
CREATE TABLE items (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name TEXT NOT NULL
|
||||
);
|
||||
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE items DROP COLUMN price;
|
||||
ALTER TABLE items DROP COLUMN description;
|
||||
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE items ADD COLUMN description TEXT;
|
||||
ALTER TABLE items ADD COLUMN price NUMERIC;
|
||||
@@ -0,0 +1,84 @@
|
||||
#!/bin/dash
|
||||
|
||||
HECTIC_NAMESPACE=test-migrate-multi-file
|
||||
|
||||
log notice "test case: ${WHITE}migrate up with multi-file layout (up/entrypoint.sql)"
|
||||
|
||||
# Initialize migrator
|
||||
if ! migrator --db-url "$DATABASE_URL" init; then
|
||||
log error "test failed: ${WHITE}init failed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Apply first migration (up/entrypoint.sql)
|
||||
if ! migrator --db-url "$DATABASE_URL" migrate up; then
|
||||
log error "test failed: ${WHITE}first migrate up failed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Verify migration was applied
|
||||
applied_count=$(psql -Atc "SELECT COUNT(*) FROM hectic.migration" "$DATABASE_URL")
|
||||
if [ "$applied_count" != "1" ]; then
|
||||
log error "test failed: ${WHITE}expected 1 migration, got $applied_count"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Verify table was created
|
||||
if ! psql -Atc "SELECT COUNT(*) FROM items" "$DATABASE_URL" >/dev/null 2>&1; then
|
||||
log error "test failed: ${WHITE}items table not created"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log info "first migration applied successfully"
|
||||
|
||||
# Apply second migration
|
||||
if ! migrator --db-url "$DATABASE_URL" migrate up; then
|
||||
log error "test failed: ${WHITE}second migrate up failed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Verify both columns were added
|
||||
if ! psql -Atc "SELECT description FROM items LIMIT 0" "$DATABASE_URL" >/dev/null 2>&1; then
|
||||
log error "test failed: ${WHITE}description column not added"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! psql -Atc "SELECT price FROM items LIMIT 0" "$DATABASE_URL" >/dev/null 2>&1; then
|
||||
log error "test failed: ${WHITE}price column not added"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log info "second migration applied successfully"
|
||||
|
||||
# Migrate down one step
|
||||
if ! migrator --db-url "$DATABASE_URL" migrate down; then
|
||||
log error "test failed: ${WHITE}migrate down failed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Verify only 1 migration remains
|
||||
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, got $applied_count"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Verify columns were removed
|
||||
if psql -Atc "SELECT description FROM items LIMIT 0" "$DATABASE_URL" >/dev/null 2>&1; then
|
||||
log error "test failed: ${WHITE}description column should be removed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Migrate down to clean state
|
||||
if ! migrator --db-url "$DATABASE_URL" migrate down; then
|
||||
log error "test failed: ${WHITE}second migrate down failed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Verify items table was dropped
|
||||
if psql -Atc "SELECT COUNT(*) FROM items" "$DATABASE_URL" >/dev/null 2>&1; then
|
||||
log error "test failed: ${WHITE}items table should be dropped"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log notice "test passed: multi-file migration layout works correctly"
|
||||
@@ -13,7 +13,9 @@ printf '%s' "$result" > result
|
||||
printf '20251004192425-some-changes
|
||||
20251004292448-some-changes
|
||||
20251104172425-third-migration
|
||||
20251104192426-multi-file-both
|
||||
20251104192427-an-other-one
|
||||
20251104192428-multi-file-up-only
|
||||
20251104292469-almoust-last
|
||||
20251204152446-very-last' > expected
|
||||
|
||||
@@ -35,8 +37,10 @@ printf '%s' "$result" > result
|
||||
|
||||
printf '20251004192425-some-changes: missing up.sql down.sql
|
||||
20251004292448-some-changes
|
||||
20251104172425-third-migration: missing down.sql
|
||||
20251104192427-an-other-one: missing down.sql
|
||||
20251104172425-third-migration: missing down.sql
|
||||
20251104192426-multi-file-both
|
||||
20251104192427-an-other-one: missing down.sql
|
||||
20251104192428-multi-file-up-only: missing down.sql
|
||||
20251104292469-almoust-last
|
||||
20251204152446-very-last' > expected
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE items;
|
||||
@@ -0,0 +1,4 @@
|
||||
CREATE TABLE items (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL
|
||||
);
|
||||
@@ -0,0 +1,4 @@
|
||||
CREATE TABLE items_backup (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL);
|
||||
INSERT INTO items_backup SELECT id, name FROM items;
|
||||
DROP TABLE items;
|
||||
ALTER TABLE items_backup RENAME TO items;
|
||||
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE items ADD COLUMN description TEXT;
|
||||
ALTER TABLE items ADD COLUMN price REAL;
|
||||
90
test/package/migrator/test/sqlite/sqlite-multi-file/run.sh
Normal file
90
test/package/migrator/test/sqlite/sqlite-multi-file/run.sh
Normal file
@@ -0,0 +1,90 @@
|
||||
#!/bin/dash
|
||||
|
||||
HECTIC_NAMESPACE=test-sqlite-multi-file
|
||||
|
||||
log notice "test case: ${WHITE}SQLite multi-file migration layout"
|
||||
|
||||
# Create SQLite database
|
||||
SQLITE_DB="$PWD/test.db"
|
||||
export DB_URL="sqlite://$SQLITE_DB"
|
||||
|
||||
log info "using SQLite database: $SQLITE_DB"
|
||||
|
||||
# Initialize migrator
|
||||
if ! migrator --db-url "$DB_URL" init; then
|
||||
log error "test failed: ${WHITE}init failed for SQLite"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Apply first migration (up/entrypoint.sql)
|
||||
if ! migrator --db-url "$DB_URL" migrate up; then
|
||||
log error "test failed: ${WHITE}first migration failed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Verify migration was applied
|
||||
migration_count=$(sqlite3 "$SQLITE_DB" "SELECT COUNT(*) FROM hectic_migration")
|
||||
if [ "$migration_count" != "1" ]; then
|
||||
log error "test failed: ${WHITE}expected 1 migration, got $migration_count"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Verify table was created
|
||||
if ! sqlite3 "$SQLITE_DB" "SELECT COUNT(*) FROM items" >/dev/null 2>&1; then
|
||||
log error "test failed: ${WHITE}items table not created"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log info "first migration applied successfully"
|
||||
|
||||
# Apply second migration
|
||||
if ! migrator --db-url "$DB_URL" migrate up; then
|
||||
log error "test failed: ${WHITE}second migration failed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Verify columns were added
|
||||
if ! sqlite3 "$SQLITE_DB" "SELECT description FROM items LIMIT 0" >/dev/null 2>&1; then
|
||||
log error "test failed: ${WHITE}description column not added"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! sqlite3 "$SQLITE_DB" "SELECT price FROM items LIMIT 0" >/dev/null 2>&1; then
|
||||
log error "test failed: ${WHITE}price column not added"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log info "second migration applied successfully"
|
||||
|
||||
# Migrate down one step
|
||||
if ! migrator --db-url "$DB_URL" migrate down; then
|
||||
log error "test failed: ${WHITE}migrate down failed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Verify only 1 migration remains
|
||||
migration_count=$(sqlite3 "$SQLITE_DB" "SELECT COUNT(*) FROM hectic_migration")
|
||||
if [ "$migration_count" != "1" ]; then
|
||||
log error "test failed: ${WHITE}expected 1 migration after down, got $migration_count"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Verify columns were removed (table recreated without extra columns)
|
||||
if sqlite3 "$SQLITE_DB" "SELECT description FROM items LIMIT 0" >/dev/null 2>&1; then
|
||||
log error "test failed: ${WHITE}description column should be removed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Migrate down to clean state
|
||||
if ! migrator --db-url "$DB_URL" migrate down; then
|
||||
log error "test failed: ${WHITE}second migrate down failed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Verify items table was dropped
|
||||
if sqlite3 "$SQLITE_DB" "SELECT COUNT(*) FROM items" >/dev/null 2>&1; then
|
||||
log error "test failed: ${WHITE}items table should be dropped"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log notice "test passed: SQLite multi-file migration layout works correctly"
|
||||
Reference in New Issue
Block a user