From 0fd2ea34a52ee83a2e9dcedb05bf43a414513260 Mon Sep 17 00:00:00 2001 From: Janez Troha Date: Fri, 11 Aug 2023 16:03:54 +0200 Subject: [PATCH] Add more docs and workflows --- .github/workflows/go.yml | 5 +++-- .github/workflows/release.yml | 38 +++++++++++++++++++++++++++++++++++ .gitignore | 3 ++- Makefile | 4 ++++ README.md | 13 ++++++++++++ cli/main.go | 14 ++++++++++--- subsetter/db_test.go | 3 +-- 7 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 5934261..12983e5 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -10,9 +10,10 @@ on: branches: [ "main" ] jobs: + vuln: + uses: cristalhq/.github/.github/workflows/vuln.yml@v0.4.0 build: - runs-on: ubuntu-latest services: postgres: @@ -37,7 +38,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v4 with: - go-version: '1.20' + go-version: stable - name: Build run: go build -v ./... diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..27aad41 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,38 @@ +name: release + +permissions: + contents: write + +on: + workflow_call: + inputs: + tag: + description: 'Tag to create' + required: true + default: 'v0.0.0' + type: string + +jobs: + run: + name: Release + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Checkout with tags + run: git fetch --prune --unshallow --tags + + - name: Create release + run: | + git log --format="%C(auto) %H %s" `git tag --sort=-committerdate | head -1`...HEAD > changelog.txt + echo ${{ secrets.GITHUB_TOKEN }} | gh auth login --with-token + gh release create ${{ github.event.inputs.tag }} -t ${{ github.event.inputs.tag }} -F changelog.txt + + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v4.4.0 + with: + version: latest + args: release --rm-dist + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 5555cbc..18c5716 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.pgsql/data \ No newline at end of file +.pgsql/data +bin \ No newline at end of file diff --git a/Makefile b/Makefile index 7c68b83..f523b41 100644 --- a/Makefile +++ b/Makefile @@ -15,3 +15,7 @@ is-postgres-running: .PHONY: pgweb pgweb:is-postgres-running @pgweb --url "postgres://test_source@localhost:5432/test_source?sslmode=disable" + + +build: + CGO_ENABLEd=0 go build -o bin/subsetter ${PACKAGE}/cli diff --git a/README.md b/README.md index 5f17498..2e537bc 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,19 @@ Utilizing the native PostgreSQL COPY command, pg-subsetter performs data transfe ## Usage +``` +Usage of subsetter: + -dst string + Destination DSN + -f float + Fraction of rows to copy (default 0.05) + -src string + Source DSN +``` + + +Example: + ```pg-subsetter -src postgresql://:@/bigdb -dst postgresql://:@/littledb -f 0.05``` # Installing diff --git a/cli/main.go b/cli/main.go index 55a25a6..dd07c59 100644 --- a/cli/main.go +++ b/cli/main.go @@ -6,12 +6,20 @@ import ( "github.com/rs/zerolog/log" ) -var src = flag.String("src", "", "Source DSN") -var dst = flag.String("dst", "", "Destination DSN") -var fraction = flag.Float64("f", 1.0, "Fraction of rows to copy") +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") func main() { flag.Parse() log.Info().Msg("Starting") + if *src == "" || *dst == "" { + log.Fatal().Msg("Source and destination DSNs are required") + } + + if *fraction <= 0 || *fraction > 1 { + log.Fatal().Msg("Fraction must be between 0 and 1") + } + } diff --git a/subsetter/db_test.go b/subsetter/db_test.go index 543599a..cc17233 100644 --- a/subsetter/db_test.go +++ b/subsetter/db_test.go @@ -41,8 +41,7 @@ func populateTestsWithData(conn *pgx.Conn, table string, size int) { for i := 0; i < size; i++ { query := fmt.Sprintf("INSERT INTO %s (text) VALUES ('test%d') RETURNING id", table, i) var row string - err := conn.QueryRow(context.Background(), query).Scan(&row) - fmt.Println(err) + conn.QueryRow(context.Background(), query).Scan(&row) query = fmt.Sprintf("INSERT INTO relation (simple_id) VALUES ('%v')", row) conn.Exec(context.Background(), query)