feat(package): migrator: ! sqlite support

This commit is contained in:
2025-12-17 03:24:59 +00:00
parent bb2ae34758
commit 956239ab79
79 changed files with 608 additions and 59 deletions

View File

@@ -2,18 +2,6 @@
HECTIC_NAMESPACE=test-init-migrator
### CASE 1
log notice "test case: ${WHITE}dry run"
# NOTE: does not matter exist inherits tables or not, it must not connect to db
if ! migration_table_sql="$(migrator --inherits tablename --inherits 'table name' init --dry-run)"; then
log error "test failed: ${WHITE}error on migration table init dry run"
exit 1
fi
printf '%s' "$migration_table_sql" | grep -Eq 'INHERITS[[:space:]]*\([[:space:]]*"tablename"[[:space:]]*,[[:space:]]*"table name"[[:space:]]*\)' ||
{ log error "test failed: ${WHITE}not correct migration table inherits"; exit 1; }
### CASE 2
log notice "test case: ${WHITE}error: table inherit tables that not exists"

View File

@@ -0,0 +1,6 @@
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);

View File

@@ -0,0 +1,12 @@
-- SQLite doesn't support DROP COLUMN directly before 3.35.0
-- We need to recreate the table
CREATE TABLE users_new (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);
INSERT INTO users_new (id, name) SELECT id, name FROM users;
DROP TABLE users;
ALTER TABLE users_new RENAME TO users;

View File

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

View File

@@ -0,0 +1,86 @@
#!/bin/dash
HECTIC_NAMESPACE=test-sqlite-basic
log notice "test case: ${WHITE}SQLite basic migration"
# Create SQLite database
SQLITE_DB="$PWD/test.db"
export DB_URL="sqlite://$SQLITE_DB"
log info "using SQLite database: $SQLITE_DB"
# Initialize migrator with SQLite
if ! migrator --db-url "$DB_URL" init; then
log error "test failed: ${WHITE}init failed for SQLite"
exit 1
fi
# Verify tables were created
if ! sqlite3 "$SQLITE_DB" "SELECT name FROM hectic_version WHERE name = 'migrator'" >/dev/null 2>&1; then
log error "test failed: ${WHITE}hectic_version table not created"
exit 1
fi
if ! sqlite3 "$SQLITE_DB" "SELECT COUNT(*) FROM hectic_migration" >/dev/null 2>&1; then
log error "test failed: ${WHITE}hectic_migration table not created"
exit 1
fi
log info "migrator tables created successfully"
# Apply first migration
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 users" >/dev/null 2>&1; then
log error "test failed: ${WHITE}users 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 email column exists
if ! sqlite3 "$SQLITE_DB" "SELECT email FROM users LIMIT 0" >/dev/null 2>&1; then
log error "test failed: ${WHITE}email column not added"
exit 1
fi
# Migrate down
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 email column removed
if sqlite3 "$SQLITE_DB" "SELECT email FROM users LIMIT 0" >/dev/null 2>&1; then
log error "test failed: ${WHITE}email column should be removed"
exit 1
fi
log notice "test passed: SQLite support works correctly"