PGDumpSchemaOnly is for use by gen/dump only. It runs pg_dump against dbURL and sets a consistent timezone and encoding.
(dbURL string)
| 246 | // PGDumpSchemaOnly is for use by gen/dump only. |
| 247 | // It runs pg_dump against dbURL and sets a consistent timezone and encoding. |
| 248 | func PGDumpSchemaOnly(dbURL string) ([]byte, error) { |
| 249 | hasPGDump := false |
| 250 | if _, err := exec.LookPath("pg_dump"); err == nil { |
| 251 | out, err := exec.Command("pg_dump", "--version").Output() |
| 252 | if err == nil { |
| 253 | // Parse output: |
| 254 | // pg_dump (PostgreSQL) 14.5 (Ubuntu 14.5-0ubuntu0.22.04.1) |
| 255 | parts := strings.Split(string(out), " ") |
| 256 | if len(parts) > 2 { |
| 257 | version, err := strconv.Atoi(strings.Split(parts[2], ".")[0]) |
| 258 | if err == nil && version >= minimumPostgreSQLVersion { |
| 259 | hasPGDump = true |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | cmdArgs := []string{ |
| 266 | "pg_dump", |
| 267 | "--schema-only", |
| 268 | dbURL, |
| 269 | "--no-privileges", |
| 270 | "--no-owner", |
| 271 | "--no-privileges", |
| 272 | "--no-publication", |
| 273 | "--no-security-labels", |
| 274 | "--no-subscriptions", |
| 275 | "--no-tablespaces", |
| 276 | |
| 277 | // We never want to manually generate |
| 278 | // queries executing against this table. |
| 279 | "--exclude-table=schema_migrations", |
| 280 | } |
| 281 | |
| 282 | if !hasPGDump { |
| 283 | cmdArgs = append([]string{ |
| 284 | "docker", |
| 285 | "run", |
| 286 | "--rm", |
| 287 | "--network=host", |
| 288 | fmt.Sprintf("%s:%d", postgresImage, minimumPostgreSQLVersion), |
| 289 | }, cmdArgs...) |
| 290 | } |
| 291 | cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) //#nosec |
| 292 | cmd.Env = append(os.Environ(), []string{ |
| 293 | "PGTZ=UTC", |
| 294 | "PGCLIENTENCODING=UTF8", |
| 295 | }...) |
| 296 | var output bytes.Buffer |
| 297 | cmd.Stdout = &output |
| 298 | cmd.Stderr = os.Stderr |
| 299 | err := cmd.Run() |
| 300 | if err != nil { |
| 301 | return nil, err |
| 302 | } |
| 303 | return normalizeDump(output.Bytes()), nil |
| 304 | } |
| 305 |