ci: expand shared package validation and test coverage
- Add lint, formatting, vet, and publish workflow checks - Run Go database tests with PostgreSQL and add Java tests - Make consumer sync opt-in; update docs and Go dependency
This commit is contained in:
1 parent
937f51b4fb
commit
0ff94f9b10
107 files changed
+10124
-410
No files matched your search
@@ -0,0 +1,74 @@
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
"time"
|
||||
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
)
|
||||
|
||||
func TestUpRefusesMissingInputsBeforeConnecting(t *testing.T) {
|
||||
files := fstest.MapFS{"migrations/001_init.sql": {Data: []byte("-- +goose Up\nSELECT 1;\n")}}
|
||||
if err := Up(context.Background(), "", files, "migrations"); err == nil || !strings.Contains(err.Error(), "database URL is required") {
|
||||
t.Fatalf("empty URL error = %v", err)
|
||||
}
|
||||
if err := Up(context.Background(), "postgres://localhost/none", nil, "migrations"); err == nil || !strings.Contains(err.Error(), "migration filesystem is required") {
|
||||
t.Fatalf("nil filesystem error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpReportsAnUnreachableDatabase(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
files := fstest.MapFS{"migrations/001_init.sql": {Data: []byte("-- +goose Up\nSELECT 1;\n")}}
|
||||
err := Up(ctx, "postgres://nobody@127.0.0.1:1/none?sslmode=disable&connect_timeout=2", files, "migrations")
|
||||
if err == nil || !strings.Contains(err.Error(), "ping migration database") {
|
||||
t.Fatalf("unreachable database error = %v, want a ping failure", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUpAppliesMigrationsOnce runs real migrations. It creates goose_db_version and a table, so it
|
||||
// is skipped unless SHARED_TEST_DATABASE_URL points at a scratch database, for example:
|
||||
//
|
||||
// SHARED_TEST_DATABASE_URL=postgres://user:pass@localhost:5432/scratch?sslmode=disable go test ./migrate/
|
||||
func TestUpAppliesMigrationsOnce(t *testing.T) {
|
||||
url := os.Getenv("SHARED_TEST_DATABASE_URL")
|
||||
if url == "" {
|
||||
t.Skip("SHARED_TEST_DATABASE_URL not set; skipping Postgres migration test")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
db, err := sql.Open("pgx", url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
t.Cleanup(func() {
|
||||
_, _ = db.Exec(`DROP TABLE IF EXISTS migrate_test_widgets; DROP TABLE IF EXISTS goose_db_version`)
|
||||
})
|
||||
_, _ = db.Exec(`DROP TABLE IF EXISTS migrate_test_widgets; DROP TABLE IF EXISTS goose_db_version`)
|
||||
|
||||
files := fstest.MapFS{
|
||||
"migrations/001_widgets.sql": {Data: []byte("-- +goose Up\nCREATE TABLE migrate_test_widgets (id INT PRIMARY KEY);\n-- +goose Down\nDROP TABLE migrate_test_widgets;\n")},
|
||||
"migrations/002_seed.sql": {Data: []byte("-- +goose Up\nINSERT INTO migrate_test_widgets (id) VALUES (1);\n-- +goose Down\nDELETE FROM migrate_test_widgets;\n")},
|
||||
}
|
||||
for run := 1; run <= 2; run++ {
|
||||
if err := Up(ctx, url, files, "migrations"); err != nil {
|
||||
t.Fatalf("run %d: Up() error = %v", run, err)
|
||||
}
|
||||
}
|
||||
var rows int
|
||||
if err := db.QueryRowContext(ctx, `SELECT count(*) FROM migrate_test_widgets`).Scan(&rows); err != nil || rows != 1 {
|
||||
t.Fatalf("widgets = %d, %v; want the seed applied exactly once", rows, err)
|
||||
}
|
||||
|
||||
broken := fstest.MapFS{"migrations/003_broken.sql": {Data: []byte("-- +goose Up\nSELECT * FROM no_such_table;\n")}}
|
||||
if err := Up(ctx, url, broken, "migrations"); err == nil || !strings.Contains(err.Error(), "run migrations") {
|
||||
t.Fatalf("broken migration error = %v, want a run-migrations failure", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user