test: add self-contained integration environment
Separate schema migrations from environment-specific fixtures, run the full API workflow against a disposable PostgreSQL Testcontainer, and provide seeded development and UI test environments. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
6153017b62
commit
6969f79015
18 changed files with 772 additions and 269 deletions
38
backend/internal/database/seed/seed.go
Normal file
38
backend/internal/database/seed/seed.go
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
package seed
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"embed"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
//go:embed *.sql
|
||||
var files embed.FS
|
||||
|
||||
func Apply(ctx context.Context, db *sql.DB, environment string) error {
|
||||
if _, err := apply(ctx, db, "common.sql"); err != nil {
|
||||
return err
|
||||
}
|
||||
switch environment {
|
||||
case "", "production":
|
||||
return nil
|
||||
case "development", "test":
|
||||
_, err := apply(ctx, db, environment+".sql")
|
||||
return err
|
||||
default:
|
||||
return fmt.Errorf("unsupported APP_ENV %q", environment)
|
||||
}
|
||||
}
|
||||
|
||||
func apply(ctx context.Context, db *sql.DB, filename string) (sql.Result, error) {
|
||||
content, err := files.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read seed %s: %w", filename, err)
|
||||
}
|
||||
result, err := db.ExecContext(ctx, string(content))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("apply seed %s: %w", filename, err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue