Skip to content

Commit

Permalink
feat: Refactor CLI code to handle force sync option
Browse files Browse the repository at this point in the history
- Modified `String` method of `arrayForce` to join strings with a comma
- Added import statements for packages "os" and "github.com/rs/zerolog"
- Initialized logger with ConsoleWriter for standard error output
- Added command line flag for "force" option
- Logged message with tables to force sync if length of "forceSync" array is greater than 0
  • Loading branch information
dz0ny committed Aug 12, 2023
1 parent 8ae0c5e commit db17409
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,26 @@ Utilizing the native PostgreSQL COPY command, pg-subsetter performs data transfe
```
Usage of subsetter:
-dst string
Destination DSN
Destination database DSN
-f float
Fraction of rows to copy (default 0.05)
-force value
Query to copy required tables (users: id = 1)
-src string
Source DSN
Source database DSN
```


Example:
### Example

```pg-subsetter -src postgresql://:@/bigdb -dst postgresql://:@/littledb -f 0.05```
```
pg-subsetter
-src postgresql://:@/bigdb
-dst postgresql://:@/littledb
-f 0.05
-force "users: id = 1"
-force "groups: id = 12"
```

# Installing

Expand Down
27 changes: 27 additions & 0 deletions cli/force.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"fmt"
"strings"
)

type Force struct {
Table string
Where string
}

type arrayForce []Force

func (i *arrayForce) String() string {
return fmt.Sprintf("%v", *i)
}

func (i *arrayForce) Set(value string) error {
q := strings.SplitAfter(strings.TrimSpace(value), ":")

*i = append(*i, Force{
Table: q[0],
Where: q[1],
})
return nil
}
11 changes: 10 additions & 1 deletion cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ package main

import (
"flag"
"os"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

var src = flag.String("src", "", "Source database DSN")
var dst = flag.String("dst", "", "Destination database DSN")
var fraction = flag.Float64("f", 0.05, "Fraction of rows to copy")
var forceSync arrayForce

func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})

flag.Var(&forceSync, "force", "Query to copy required tables (users: id = 1)")
flag.Parse()
log.Info().Msg("Starting")

if *src == "" || *dst == "" {
log.Fatal().Msg("Source and destination DSNs are required")
Expand All @@ -22,4 +27,8 @@ func main() {
log.Fatal().Msg("Fraction must be between 0 and 1")
}

if len(forceSync) > 0 {
log.Info().Msg("Forcing sync for tables: " + forceSync.String())
}

}
1 change: 0 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
golangci-lint
postgresql
process-compose
shellcheck
nixpkgs-fmt
pgweb
];
Expand Down

0 comments on commit db17409

Please sign in to comment.