diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 8b26191..c9454f0 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -5,9 +5,9 @@ name: Go on: push: - branches: [ "main" ] + branches: ["main"] pull_request: - branches: [ "main" ] + branches: ["main"] jobs: vuln: @@ -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/* diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..edfe2f5 --- /dev/null +++ b/.github/workflows/lint.yml @@ -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/golangci-lint-action@v3.4.0 + with: + version: latest + args: --timeout 5m \ No newline at end of file diff --git a/Makefile b/Makefile index 6728bfb..8771f2c 100644 --- a/Makefile +++ b/Makefile @@ -19,3 +19,6 @@ pgweb:is-postgres-running build: rm -rf dist goreleaser build --snapshot --clean + +lint: + golangci-lint run \ No newline at end of file diff --git a/flake.nix b/flake.nix index e355e0d..29396f0 100644 --- a/flake.nix +++ b/flake.nix @@ -34,6 +34,7 @@ packages = with pkgs; [ go goreleaser + golangci-lint postgresql process-compose shellcheck diff --git a/subsetter/db_test.go b/subsetter/db_test.go index cc17233..5c6ecea 100644 --- a/subsetter/db_test.go +++ b/subsetter/db_test.go @@ -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 @@ -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) + } } diff --git a/subsetter/query.go b/subsetter/query.go index 325cecc..8054b52 100644 --- a/subsetter/query.go +++ b/subsetter/query.go @@ -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 @@ -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() @@ -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 } @@ -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 } diff --git a/subsetter/relations.go b/subsetter/relations.go index f46de46..3cb84d4 100644 --- a/subsetter/relations.go +++ b/subsetter/relations.go @@ -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