Skip to content

Commit

Permalink
feat(dump): Include dbname in generated filename
Browse files Browse the repository at this point in the history
Fixes #126
  • Loading branch information
gabe565 committed Feb 17, 2024
1 parent d65e0f3 commit df4e5e0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions internal/actions/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Dump struct {
func (action Dump) Run(ctx context.Context) (err error) {
if action.Filename == "" {
action.Filename = Filename{
Database: action.Database,
Namespace: action.Client.Namespace,
Ext: action.Dialect.DumpExtension(action.Format),
Date: time.Now(),
Expand Down
11 changes: 10 additions & 1 deletion internal/actions/dump/generate_filename.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@ import (
const DateFormat = "2006-01-02_150405"

type Filename struct {
Database string
Namespace string
Ext string
Date time.Time
}

func (vars Filename) Generate() string {
return vars.Namespace + "_" + vars.Date.Format(DateFormat) + vars.Ext
result := vars.Namespace + "_"
switch vars.Database {
case "":
case vars.Namespace:
default:
result += vars.Database + "_"
}
result += vars.Date.Format(DateFormat) + vars.Ext
return result
}

func HelpFilename() string {
Expand Down
6 changes: 5 additions & 1 deletion internal/actions/dump/generate_filename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

func TestFilename_Generate(t *testing.T) {
type fields struct {
Database string
Namespace string
Ext string
Date time.Time
Expand All @@ -19,11 +20,14 @@ func TestFilename_Generate(t *testing.T) {
want string
wantErr bool
}{
{"simple", fields{"test", ".sql.gz", time.Time{}}, "test_0001-01-01_000000.sql.gz", false},
{"no database", fields{"", "test", ".sql.gz", time.Time{}}, "test_0001-01-01_000000.sql.gz", false},
{"with database", fields{"postgres", "test", ".sql.gz", time.Time{}}, "test_postgres_0001-01-01_000000.sql.gz", false},
{"database matches namespace", fields{"test", "test", ".sql.gz", time.Time{}}, "test_0001-01-01_000000.sql.gz", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
vars := Filename{
Database: tt.fields.Database,
Namespace: tt.fields.Namespace,
Ext: tt.fields.Ext,
Date: tt.fields.Date,
Expand Down

0 comments on commit df4e5e0

Please sign in to comment.