()
| 49 | } |
| 50 | |
| 51 | func main() { |
| 52 | t := &mockTB{} |
| 53 | |
| 54 | // Ensure cleanups run on both normal exit and SIGINT/SIGTERM. |
| 55 | // Go's default signal handlers call os.Exit, which skips deferred |
| 56 | // funcs and would leave an embedded-postgres daemon orphaned. |
| 57 | var cleanupOnce sync.Once |
| 58 | runCleanup := func() { |
| 59 | cleanupOnce.Do(func() { |
| 60 | for _, f := range t.cleanup { |
| 61 | f() |
| 62 | } |
| 63 | }) |
| 64 | } |
| 65 | defer runCleanup() |
| 66 | |
| 67 | sigCh := make(chan os.Signal, 1) |
| 68 | signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM) |
| 69 | go func() { |
| 70 | <-sigCh |
| 71 | runCleanup() |
| 72 | os.Exit(130) |
| 73 | }() |
| 74 | |
| 75 | connection := os.Getenv("DB_DUMP_CONNECTION_URL") |
| 76 | if connection == "" { |
| 77 | var cleanup func() |
| 78 | var err error |
| 79 | connection, cleanup, err = dbtestutil.OpenContainerized(t, dbtestutil.DBContainerOptions{}) |
| 80 | if err != nil { |
| 81 | _, _ = fmt.Fprintf(os.Stderr, "containerized postgres unavailable (%s); falling back to embedded postgres\n", err) |
| 82 | connection, cleanup, err = openEmbeddedPostgres() |
| 83 | if err != nil { |
| 84 | panic(err) |
| 85 | } |
| 86 | } |
| 87 | t.Cleanup(cleanup) |
| 88 | } |
| 89 | |
| 90 | db, err := sql.Open("postgres", connection) |
| 91 | if err != nil { |
| 92 | err = xerrors.Errorf("open database failed: %w", err) |
| 93 | panic(err) |
| 94 | } |
| 95 | defer db.Close() |
| 96 | |
| 97 | err = migrations.Up(db) |
| 98 | if err != nil { |
| 99 | err = xerrors.Errorf("run migrations failed: %w", err) |
| 100 | panic(err) |
| 101 | } |
| 102 | |
| 103 | dumpBytes, err := dbtestutil.PGDumpSchemaOnly(connection) |
| 104 | if err != nil { |
| 105 | if !pgDumpUsable() { |
| 106 | _, _ = fmt.Fprintf(os.Stderr, |
| 107 | "\nThis step needs pg_dump (PostgreSQL v13 or later) on PATH OR a Docker-compatible daemon.\n"+ |
| 108 | "Install pg_dump locally to avoid Docker:\n"+ |
nothing calls this directly
no test coverage detected