feat(package): migrator: mvp

This commit is contained in:
2025-12-16 17:28:36 +00:00
parent 13fdfac2ef
commit bb2ae34758
55 changed files with 890 additions and 46 deletions

View File

@@ -25,7 +25,7 @@ json_diff() {
}
# run test
mkdir './test'
cp -r "$test"/* './test/'
cd './test'
. './run.sh'
#mkdir './test'
#cp -r "$test"/* './test/'
#cd './test'
#. './run.sh'

View File

@@ -0,0 +1,2 @@
ALTER TABLE tags DROP COLUMN name;

View File

@@ -0,0 +1,2 @@
ALTER TABLE tags ADD COLUMN name TEXT;

View File

@@ -0,0 +1,47 @@
#!/bin/dash
HECTIC_NAMESPACE=test-migrate-already-at-target
log notice "test case: ${WHITE}migrate when already at target"
# Create initial schema
psql "$DATABASE_URL" -c 'CREATE TABLE tags (id INTEGER PRIMARY KEY)'
# Initialize migrator
if ! migrator --db-url "$DATABASE_URL" init; then
log error "test failed: ${WHITE}init failed"
exit 1
fi
# Apply migration
if ! migrator --db-url "$DATABASE_URL" migrate up; then
log error "test failed: ${WHITE}first migrate up failed"
exit 1
fi
# Try to migrate to same position (should be no-op)
if ! migrator --db-url "$DATABASE_URL" migrate to 20250101000001-add-name; then
log error "test failed: ${WHITE}migrate to same position failed"
exit 1
fi
# Verify still only 1 migration
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
# Try migrate up when no more migrations available
set +e
migrator --db-url "$DATABASE_URL" migrate up
exit_code=$?
set -e
if [ "$exit_code" = "0" ]; then
log error "test failed: ${WHITE}should error when no migrations left"
exit 1
fi
log notice "test passed"

View File

@@ -0,0 +1,2 @@
ALTER TABLE orders DROP COLUMN user_id;

View File

@@ -0,0 +1,2 @@
ALTER TABLE orders ADD COLUMN user_id INTEGER;

View File

@@ -0,0 +1,2 @@
ALTER TABLE orders DROP COLUMN total;

View File

@@ -0,0 +1,2 @@
ALTER TABLE orders ADD COLUMN total DECIMAL(10,2);

View File

@@ -0,0 +1,2 @@
ALTER TABLE orders DROP COLUMN status;

View File

@@ -0,0 +1,2 @@
ALTER TABLE orders ADD COLUMN status TEXT;

View File

@@ -0,0 +1,2 @@
ALTER TABLE orders DROP COLUMN created_at;

View File

@@ -0,0 +1,2 @@
ALTER TABLE orders ADD COLUMN created_at TIMESTAMPTZ;

View File

@@ -0,0 +1,53 @@
#!/bin/dash
HECTIC_NAMESPACE=test-migrate-down-multiple
log notice "test case: ${WHITE}migrate down multiple steps"
# Create initial schema
psql "$DATABASE_URL" -c 'CREATE TABLE orders (id INTEGER PRIMARY KEY)'
# Initialize migrator
if ! migrator --db-url "$DATABASE_URL" init; then
log error "test failed: ${WHITE}init failed"
exit 1
fi
# Apply 4 migrations
if ! migrator --db-url "$DATABASE_URL" migrate up 4; then
log error "test failed: ${WHITE}migrate up failed"
exit 1
fi
# Verify all columns exist
if ! psql -Atc "SELECT user_id, total, status, created_at FROM orders LIMIT 0" "$DATABASE_URL" >/dev/null 2>&1; then
log error "test failed: ${WHITE}not all columns added"
exit 1
fi
# Migrate down 3 steps (should leave only first migration)
if ! migrator --db-url "$DATABASE_URL" migrate down 3; then
log error "test failed: ${WHITE}migrate down 3 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, got $applied_count"
exit 1
fi
# Verify only user_id column remains
if ! psql -Atc "SELECT user_id FROM orders LIMIT 0" "$DATABASE_URL" >/dev/null 2>&1; then
log error "test failed: ${WHITE}user_id should still exist"
exit 1
fi
if psql -Atc "SELECT total FROM orders LIMIT 0" "$DATABASE_URL" >/dev/null 2>&1; then
log error "test failed: ${WHITE}total column should be removed"
exit 1
fi
log notice "test passed"

View File

@@ -0,0 +1,2 @@
ALTER TABLE products DROP COLUMN price;

View File

@@ -0,0 +1,2 @@
ALTER TABLE products ADD COLUMN price DECIMAL(10,2);

View File

@@ -0,0 +1,2 @@
ALTER TABLE products DROP COLUMN description;

View File

@@ -0,0 +1,2 @@
ALTER TABLE products ADD COLUMN description TEXT;

View File

@@ -0,0 +1,53 @@
#!/bin/dash
HECTIC_NAMESPACE=test-migrate-down-single
log notice "test case: ${WHITE}migrate down single step"
# Create initial schema
psql "$DATABASE_URL" -c 'CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT)'
# Initialize migrator
if ! migrator --db-url "$DATABASE_URL" init; then
log error "test failed: ${WHITE}init failed"
exit 1
fi
# Apply 2 migrations
if ! migrator --db-url "$DATABASE_URL" migrate up 2; then
log error "test failed: ${WHITE}migrate up failed"
exit 1
fi
# Verify both columns exist
if ! psql -Atc "SELECT price, description FROM products LIMIT 0" "$DATABASE_URL" >/dev/null 2>&1; then
log error "test failed: ${WHITE}columns not added"
exit 1
fi
# 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, got $applied_count"
exit 1
fi
# Verify description column was removed but price remains
if psql -Atc "SELECT description FROM products LIMIT 0" "$DATABASE_URL" >/dev/null 2>&1; then
log error "test failed: ${WHITE}description column should be removed"
exit 1
fi
if ! psql -Atc "SELECT price FROM products LIMIT 0" "$DATABASE_URL" >/dev/null 2>&1; then
log error "test failed: ${WHITE}price column should still exist"
exit 1
fi
log notice "test passed"

View File

@@ -0,0 +1,4 @@
-- Remove transformed columns (original data preserved)
ALTER TABLE products DROP COLUMN price_display;
ALTER TABLE products DROP COLUMN price_dollars;

View File

@@ -0,0 +1,9 @@
-- Add new columns and transform existing data
ALTER TABLE products ADD COLUMN price_dollars DECIMAL(10,2);
ALTER TABLE products ADD COLUMN price_display TEXT;
-- Transform existing data
UPDATE products
SET
price_dollars = price_cents::DECIMAL / 100,
price_display = '$' || to_char(price_cents::DECIMAL / 100, 'FM999999999.00');

View File

@@ -0,0 +1,98 @@
#!/bin/dash
HECTIC_NAMESPACE=test-migrate-existing-data-migration
log notice "test case: ${WHITE}data migration on existing populated table"
# Create existing table with data
log info "creating existing table with data"
psql "$DATABASE_URL" -v ON_ERROR_STOP=1 <<SQL
CREATE TABLE products (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
price_cents INTEGER NOT NULL
);
INSERT INTO products (name, price_cents) VALUES
('Widget', 1000),
('Gadget', 2500),
('Gizmo', 500);
SQL
# Verify initial data
product_count=$(psql -Atc "SELECT COUNT(*) FROM products" "$DATABASE_URL")
if [ "$product_count" != "3" ]; then
log error "test failed: ${WHITE}initial data not created"
exit 1
fi
# Initialize migrator
if ! migrator --db-url "$DATABASE_URL" init; then
log error "test failed: ${WHITE}init failed"
exit 1
fi
# Apply migration that does data transformation
log info "applying data migration"
if ! migrator --db-url "$DATABASE_URL" migrate up; then
log error "test failed: ${WHITE}data migration failed"
exit 1
fi
# Verify new columns exist
if ! psql -Atc "SELECT price_dollars, price_display FROM products LIMIT 0" "$DATABASE_URL" >/dev/null 2>&1; then
log error "test failed: ${WHITE}new columns not added"
exit 1
fi
# Verify data was transformed correctly
widget_price=$(psql -Atc "SELECT price_dollars FROM products WHERE name = 'Widget'" "$DATABASE_URL")
widget_display=$(psql -Atc "SELECT price_display FROM products WHERE name = 'Widget'" "$DATABASE_URL")
if [ "$widget_price" != "10.00" ]; then
log error "test failed: ${WHITE}price_dollars not calculated correctly, got: $widget_price"
exit 1
fi
if [ "$widget_display" != "\$10.00" ]; then
log error "test failed: ${WHITE}price_display not formatted correctly, got: $widget_display"
exit 1
fi
log info "data transformation successful"
# Verify all products were transformed
transformed_count=$(psql -Atc "SELECT COUNT(*) FROM products WHERE price_dollars IS NOT NULL" "$DATABASE_URL")
if [ "$transformed_count" != "3" ]; then
log error "test failed: ${WHITE}not all products transformed, got: $transformed_count"
exit 1
fi
# Test rollback of data migration
log info "rolling back data migration"
if ! migrator --db-url "$DATABASE_URL" migrate down; then
log error "test failed: ${WHITE}rollback failed"
exit 1
fi
# Verify columns removed
if psql -Atc "SELECT price_dollars FROM products LIMIT 0" "$DATABASE_URL" >/dev/null 2>&1; then
log error "test failed: ${WHITE}columns should be removed after rollback"
exit 1
fi
# Verify original data still intact
original_count=$(psql -Atc "SELECT COUNT(*) FROM products WHERE price_cents IS NOT NULL" "$DATABASE_URL")
if [ "$original_count" != "3" ]; then
log error "test failed: ${WHITE}original data corrupted after rollback"
exit 1
fi
widget_original=$(psql -Atc "SELECT price_cents FROM products WHERE name = 'Widget'" "$DATABASE_URL")
if [ "$widget_original" != "1000" ]; then
log error "test failed: ${WHITE}original price data corrupted, got: $widget_original"
exit 1
fi
log notice "test passed: data migration works on existing populated tables"

View File

@@ -0,0 +1,3 @@
-- Remove column from existing table
ALTER TABLE users DROP COLUMN bio;

View File

@@ -0,0 +1,3 @@
-- Add column to existing table
ALTER TABLE users ADD COLUMN bio TEXT;

View File

@@ -0,0 +1,3 @@
-- Remove new table
DROP TABLE comments;

View File

@@ -0,0 +1,9 @@
-- Add new table that references existing tables
CREATE TABLE comments (
id SERIAL PRIMARY KEY,
post_id INTEGER NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
content TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);

View File

@@ -0,0 +1,151 @@
#!/bin/dash
HECTIC_NAMESPACE=test-migrate-existing-database
log notice "test case: ${WHITE}add migrator to existing database with data"
# Simulate existing database with tables and data
log info "creating existing database schema and data"
psql "$DATABASE_URL" -v ON_ERROR_STOP=1 <<SQL
-- Existing tables from before migrator
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username TEXT NOT NULL,
email TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(id),
title TEXT NOT NULL,
content TEXT,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Insert existing data
INSERT INTO users (username, email) VALUES
('alice', 'alice@example.com'),
('bob', 'bob@example.com'),
('charlie', 'charlie@example.com');
INSERT INTO posts (user_id, title, content) VALUES
(1, 'First Post', 'Hello World'),
(1, 'Second Post', 'More content'),
(2, 'Bob Post', 'Bob content');
SQL
# Verify existing data
user_count=$(psql -Atc "SELECT COUNT(*) FROM users" "$DATABASE_URL")
post_count=$(psql -Atc "SELECT COUNT(*) FROM posts" "$DATABASE_URL")
if [ "$user_count" != "3" ] || [ "$post_count" != "3" ]; then
log error "test failed: ${WHITE}existing data not created properly"
exit 1
fi
log info "existing database has $user_count users and $post_count posts"
# NOW initialize migrator on existing database
log info "initializing migrator on existing database"
if ! migrator --db-url "$DATABASE_URL" init; then
log error "test failed: ${WHITE}init failed on existing database"
exit 1
fi
# Verify migrator schema was created
if ! psql -Atc "SELECT COUNT(*) FROM hectic.migration" "$DATABASE_URL" >/dev/null 2>&1; then
log error "test failed: ${WHITE}hectic.migration table not created"
exit 1
fi
# Verify existing data is still intact
user_count_after=$(psql -Atc "SELECT COUNT(*) FROM users" "$DATABASE_URL")
post_count_after=$(psql -Atc "SELECT COUNT(*) FROM posts" "$DATABASE_URL")
if [ "$user_count_after" != "$user_count" ] || [ "$post_count_after" != "$post_count" ]; then
log error "test failed: ${WHITE}existing data was affected by migrator init"
exit 1
fi
log info "existing data preserved: $user_count_after users, $post_count_after posts"
# Apply a migration that modifies existing table
log info "applying migration to existing table"
if ! migrator --db-url "$DATABASE_URL" migrate up; then
log error "test failed: ${WHITE}migration on existing table failed"
exit 1
fi
# Verify migration was applied
if ! psql -Atc "SELECT bio FROM users LIMIT 0" "$DATABASE_URL" >/dev/null 2>&1; then
log error "test failed: ${WHITE}bio column not added to existing table"
exit 1
fi
# Verify existing data still intact with NULL in new column
alice_bio=$(psql -Atc "SELECT bio FROM users WHERE username = 'alice'" "$DATABASE_URL")
if [ "$alice_bio" != "" ]; then
log error "test failed: ${WHITE}new column should be NULL for existing rows, got: $alice_bio"
exit 1
fi
# Verify we can still query existing data
alice_email=$(psql -Atc "SELECT email FROM users WHERE username = 'alice'" "$DATABASE_URL")
if [ "$alice_email" != "alice@example.com" ]; then
log error "test failed: ${WHITE}existing data corrupted"
exit 1
fi
log info "migration applied successfully to existing table"
# Apply second migration that adds a new table
if ! migrator --db-url "$DATABASE_URL" migrate up; then
log error "test failed: ${WHITE}second migration failed"
exit 1
fi
# Verify new table exists
if ! psql -Atc "SELECT COUNT(*) FROM comments" "$DATABASE_URL" >/dev/null 2>&1; then
log error "test failed: ${WHITE}comments table not created"
exit 1
fi
# Verify we can add data that references existing data
psql "$DATABASE_URL" -v ON_ERROR_STOP=1 <<SQL
INSERT INTO comments (post_id, user_id, content)
VALUES (1, 2, 'Nice post!');
SQL
comment_count=$(psql -Atc "SELECT COUNT(*) FROM comments" "$DATABASE_URL")
if [ "$comment_count" != "1" ]; then
log error "test failed: ${WHITE}could not insert into new table with foreign keys to existing data"
exit 1
fi
log info "new table works with existing data relationships"
# Test migration rollback with existing database
log info "testing rollback on database with pre-existing tables"
if ! migrator --db-url "$DATABASE_URL" migrate down; then
log error "test failed: ${WHITE}migration down failed"
exit 1
fi
# Verify comments table was removed
if psql -Atc "SELECT COUNT(*) FROM comments" "$DATABASE_URL" >/dev/null 2>&1; then
log error "test failed: ${WHITE}comments table should be removed"
exit 1
fi
# Verify existing tables still intact
final_user_count=$(psql -Atc "SELECT COUNT(*) FROM users" "$DATABASE_URL")
final_post_count=$(psql -Atc "SELECT COUNT(*) FROM posts" "$DATABASE_URL")
if [ "$final_user_count" != "$user_count" ] || [ "$final_post_count" != "$post_count" ]; then
log error "test failed: ${WHITE}existing data affected by rollback"
exit 1
fi
log notice "test passed: migrator works correctly with existing database"

View File

@@ -0,0 +1,3 @@
-- Remove test table
DROP TABLE hectic.test_table;

View File

@@ -0,0 +1,6 @@
-- Add a table to hectic schema alongside user's existing tables
CREATE TABLE hectic.test_table (
id SERIAL PRIMARY KEY,
value TEXT
);

View File

@@ -0,0 +1,73 @@
#!/bin/dash
HECTIC_NAMESPACE=test-migrate-existing-with-conflicts
log notice "test case: ${WHITE}migrator with conflicting existing schema"
# Create a database that already has a 'hectic' schema (potential conflict)
log info "creating existing database with hectic schema"
psql "$DATABASE_URL" -v ON_ERROR_STOP=1 <<SQL
-- User already has something in hectic schema
CREATE SCHEMA hectic;
CREATE TABLE hectic.user_data (
id SERIAL PRIMARY KEY,
data TEXT
);
INSERT INTO hectic.user_data (data) VALUES ('important data');
SQL
# Verify existing hectic data
existing_data=$(psql -Atc "SELECT data FROM hectic.user_data" "$DATABASE_URL")
if [ "$existing_data" != "important data" ]; then
log error "test failed: ${WHITE}existing hectic data not created"
exit 1
fi
log info "existing hectic schema contains user_data table"
# Initialize migrator (should handle existing hectic schema gracefully)
log info "initializing migrator with existing hectic schema"
if ! migrator --db-url "$DATABASE_URL" init; then
log error "test failed: ${WHITE}init failed with existing hectic schema"
exit 1
fi
# Verify migrator tables were created
if ! psql -Atc "SELECT COUNT(*) FROM hectic.migration" "$DATABASE_URL" >/dev/null 2>&1; then
log error "test failed: ${WHITE}hectic.migration not created"
exit 1
fi
if ! psql -Atc "SELECT COUNT(*) FROM hectic.version" "$DATABASE_URL" >/dev/null 2>&1; then
log error "test failed: ${WHITE}hectic.version not created"
exit 1
fi
# Verify existing user data still intact
existing_data_after=$(psql -Atc "SELECT data FROM hectic.user_data" "$DATABASE_URL")
if [ "$existing_data_after" != "$existing_data" ]; then
log error "test failed: ${WHITE}existing hectic.user_data was corrupted"
exit 1
fi
log info "existing hectic.user_data preserved"
# Apply a migration
if ! migrator --db-url "$DATABASE_URL" migrate up; then
log error "test failed: ${WHITE}migration failed"
exit 1
fi
# Verify both user table and migrator tables coexist
tables_in_hectic=$(psql -Atc "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'hectic'" "$DATABASE_URL")
if [ "$tables_in_hectic" -lt 4 ]; then
log error "test failed: ${WHITE}expected at least 4 tables in hectic schema (user_data, migration, version, test_table), got $tables_in_hectic"
exit 1
fi
log info "hectic schema contains $tables_in_hectic tables (user + migrator tables)"
log notice "test passed: migrator coexists with existing hectic schema"

View File

@@ -0,0 +1,2 @@
ALTER TABLE sessions DROP COLUMN user_id;

View File

@@ -0,0 +1,2 @@
ALTER TABLE sessions ADD COLUMN user_id INTEGER;

View File

@@ -0,0 +1,2 @@
ALTER TABLE sessions DROP COLUMN token;

View File

@@ -0,0 +1,2 @@
ALTER TABLE sessions ADD COLUMN token TEXT;

View File

@@ -0,0 +1,2 @@
ALTER TABLE sessions DROP COLUMN expires_at;

View File

@@ -0,0 +1,2 @@
ALTER TABLE sessions ADD COLUMN expires_at TIMESTAMPTZ;

View File

@@ -0,0 +1,53 @@
#!/bin/dash
HECTIC_NAMESPACE=test-migrate-to-backward
log notice "test case: ${WHITE}migrate to (backward) specific migration"
# Create initial schema
psql "$DATABASE_URL" -c 'CREATE TABLE sessions (id INTEGER PRIMARY KEY)'
# Initialize migrator
if ! migrator --db-url "$DATABASE_URL" init; then
log error "test failed: ${WHITE}init failed"
exit 1
fi
# Apply all 3 migrations
if ! migrator --db-url "$DATABASE_URL" migrate up 3; then
log error "test failed: ${WHITE}migrate up failed"
exit 1
fi
# Verify all 3 columns exist
if ! psql -Atc "SELECT user_id, token, expires_at FROM sessions LIMIT 0" "$DATABASE_URL" >/dev/null 2>&1; then
log error "test failed: ${WHITE}not all columns added"
exit 1
fi
# Migrate back to first migration
if ! migrator --db-url "$DATABASE_URL" migrate to 20250101000001-add-user-id; then
log error "test failed: ${WHITE}migrate to (backward) 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, got $applied_count"
exit 1
fi
# Verify only user_id exists
if ! psql -Atc "SELECT user_id FROM sessions LIMIT 0" "$DATABASE_URL" >/dev/null 2>&1; then
log error "test failed: ${WHITE}user_id should exist"
exit 1
fi
if psql -Atc "SELECT token FROM sessions LIMIT 0" "$DATABASE_URL" >/dev/null 2>&1; then
log error "test failed: ${WHITE}token should be removed"
exit 1
fi
log notice "test passed"

View File

@@ -0,0 +1,2 @@
ALTER TABLE comments DROP COLUMN content;

View File

@@ -0,0 +1,2 @@
ALTER TABLE comments ADD COLUMN content TEXT;

View File

@@ -0,0 +1,2 @@
ALTER TABLE comments DROP COLUMN user_id;

View File

@@ -0,0 +1,2 @@
ALTER TABLE comments ADD COLUMN user_id INTEGER;

View File

@@ -0,0 +1,36 @@
#!/bin/dash
HECTIC_NAMESPACE=test-migrate-to-forward
log notice "test case: ${WHITE}migrate to (forward) specific migration"
# Create initial schema
psql "$DATABASE_URL" -c 'CREATE TABLE comments (id INTEGER PRIMARY KEY)'
# Initialize migrator
if ! migrator --db-url "$DATABASE_URL" init; then
log error "test failed: ${WHITE}init failed"
exit 1
fi
# Migrate to second migration (skipping intermediate)
if ! migrator --db-url "$DATABASE_URL" migrate to 20250101000002-add-user-id; then
log error "test failed: ${WHITE}migrate to failed"
exit 1
fi
# Verify 2 migrations were applied
applied_count=$(psql -Atc "SELECT COUNT(*) FROM hectic.migration" "$DATABASE_URL")
if [ "$applied_count" != "2" ]; then
log error "test failed: ${WHITE}expected 2 migrations, got $applied_count"
exit 1
fi
# Verify both columns exist
if ! psql -Atc "SELECT content, user_id FROM comments LIMIT 0" "$DATABASE_URL" >/dev/null 2>&1; then
log error "test failed: ${WHITE}columns not added"
exit 1
fi
log notice "test passed"

View File

@@ -0,0 +1,2 @@
ALTER TABLE posts DROP COLUMN content;

View File

@@ -0,0 +1,2 @@
ALTER TABLE posts ADD COLUMN content TEXT;

View File

@@ -0,0 +1,2 @@
ALTER TABLE posts DROP COLUMN author;

View File

@@ -0,0 +1,2 @@
ALTER TABLE posts ADD COLUMN author TEXT;

View File

@@ -0,0 +1,2 @@
ALTER TABLE posts DROP COLUMN published_at;

View File

@@ -0,0 +1,2 @@
ALTER TABLE posts ADD COLUMN published_at TIMESTAMPTZ;

View File

@@ -0,0 +1,36 @@
#!/bin/dash
HECTIC_NAMESPACE=test-migrate-up-multiple
log notice "test case: ${WHITE}migrate up multiple steps"
# Create initial schema
psql "$DATABASE_URL" -c 'CREATE TABLE posts (id INTEGER PRIMARY KEY, title TEXT)'
# Initialize migrator
if ! migrator --db-url "$DATABASE_URL" init; then
log error "test failed: ${WHITE}init failed"
exit 1
fi
# Apply 3 migrations at once
if ! migrator --db-url "$DATABASE_URL" migrate up 3; then
log error "test failed: ${WHITE}migrate up 3 failed"
exit 1
fi
# Verify all 3 migrations were applied
applied_count=$(psql -Atc "SELECT COUNT(*) FROM hectic.migration" "$DATABASE_URL")
if [ "$applied_count" != "3" ]; then
log error "test failed: ${WHITE}expected 3 migrations, got $applied_count"
exit 1
fi
# Verify all columns were added
if ! psql -Atc "SELECT content, author, published_at FROM posts LIMIT 0" "$DATABASE_URL" >/dev/null 2>&1; then
log error "test failed: ${WHITE}not all columns were added"
exit 1
fi
log notice "test passed"

View File

@@ -0,0 +1,2 @@
ALTER TABLE users DROP COLUMN email;

View File

@@ -0,0 +1,2 @@
ALTER TABLE users ADD COLUMN email TEXT;

View File

@@ -0,0 +1,36 @@
#!/bin/dash
HECTIC_NAMESPACE=test-migrate-up-single
log notice "test case: ${WHITE}migrate up single step"
# Create initial schema
psql "$DATABASE_URL" -c 'CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)'
# Initialize migrator
if ! migrator --db-url "$DATABASE_URL" init; then
log error "test failed: ${WHITE}init failed"
exit 1
fi
# Apply first migration
if ! migrator --db-url "$DATABASE_URL" migrate up; then
log error "test failed: ${WHITE}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 column was added
if ! psql -Atc "SELECT email FROM users LIMIT 0" "$DATABASE_URL" >/dev/null 2>&1; then
log error "test failed: ${WHITE}email column not added"
exit 1
fi
log notice "test passed"

View File

@@ -1 +0,0 @@
ALTER TABLE profile ADD COLUMN info TEXT;

View File

@@ -1,21 +0,0 @@
#!/bin/dash
HECTIC_NAMESPACE=test-migration-list
log notice "test case: ${WHITE}migration up"
psql "$DATABASE_URL" 'CREATE TABLE profile (
id INTEGER,
username TEXT
)'
if ! migrator --db-url "$DATABASE_URL" migrate to 20251104192425-add-info-to-profile; then
log error "test failed: ${WHITE}error on migration up"
exit 1
fi
log notice "$(columns profile)"
exit 1