mirror of
https://github.com/coder/coder.git
synced 2025-07-15 22:20:27 +00:00
chore(coderd/database/gen/dump): add optional DB_DUMP_CONNECTION_URL (#16243)
This commit is contained in:
committed by
GitHub
parent
5f4ff58f84
commit
f27e73d21a
@ -7,6 +7,8 @@ import (
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"github.com/coder/coder/v2/coderd/database/dbtestutil"
|
||||
"github.com/coder/coder/v2/coderd/database/migrations"
|
||||
)
|
||||
@ -37,25 +39,34 @@ func main() {
|
||||
}
|
||||
}()
|
||||
|
||||
connection, cleanup, err := dbtestutil.OpenContainerized(t, dbtestutil.DBContainerOptions{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
connection := os.Getenv("DB_DUMP_CONNECTION_URL")
|
||||
if connection == "" {
|
||||
var cleanup func()
|
||||
var err error
|
||||
connection, cleanup, err = dbtestutil.OpenContainerized(t, dbtestutil.DBContainerOptions{})
|
||||
if err != nil {
|
||||
err = xerrors.Errorf("open containerized database failed: %w", err)
|
||||
panic(err)
|
||||
}
|
||||
defer cleanup()
|
||||
}
|
||||
defer cleanup()
|
||||
|
||||
db, err := sql.Open("postgres", connection)
|
||||
if err != nil {
|
||||
err = xerrors.Errorf("open database failed: %w", err)
|
||||
panic(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
err = migrations.Up(db)
|
||||
if err != nil {
|
||||
err = xerrors.Errorf("run migrations failed: %w", err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
dumpBytes, err := dbtestutil.PGDumpSchemaOnly(connection)
|
||||
if err != nil {
|
||||
err = xerrors.Errorf("dump schema failed: %w", err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@ -65,6 +76,7 @@ func main() {
|
||||
}
|
||||
err = os.WriteFile(filepath.Join(mainPath, "..", "..", "..", "dump.sql"), append(preamble, dumpBytes...), 0o600)
|
||||
if err != nil {
|
||||
err = xerrors.Errorf("write dump failed: %w", err)
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
@ -72,6 +72,11 @@ the following tools by hand:
|
||||
- [`pg_dump`](https://stackoverflow.com/a/49689589)
|
||||
- on macOS, run `brew install libpq zstd`
|
||||
- on Linux, install [`zstd`](https://github.com/horta/zstd.install)
|
||||
- PostgreSQL 13 (optional if Docker is available)
|
||||
- *Note*: If you are using Docker, you can skip this step
|
||||
- on macOS, run `brew install postgresql@13` and `brew services start postgresql@13`
|
||||
- To enable schema generation with non-containerized PostgreSQL, set the following environment variable:
|
||||
- `export DB_DUMP_CONNECTION_URL="postgres://postgres@localhost:5432/postgres?password=postgres&sslmode=disable"`
|
||||
- `pkg-config`
|
||||
- on macOS, run `brew install pkg-config`
|
||||
- `pixman`
|
||||
|
Reference in New Issue
Block a user