fix: make sure to dump everything from postgres databases
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
96d61fd6e0
commit
1a18797af8
|
@ -1,7 +1,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/client"
|
||||
"log"
|
||||
|
@ -17,7 +16,9 @@ func NewPostgresDumper(container *types.ContainerJSON) *PostgresDumper {
|
|||
}
|
||||
}
|
||||
|
||||
func (d *PostgresDumper) buildConnStr() string {
|
||||
func (d *PostgresDumper) Dump(c *client.Client) error {
|
||||
log.Printf("Dumping postgres database from container %s...\n", d.Container.Name)
|
||||
|
||||
env := parseEnv(d.Container.Config.Env)
|
||||
|
||||
user := "root"
|
||||
|
@ -25,30 +26,5 @@ func (d *PostgresDumper) buildConnStr() string {
|
|||
user = u
|
||||
}
|
||||
|
||||
db := "postgres"
|
||||
if d, has := env["POSTGRES_DB"]; has {
|
||||
db = d
|
||||
}
|
||||
|
||||
pw := env["POSTGRES_ROOT_PASSWORD"]
|
||||
if p, has := env["POSTGRES_PASSWORD"]; has {
|
||||
pw = p
|
||||
}
|
||||
|
||||
port := "5432"
|
||||
if p, has := env["POSTGRES_PORT"]; has {
|
||||
port = p
|
||||
}
|
||||
|
||||
host := d.Container.NetworkSettings.DefaultNetworkSettings.IPAddress
|
||||
|
||||
return fmt.Sprintf("postgresql://%s:%s@%s:%s/%s", user, pw, host, port, db)
|
||||
}
|
||||
|
||||
func (d *PostgresDumper) Dump(c *client.Client) error {
|
||||
log.Printf("Dumping postgres database from container %s...\n", d.Container.Name)
|
||||
|
||||
connStr := d.buildConnStr()
|
||||
|
||||
return runAndSaveCommandInContainer(getDumpFilename(d.Container.Name), c, d.Container, "pg_dump", "--dbname", connStr)
|
||||
return runAndSaveCommandInContainer(getDumpFilename(d.Container.Name), c, d.Container, "pg_dumpall", "-U", user)
|
||||
}
|
||||
|
|
|
@ -1,117 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPostgresDumper_buildConnStr(t *testing.T) {
|
||||
nw := &types.NetworkSettings{
|
||||
DefaultNetworkSettings: types.DefaultNetworkSettings{
|
||||
IPAddress: "1.2.3.4",
|
||||
},
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
container *types.ContainerJSON
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "values for everything",
|
||||
container: &types.ContainerJSON{
|
||||
NetworkSettings: nw,
|
||||
Config: &container.Config{
|
||||
Env: []string{
|
||||
"POSTGRES_USER=loremipsum",
|
||||
"POSTGRES_DB=ipsum",
|
||||
"POSTGRES_PASSWORD=notapassword",
|
||||
"POSTGRES_PORT=1234",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: "postgresql://loremipsum:notapassword@1.2.3.4:1234/ipsum",
|
||||
},
|
||||
{
|
||||
name: "no user",
|
||||
container: &types.ContainerJSON{
|
||||
NetworkSettings: nw,
|
||||
Config: &container.Config{
|
||||
Env: []string{
|
||||
"POSTGRES_DB=ipsum",
|
||||
"POSTGRES_PASSWORD=notapassword",
|
||||
"POSTGRES_PORT=1234",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: "postgresql://root:notapassword@1.2.3.4:1234/ipsum",
|
||||
},
|
||||
{
|
||||
name: "no password",
|
||||
container: &types.ContainerJSON{
|
||||
NetworkSettings: nw,
|
||||
Config: &container.Config{
|
||||
Env: []string{
|
||||
"POSTGRES_USER=loremipsum",
|
||||
"POSTGRES_DB=ipsum",
|
||||
"POSTGRES_PORT=1234",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: "postgresql://loremipsum:@1.2.3.4:1234/ipsum",
|
||||
},
|
||||
{
|
||||
name: "no password, but root",
|
||||
container: &types.ContainerJSON{
|
||||
NetworkSettings: nw,
|
||||
Config: &container.Config{
|
||||
Env: []string{
|
||||
"POSTGRES_USER=loremipsum",
|
||||
"POSTGRES_DB=ipsum",
|
||||
"POSTGRES_PORT=1234",
|
||||
"POSTGRES_ROOT_PASSWORD=roooot",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: "postgresql://loremipsum:roooot@1.2.3.4:1234/ipsum",
|
||||
},
|
||||
{
|
||||
name: "no port",
|
||||
container: &types.ContainerJSON{
|
||||
NetworkSettings: nw,
|
||||
Config: &container.Config{
|
||||
Env: []string{
|
||||
"POSTGRES_USER=loremipsum",
|
||||
"POSTGRES_DB=ipsum",
|
||||
"POSTGRES_PASSWORD=notapassword",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: "postgresql://loremipsum:notapassword@1.2.3.4:5432/ipsum",
|
||||
},
|
||||
{
|
||||
name: "no db",
|
||||
container: &types.ContainerJSON{
|
||||
NetworkSettings: nw,
|
||||
Config: &container.Config{
|
||||
Env: []string{
|
||||
"POSTGRES_USER=loremipsum",
|
||||
"POSTGRES_PASSWORD=notapassword",
|
||||
"POSTGRES_PORT=1234",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: "postgresql://loremipsum:notapassword@1.2.3.4:1234/postgres",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
d := &PostgresDumper{
|
||||
Container: tt.container,
|
||||
}
|
||||
if got := d.buildConnStr(); got != tt.want {
|
||||
t.Errorf("buildConnStr() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue