Skip to content

Commit

Permalink
Fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
dz0ny committed Aug 11, 2023
1 parent 7b620ba commit 438218e
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 34 deletions.
38 changes: 21 additions & 17 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ name: Go

on:
push:
branches: [ "main" ]
branches: ["main"]
pull_request:
branches: [ "main" ]
branches: ["main"]

jobs:
vuln:
Expand All @@ -33,21 +33,25 @@ jobs:
# Maps tcp port 5432 on service container to the host
- 5432:5432
steps:
- uses: actions/checkout@v3
- run: .pgsql/testdb.sh
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: stable
- uses: actions/checkout@v3
- run: .pgsql/testdb.sh
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: stable
- name: Install GoReleaser
uses: goreleaser/goreleaser-action@v4
with:
install-only: true

- name: Build
run: make build
- name: Build
run: make build

- name: Test
run: go test -v ./...
- name: Test
run: go test -v ./...

- name: Upload assets
uses: actions/upload-artifact@v3
with:
name: pg-subsetter
path: dist/*
- name: Upload assets
uses: actions/upload-artifact@v3
with:
name: pg-subsetter
path: dist/*
34 changes: 34 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: lint

permissions: {} # no need any permissions

on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_call:

jobs:
run:
name: Lint
runs-on: ubuntu-latest
timeout-minutes: 5
strategy:
fail-fast: true

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Install Go
uses: actions/setup-go@v4
with:
go-version: 'stable'
check-latest: true

- name: Lint
uses: golangci/[email protected]
with:
version: latest
args: --timeout 5m
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ pgweb:is-postgres-running
build:
rm -rf dist
goreleaser build --snapshot --clean

lint:
golangci-lint run
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
packages = with pkgs; [
go
goreleaser
golangci-lint
postgresql
process-compose
shellcheck
Expand Down
22 changes: 17 additions & 5 deletions subsetter/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func getTestConnection() *pgx.Conn {
}

func populateTests(conn *pgx.Conn) {
conn.Exec(context.Background(), `
_, err := conn.Exec(context.Background(), `
CREATE TABLE simple (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
text TEXT
Expand All @@ -35,24 +35,36 @@ func populateTests(conn *pgx.Conn) {
ALTER TABLE relation ADD CONSTRAINT relation_simple_fk FOREIGN KEY (simple_id) REFERENCES simple(id);
`)

if err != nil {
panic(err)
}
}

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
conn.QueryRow(context.Background(), query).Scan(&row)
err := conn.QueryRow(context.Background(), query).Scan(&row)
if err != nil {
panic(err)
}
query = fmt.Sprintf("INSERT INTO relation (simple_id) VALUES ('%v')", row)

conn.Exec(context.Background(), query)

_, err = conn.Exec(context.Background(), query)
if err != nil {
panic(err)
}
}
}

func clearPopulateTests(conn *pgx.Conn) {
conn.Exec(context.Background(), `
_, err := conn.Exec(context.Background(), `
ALTER TABLE relation DROP CONSTRAINT relation_simple_fk;
DROP TABLE simple;
DROP TABLE relation;
`)
if err != nil {
panic(err)
}
}
25 changes: 16 additions & 9 deletions subsetter/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ func GetTables(conn *pgx.Conn) (tables []string, err error) {
rows, err := conn.Query(context.Background(), q)
for rows.Next() {
var name string
rows.Scan(&name)
tables = append(tables, name)
if err := rows.Scan(&name); err == nil {
tables = append(tables, name)
}
}
rows.Close()
return
Expand All @@ -30,13 +31,15 @@ func GetTablesWithRows(conn *pgx.Conn) (tables []Table, err error) {
rows, err := conn.Query(context.Background(), q)
for rows.Next() {
var table Table
rows.Scan(&table.Name, &table.Rows)

// fix for tables with no rows
if table.Rows == -1 {
table.Rows = 0
if err := rows.Scan(&table.Name, &table.Rows); err == nil {
// fix for tables with no rows
if table.Rows == -1 {
table.Rows = 0
}
tables = append(tables, table)
}
tables = append(tables, table)

}
rows.Close()

Expand All @@ -46,7 +49,9 @@ func GetTablesWithRows(conn *pgx.Conn) (tables []Table, err error) {
func CopyTableToString(table string, limit int, conn *pgx.Conn) (result string, err error) {
q := fmt.Sprintf(`copy (SELECT * FROM %s order by random() limit %d) to stdout`, table, limit)
var buff bytes.Buffer
conn.PgConn().CopyTo(context.Background(), &buff, q)
if _, err = conn.PgConn().CopyFrom(context.Background(), &buff, q); err != nil {
return
}
result = buff.String()
return
}
Expand All @@ -55,7 +60,9 @@ func CopyStringToTable(table string, data string, conn *pgx.Conn) (err error) {
q := fmt.Sprintf(`copy %s from stdout`, table)
var buff bytes.Buffer
buff.WriteString(data)
conn.PgConn().CopyFrom(context.Background(), &buff, q)
if _, err = conn.PgConn().CopyFrom(context.Background(), &buff, q); err != nil {
return
}

return
}
6 changes: 3 additions & 3 deletions subsetter/relations.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func GetRelations(table string, conn *pgx.Conn) (relations []string, err error)
rows, err := conn.Query(context.Background(), q, table)
for rows.Next() {
var table string
rows.Scan(&table)

relations = append(relations, table)
if err := rows.Scan(&table); err == nil {
relations = append(relations, table)
}
}
rows.Close()
return
Expand Down

0 comments on commit 438218e

Please sign in to comment.