Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibilidade de criar os índices extras via CLI #292

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
16 changes: 15 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ var dropCmd = &cobra.Command{
},
}

var extraIndexesCmd = &cobra.Command{
Use: "extra-indexes",
Short: "Creates indexes in database", //Acho que essa explicacao pode melhorar
RunE: func(_ *cobra.Command, args []string) error {
db, err := loadDatabase()
if err != nil {
return fmt.Errorf("could not find database: %w", err)
}
defer db.Close()
return db.ExtraIndexes(args)
},
}

func addDataDir(c *cobra.Command) *cobra.Command {
c.Flags().StringVarP(&dir, "directory", "d", defaultDataDir, "directory of the downloaded files")
return c
Expand All @@ -80,7 +93,7 @@ func addDatabase(c *cobra.Command) *cobra.Command {

// CLI returns the root command from Cobra CLI tool.
func CLI() *cobra.Command {
for _, c := range []*cobra.Command{createCmd, dropCmd} {
for _, c := range []*cobra.Command{createCmd, dropCmd, extraIndexesCmd} {
addDatabase(c)
}
for _, c := range []*cobra.Command{
Expand All @@ -92,6 +105,7 @@ func CLI() *cobra.Command {
checkCLI(),
createCmd,
dropCmd,
extraIndexesCmd,
transformCLI(),
sampleCLI(),
mirrorCLI(),
Expand Down
1 change: 1 addition & 0 deletions cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type database interface {
CreateCompanies([][]string) error
PostLoad() error
MetaSave(string, string) error
ExtraIndexes(idxs []string) error
// api
GetCompany(string) (string, error)
MetaRead(string) (string, error)
Expand Down
18 changes: 18 additions & 0 deletions db/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,21 @@ func (m *MongoDB) GetCompany(cnpj string) (string, error) {
}
return string(b), nil
}

func (m *MongoDB) ExtraIndexes(idxs []string) error {
log.Output(1, "Creating the indexes...")
c := m.db.Collection(companyTableName)
var i []mongo.IndexModel
for _, field := range idxs {
i = append(i, mongo.IndexModel{
Keys: bson.D{{Key: field, Value: 1}},
})
}
_, err := c.Indexes().CreateMany(m.ctx, i)
if err != nil {
return fmt.Errorf("error creating indexes: %w", err)
}
log.Output(1, fmt.Sprintf("Indexes successfully created in the collection %s", companyTableName))
return nil

}
33 changes: 33 additions & 0 deletions db/postgres.go
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isso aqui não é uma boa prática: concatenar strings para formar um SQL e escrever SQL dentro do Go. Melhor usar templates — por segurança, por facilidade de leitura e manutenção, e para manter o padrão de todos os outros comandos SQL que o módulo Postgres implementa.

Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,36 @@ func NewPostgreSQL(uri, schema string) (PostgreSQL, error) {
}
return p, nil
}

func (p *PostgreSQL) ExtraIndexes(idxs []string) error {
for _, v := range idxs {
t := "((json->'" + v + "'))"
iname := "json_"
qsa := []string{"pais", "nome_socio", "codigo_pais", "faixa_etaria", "cnpj_cpf_do_socio", "qualificacao_socio", "codigo_faixa_etaria", "data_entrada_sociedade", "identificador_de_socio", "cpf_representante_legal", "nome_representante_legal", "codigo_qualificacao_socio", "qualificacao_representante_legal", "codigo_qualificacao_representante_legal"}
for _, v_qsa := range qsa {
if v == v_qsa {
t = "(jsonb_extract_path(json, 'qsa', '" + v_qsa + "') jsonb_ops)"
iname += "qsa_"
break
}
}
cnae := []string{"codigo", "descricao"}
for _, v_cnae := range cnae {
if v == v_cnae {
t = "(jsonb_extract_path(json, 'cnae', '" + v_cnae + "') jsonb_ops)"
iname += "cnae_"
break
}
}
q := fmt.Sprintf(
"CREATE INDEX IF NOT EXISTS idx_%s%s ON %s USING GIN %s;",
iname, v, p.CompanyTableName, t,
)
if _, err := p.pool.Exec(context.Background(), q); err != nil {
return fmt.Errorf("error to create indexe %s: %w", v, err)
}
log.Output(1, "Indexe "+v+" created")
}

return nil
}