Skip to content

Commit

Permalink
Merge pull request #257 from cuducos/alphanumeric-cnpj
Browse files Browse the repository at this point in the history
Adiciona suporte a CNPJ alfanumérico
  • Loading branch information
cuducos authored Oct 28, 2024
2 parents 85f548d + 055a303 commit 07fd927
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 27 deletions.
20 changes: 9 additions & 11 deletions db/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"log"
"path/filepath"
"strconv"
"strings"
"text/template"

Expand Down Expand Up @@ -99,12 +98,16 @@ func (p *PostgreSQL) DropTable() error {
// CreateCompanies performs a copy to create a batch of companies in the
// database. It expects an array and each item should be another array with only
// two items: the ID and the JSON field values.
func (p *PostgreSQL) CreateCompanies(batch [][]any) error {
func (p *PostgreSQL) CreateCompanies(batch [][]string) error {
b := make([][]any, len(batch))
for i, r := range batch {
b[i] = []any{r[0], r[1]}
}
_, err := p.pool.CopyFrom(
context.Background(),
pgx.Identifier{p.CompanyTableName},
[]string{idFieldName, jsonFieldName},
pgx.CopyFromRows(batch),
pgx.CopyFromRows(b),
)
if err != nil {
return fmt.Errorf("error while importing data to postgres: %w", err)
Expand All @@ -114,25 +117,20 @@ func (p *PostgreSQL) CreateCompanies(batch [][]any) error {

// GetCompany returns the JSON of a company based on a CNPJ number.
func (p *PostgreSQL) GetCompany(id string) (string, error) {
n, err := strconv.ParseInt(id, 10, 0)
if err != nil {
return "", fmt.Errorf("error converting cnpj %s to integer: %w", id, err)
}

ctx := context.Background()
if p.newRelic != nil {
txn := p.newRelic.StartTransaction("GetCompany")
ctx = newrelic.NewContext(ctx, txn)
defer txn.End()
}

rows, err := p.pool.Query(ctx, p.sql["get"], n)
rows, err := p.pool.Query(ctx, p.sql["get"], id)
if err != nil {
return "", fmt.Errorf("error looking for cnpj %d: %w", n, err)
return "", fmt.Errorf("error looking for cnpj %s: %w", id, err)
}
j, err := pgx.CollectOneRow(rows, pgx.RowTo[string])
if err != nil {
return "", fmt.Errorf("error reading cnpj %d: %w", n, err)
return "", fmt.Errorf("error reading cnpj %s: %w", id, err)
}
return j, nil
}
Expand Down
4 changes: 2 additions & 2 deletions db/postgres/create.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
CREATE TABLE IF NOT EXISTS {{ .CompanyTableFullName }} (
tmp_pk SERIAL PRIMARY KEY,
{{ .IDFieldName }} bigint NOT NULL,
{{ .IDFieldName }} char(14) NOT NULL,
{{ .JSONFieldName }} jsonb NOT NULL
);
CREATE TABLE IF NOT EXISTS {{ .MetaTableFullName }} (
{{ .KeyFieldName }} char(16) NOT NULL PRIMARY KEY,
{{ .KeyFieldName }} char(16) NOT NULL PRIMARY KEY,
{{ .ValueFieldName }} text NOT NULL
)
6 changes: 3 additions & 3 deletions db/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func TestPostgresDB(t *testing.T) {
id := 33683111000280
id := "33683111000280"
json := `{"qsa": [{"name": 42}, {"name": "forty-two"}], "answer": 42}`

u := os.Getenv("TEST_DATABASE_URL")
Expand Down Expand Up @@ -35,10 +35,10 @@ func TestPostgresDB(t *testing.T) {
if err := pg.PreLoad(); err != nil {
t.Errorf("expected no error pre load, got %s", err)
}
if err := pg.CreateCompanies([][]any{{id, json}}); err != nil {
if err := pg.CreateCompanies([][]string{{id, json}}); err != nil {
t.Errorf("expected no error saving a company, got %s", err)
}
if err := pg.CreateCompanies([][]any{{id, json}}); err != nil {
if err := pg.CreateCompanies([][]string{{id, json}}); err != nil {
t.Errorf("expected no error saving a duplicated company, got %s", err)
}
if err := pg.PostLoad(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/avast/retry-go/v4 v4.6.0
github.com/aws/aws-sdk-go v1.55.5
github.com/cuducos/chunk v1.1.3
github.com/cuducos/go-cnpj v0.1.1
github.com/cuducos/go-cnpj v0.1.2
github.com/dgraph-io/badger/v4 v4.3.1
github.com/jackc/pgx/v5 v5.7.1
github.com/newrelic/go-agent/v3 v3.35.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/cuducos/chunk v1.1.3 h1:DoB2DF2Kj6xLPcDFe+VBBIMv3JfNDGMBbUKgrwmJglY=
github.com/cuducos/chunk v1.1.3/go.mod h1:MieiCPvKtGwJWcSoHb4HIzC8VtQ2LeH0xzt7d6rYQz0=
github.com/cuducos/go-cnpj v0.1.1 h1:ahG8SmCa7cBMv2vuR+5GU5Se0RW2RwRwIbfFHXH1/P4=
github.com/cuducos/go-cnpj v0.1.1/go.mod h1:Oe0M530eHl9eXExbNWriqTD9kMv618ScjlGtEey/f4Y=
github.com/cuducos/go-cnpj v0.1.2 h1:EKfO9AJPjxBh/Pc8S+SMSeXvQ7kLwhDoF6te4md58OA=
github.com/cuducos/go-cnpj v0.1.2/go.mod h1:Oe0M530eHl9eXExbNWriqTD9kMv618ScjlGtEey/f4Y=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
2 changes: 1 addition & 1 deletion transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (

type database interface {
PreLoad() error
CreateCompanies([][]any) error
CreateCompanies([][]string) error
PostLoad() error
MetaSave(string, string) error
}
Expand Down
9 changes: 2 additions & 7 deletions transform/venues.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package transform
import (
"fmt"
"io"
"strconv"
"sync"
"sync/atomic"

Expand All @@ -15,17 +14,13 @@ func saveBatch(db database, b []company) (int, error) {
if len(b) == 0 {
return 0, nil
}
s := make([][]any, len(b))
s := make([][]string, len(b))
for i, c := range b {
j, err := c.JSON()
if err != nil {
return 0, fmt.Errorf("error getting company %s as json: %w", cnpj.Mask(c.CNPJ), err)
}
n, err := strconv.Atoi(c.CNPJ)
if err != nil {
return 0, fmt.Errorf("could not convert cnpj %s to int: %w", c.CNPJ, err)
}
s[i] = []any{n, j}
s[i] = []string{c.CNPJ, j}
}
if err := db.CreateCompanies(s); err != nil {
return 0, fmt.Errorf("error saving companies: %w", err)
Expand Down

0 comments on commit 07fd927

Please sign in to comment.