-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lucas Golino
committed
Sep 2, 2023
1 parent
681e8dc
commit 384e880
Showing
8 changed files
with
153 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
export PROJECT_ID=PROJECT_ID | ||
export GCS_BUCKET_NAME=GCS_BUCKET_NAME | ||
|
||
export PSQL_HOST=PSQL_HOST | ||
export PSQL_PORT=PSQL_PORT | ||
export PSQL_DBNAME=PSQL_DBNAME | ||
export PSQL_USER=PSQL_USER | ||
export PSQL_PASSWORD=PSQL_PASSWORD | ||
|
||
source ./.envrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package psql | ||
|
||
import ( | ||
"fmt" | ||
pg "github.com/habx/pg-commands" | ||
"os" | ||
"strconv" | ||
) | ||
|
||
type Config struct { | ||
Host string | ||
Port int | ||
DBName string | ||
User string | ||
Password string | ||
} | ||
|
||
type Psql struct { | ||
Cfg Config | ||
} | ||
|
||
func NewDriver() *Psql { | ||
driver := Psql{} | ||
driver.config() | ||
|
||
return &driver | ||
} | ||
|
||
func (p *Psql) config() { | ||
var port int | ||
var err error | ||
var stringPort = os.Getenv("PSQL_PORT") | ||
|
||
if port, err = strconv.Atoi(stringPort); err != nil { | ||
port = 5432 | ||
} | ||
|
||
p.Cfg = Config{ | ||
Host: os.Getenv("PSQL_HOST"), | ||
Port: port, | ||
DBName: os.Getenv("PSQL_DBNAME"), | ||
User: os.Getenv("PSQL_USER"), | ||
Password: os.Getenv("PSQL_PASSWORD"), | ||
} | ||
} | ||
|
||
func (p *Psql) Get() (string, error) { | ||
dump, err := pg.NewDump(&pg.Postgres{ | ||
Host: p.Cfg.Host, | ||
Port: p.Cfg.Port, | ||
DB: p.Cfg.DBName, | ||
Username: p.Cfg.User, | ||
Password: p.Cfg.Password, | ||
}) | ||
|
||
if err != nil { | ||
return "", fmt.Errorf("failed to create dump connection: %w", err) | ||
} | ||
|
||
dir := fmt.Sprintf("%s/", os.TempDir()) | ||
dump.SetPath(dir) | ||
|
||
dumpExec := dump.Exec(pg.ExecOptions{ | ||
StreamPrint: false, | ||
}) | ||
|
||
if dumpExec.Error != nil { | ||
fmt.Printf("COMMAND: %s\n", dumpExec.FullCommand) | ||
fmt.Printf("ERROR: %s\n", dumpExec.Output) | ||
return "", fmt.Errorf("failed to dump database: %s", dumpExec.Error.Err) | ||
} | ||
|
||
fmt.Printf("Successfully dumped database %s\n", dumpExec.File) | ||
|
||
return fmt.Sprintf("%s%s", dir, dump.GetFileName()), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package psync | ||
|
||
type Driver interface { | ||
Get() (string, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters