Skip optional demo seeding safely during production deployments
This commit is contained in:
parent
2e7f24e7f7
commit
fa646a6243
|
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
|
@ -16,10 +17,15 @@ func main() {
|
|||
environment := flag.String("environment", "", "Required: development, test, or staging")
|
||||
flag.Parse()
|
||||
_ = godotenv.Load()
|
||||
if err := demoseed.ValidateEnvironment(*environment, os.Getenv("APP_ENV"), os.Getenv("ENVIRONMENT"), os.Getenv("GO_ENV"), os.Getenv("ENV")); err != nil {
|
||||
skip, err := seedEnvironmentPolicy(*environment, os.Getenv)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if skip {
|
||||
fmt.Println("Skipping demo seed: production environment detected; no database connection or demo data changes were made.")
|
||||
return
|
||||
}
|
||||
url := os.Getenv("DB_URL")
|
||||
if url == "" {
|
||||
fmt.Fprintln(os.Stderr, "DB_URL is required")
|
||||
|
|
@ -51,3 +57,13 @@ func main() {
|
|||
fmt.Printf(" %-36s %-16s %s\n", a.Email, a.Role, a.Audience)
|
||||
}
|
||||
}
|
||||
|
||||
// Production skips are successful no-ops for deployment runners that invoke the
|
||||
// optional seed step unconditionally. Invalid command targets still fail.
|
||||
func seedEnvironmentPolicy(target string, getenv func(string) string) (bool, error) {
|
||||
err := demoseed.ValidateEnvironment(target, getenv("APP_ENV"), getenv("ENVIRONMENT"), getenv("GO_ENV"), getenv("ENV"))
|
||||
if errors.Is(err, demoseed.ErrProductionEnvironment) {
|
||||
return true, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
|
|
|||
33
cmd/demo-seed/main_test.go
Normal file
33
cmd/demo-seed/main_test.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSeedEnvironmentPolicy(t *testing.T) {
|
||||
for _, key := range []string{"APP_ENV", "ENVIRONMENT", "GO_ENV", "ENV"} {
|
||||
for _, value := range []string{"production", "prod", " PRODUCTION "} {
|
||||
t.Run(key+"/"+value, func(t *testing.T) {
|
||||
skip, err := seedEnvironmentPolicy("development", func(name string) string {
|
||||
if name == key {
|
||||
return value
|
||||
}
|
||||
return ""
|
||||
})
|
||||
if err != nil || !skip {
|
||||
t.Fatalf("production must skip without failure: skip=%v err=%v", skip, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
for _, target := range []string{"development", "test", "staging"} {
|
||||
skip, err := seedEnvironmentPolicy(target, func(string) string { return "" })
|
||||
if err != nil || skip {
|
||||
t.Fatalf("%s should seed: skip=%v err=%v", target, skip, err)
|
||||
}
|
||||
}
|
||||
for _, target := range []string{"", "production", "invalid"} {
|
||||
skip, err := seedEnvironmentPolicy(target, func(string) string { return "production" })
|
||||
if err == nil || skip {
|
||||
t.Fatalf("invalid target %q must fail", target)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -58,7 +58,11 @@ The command reads `DB_URL` from the environment or the backend `.env`. It needs
|
|||
only PostgreSQL; it does not start the application or require payment, email,
|
||||
SMS, or storage provider credentials. `-environment` must explicitly be
|
||||
`development`, `test`, or `staging`. `APP_ENV`, `ENVIRONMENT`, `GO_ENV`, or `ENV` set to
|
||||
`production`/`prod` prevents seeding.
|
||||
`production`/`prod` prevents seeding. The CLI logs a skip and exits successfully
|
||||
before reading database configuration or opening a connection, so an unconditional
|
||||
optional seed step does not block a production release. An invalid or missing
|
||||
`-environment` remains an error. Production deployment scripts should omit demo
|
||||
seeding altogether; this behavior is a compatibility safeguard for existing runners.
|
||||
|
||||
For the repository's Docker PostgreSQL:
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package demoseed
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
|
|
@ -13,6 +14,9 @@ import (
|
|||
|
||||
const DefaultPassword = "RezinaDemo!2026"
|
||||
|
||||
// ErrProductionEnvironment prevents demo fixtures from being written to production.
|
||||
var ErrProductionEnvironment = errors.New("demo seeding is disabled in production")
|
||||
|
||||
type Account struct{ Email, Role, Audience, Name string }
|
||||
|
||||
var Accounts = []Account{
|
||||
|
|
@ -35,7 +39,7 @@ func ValidateEnvironment(target string, settings ...string) error {
|
|||
for _, value := range settings {
|
||||
value = strings.ToLower(strings.TrimSpace(value))
|
||||
if value == "production" || value == "prod" {
|
||||
return fmt.Errorf("demo seeding is disabled in production")
|
||||
return ErrProductionEnvironment
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user