Skip to content

Commit

Permalink
Fix sync rule
Browse files Browse the repository at this point in the history
  • Loading branch information
dz0ny committed Aug 15, 2023
1 parent cd41314 commit f03db14
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pg-subsetter

[![lint](https://github.com/teamniteo/pg-subsetter/actions/workflows/lint.yml/badge.svg)](https://github.com/teamniteo/pg-subsetter/actions/workflows/lint.yml) [![build](https://github.com/teamniteo/pg-subsetter/actions/workflows/go.yml/badge.svg)](https://github.com/teamniteo/pg-subsetter/actions/workflows/go.yml) [![vuln](https://github.com/teamniteo/pg-subsetter/actions/workflows/vuln.yml/badge.svg)](https://github.com/teamniteo/pg-subsetter/actions/workflows/vuln.yml)
[![lint](https://github.com/teamniteo/pg-subsetter/actions/workflows/lint.yml/badge.svg)](https://github.com/teamniteo/pg-subsetter/actions/workflows/lint.yml) [![build](https://github.com/teamniteo/pg-subsetter/actions/workflows/go.yml/badge.svg)](https://github.com/teamniteo/pg-subsetter/actions/workflows/go.yml) [![vuln](https://github.com/teamniteo/pg-subsetter/actions/workflows/vuln.yml/badge.svg)](https://github.com/teamniteo/pg-subsetter/actions/workflows/vuln.yml) [![release](https://github.com/teamniteo/pg-subsetter/actions/workflows/release.yml/badge.svg)](https://github.com/teamniteo/pg-subsetter/actions/workflows/release.yml)


`pg-subsetter` is a tool designed to synchronize a fraction of a PostgreSQL database to another PostgreSQL database on the fly, it does not copy the SCHEMA.
Expand Down Expand Up @@ -47,7 +47,7 @@ Usage of subsetter:
Prepare schema in target database:

```bash
pg_dump --schema-only -n public -f schemadump.sql "postgres://test_source@localhost:5432/test_source?sslmode=disable"
pg_dump --schema-only --no-owner --no-acl -n public -f schemadump.sql "postgres://test_source@localhost:5432/test_source?sslmode=disable"
psql -f schemadump.sql "postgres://test_target@localhost:5432/test_target?sslmode=disable"
```

Expand Down
10 changes: 9 additions & 1 deletion subsetter/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package subsetter

import (
"context"
"fmt"
"sort"

"github.com/jackc/pgx/v5/pgconn"
Expand All @@ -25,6 +26,13 @@ type Rule struct {
Where string
}

func (r Rule) Query() string {
if r.Where == "" {
return fmt.Sprintf("SELECT * FROM %s", r.Table)
}
return fmt.Sprintf("SELECT * FROM %s WHERE %s", r.Table, r.Where)
}

func NewSync(source string, target string, fraction float64, include []Rule, exclude []Rule, verbose bool) (*Sync, error) {
src, err := pgxpool.New(context.Background(), source)
if err != nil {
Expand Down Expand Up @@ -119,7 +127,7 @@ func (s *Sync) CopyTables(tables []Table) (err error) {
if include.Table == table.Name {
log.Info().Str("query", include.Where).Msgf("Selecting forced rows for table %s", table.Name)
var data string
if data, err = CopyQueryToString(include.Where, s.source); err != nil {
if data, err = CopyQueryToString(include.Query(), s.source); err != nil {
return errors.Wrapf(err, "Error copying forced rows for table %s", table.Name)
}
if err = CopyStringToTable(table.Name, data, s.destination); err != nil {
Expand Down

0 comments on commit f03db14

Please sign in to comment.