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

Adiciona possibilidade de uso com MongoDB #278

Open
wants to merge 63 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 61 commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
f4e7b00
first commit mongodb
mbnunes Jan 22, 2025
7efa454
insert in mongo working
mbnunes Jan 24, 2025
c4ea1a4
fix interface
mbnunes Jan 24, 2025
6709df5
testes mongodb
mbnunes Jan 25, 2025
a3f4a27
consulta updated e o cnpj ja funcionando no mongo.
mbnunes Jan 27, 2025
157b65a
restaurando o github url original
mbnunes Jan 27, 2025
200e754
fixes
mbnunes Jan 27, 2025
d1172dd
defer and recover debugs
mbnunes Jan 28, 2025
58c87e1
falta de conhecimento talvez?
mbnunes Jan 28, 2025
41f99ed
removido os debugs
mbnunes Jan 29, 2025
459350b
fixes
mbnunes Jan 29, 2025
106abd4
ajustes solicitados
mbnunes Jan 29, 2025
befacea
fix
mbnunes Jan 29, 2025
2619c3a
aplicando mesmo padrao
mbnunes Jan 29, 2025
d32d63b
mais fixes
mbnunes Jan 29, 2025
1f67f18
alguns fixes
mbnunes Jan 31, 2025
f999598
fix PR
mbnunes Jan 31, 2025
4f8fcb1
traducao
mbnunes Feb 1, 2025
ea16a80
fixes
mbnunes Feb 3, 2025
d2f00d0
exportei company, partenerData e cnae
mbnunes Feb 3, 2025
7cd37a7
mudança no createIndex para o PostLoad e algumas traducoes
mbnunes Feb 3, 2025
77d8408
retirado linhas vazias e nao ter necessidade da uri novamente
mbnunes Feb 3, 2025
1d79030
refix
mbnunes Feb 3, 2025
7e8116d
translate and remove lines
mbnunes Feb 3, 2025
aaafcf8
Update .env.sample
mbnunes Feb 3, 2025
2706002
new fixes
mbnunes Feb 3, 2025
07c61f8
CreateCompanies golang mod
mbnunes Feb 4, 2025
3c5327c
tentativa de resolver conflitos
mbnunes Feb 6, 2025
1aa1925
tentando corrigir conflito
mbnunes Feb 6, 2025
2499062
variavel repetida
mbnunes Feb 6, 2025
d04ed02
fix again
mbnunes Feb 6, 2025
c6f19f6
removing snappy
mbnunes Feb 6, 2025
b0bc749
novo go.mod e go.sum para o conflict
mbnunes Feb 6, 2025
6b68289
Revert "novo go.mod e go.sum para o conflict"
mbnunes Feb 6, 2025
2ca0b5f
Update db/mongodb.go
mbnunes Feb 6, 2025
e29b690
Update db/mongodb.go
mbnunes Feb 6, 2025
7dcde69
Update db/mongodb.go
mbnunes Feb 6, 2025
5822940
Update db/mongodb.go
mbnunes Feb 6, 2025
7120eca
Update db/mongodb.go
mbnunes Feb 6, 2025
ecdfe97
Update db/mongodb.go
mbnunes Feb 6, 2025
5a7fa61
Update db/mongodb.go
mbnunes Feb 6, 2025
1ca1b60
Update db/mongodb.go
mbnunes Feb 6, 2025
3c2888b
Update db/mongodb.go
mbnunes Feb 6, 2025
5862d19
Update db/mongodb.go
mbnunes Feb 6, 2025
f81cea5
Update db/mongodb.go
mbnunes Feb 6, 2025
65e7aca
Update db/mongodb.go
mbnunes Feb 6, 2025
36ddffe
Update db/mongodb.go
mbnunes Feb 6, 2025
84dc685
Update db/mongodb.go
mbnunes Feb 6, 2025
5a43234
Update db/mongodb.go
mbnunes Feb 6, 2025
b00efca
Update db/mongodb.go
mbnunes Feb 6, 2025
100ce65
Update db/mongodb.go
mbnunes Feb 6, 2025
1a593b1
Update db/mongodb.go
mbnunes Feb 6, 2025
c9695b0
Update db/mongodb.go
mbnunes Feb 7, 2025
f95f715
first commit mongodb
mbnunes Feb 7, 2025
63abd6d
Merge remote-tracking branch 'origin/mongodb' into mongodb
mbnunes Feb 7, 2025
893efad
last fix
mbnunes Feb 7, 2025
71ac1ff
adicionado comentario sobre o []interface{}
mbnunes Feb 7, 2025
dd6f812
tentando resolver a falta do snappy
mbnunes Feb 7, 2025
c3d11de
sem snappy
mbnunes Feb 7, 2025
a3d7c3e
ok nao mexo mais nisso :D
mbnunes Feb 7, 2025
814b538
Merge branch 'main' into mongodb
cuducos Feb 7, 2025
329d11c
ci ajustes
mbnunes Feb 7, 2025
77f3f4b
Teste do CI
mbnunes Feb 9, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
DATABASE_URL=postgres://minhareceita:minhareceita@postgres:5432/minhareceita?sslmode=disable

# Tests
TEST_DATABASE_URL=postgres://minhareceita:minhareceita@postgres_test:5555/minhareceita?sslmode=disable
TEST_POSTGRES_URL=postgres://minhareceita:minhareceita@postgres_test:5555/minhareceita?sslmode=disable
TEST_MONGODB_URL=mongodb://minhareceita:minhareceita@mongo_test:27017/minhareceita?authSource=admin

# Mirror
ENDPOINT_URL=https://nyc3.digitaloceanspaces.com
Expand Down
25 changes: 19 additions & 6 deletions cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"strings"

"github.com/cuducos/minha-receita/api"
"github.com/cuducos/minha-receita/db"
Expand Down Expand Up @@ -53,18 +54,30 @@ var apiCmd = &cobra.Command{
return err
}
}
pg, err := db.NewPostgreSQL(u, postgresSchema, nr)
if err != nil {
return err
}
defer pg.Close()
if port == "" {
port = os.Getenv("PORT")
}
if port == "" {
port = defaultPort
}
api.Serve(&pg, port, nr)
if strings.HasPrefix(u, "mongodb://") {
mdb, _ := db.NewMongoDB(u)
if err != nil {
return err
}
defer mdb.Close()
api.Serve(&mdb, port, nr)
} else if strings.HasPrefix(u, "postgres://") || strings.HasPrefix(u, "postgresql://") {
pg, err := db.NewPostgreSQL(u, postgresSchema, nr)
if err != nil {
return err
}
defer pg.Close()
api.Serve(&pg, port, nr)
} else {
return fmt.Errorf("unsupported database URI, must start with postgres:// or mongodb://")
}

return nil
},
}
Expand Down
57 changes: 44 additions & 13 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import (
"fmt"
"os"

"github.com/spf13/cobra"
"strings"

"github.com/cuducos/minha-receita/db"
"github.com/spf13/cobra"
)

const (
Expand Down Expand Up @@ -47,7 +47,7 @@
}
u := os.Getenv("DATABASE_URL")
if u == "" {
return "", fmt.Errorf("could not find a database URI, set the DATABASE_URL environment variable with the credentials for a PostgreSQL database")
return "", fmt.Errorf("could not find a database URI, set the DATABASE_URL environment variable with the credentials for a PostgreSQL or MongoDB database")
}
return u, nil
}
Expand All @@ -60,18 +60,34 @@

var createCmd = &cobra.Command{
Use: "create",
Short: "Creates the required tables in PostgreSQL",
Short: "Creates the required tables in the database",
RunE: func(_ *cobra.Command, _ []string) error {
u, err := loadDatabaseURI()
if err != nil {
return err
}
pg, err := db.NewPostgreSQL(u, postgresSchema, nil)
if err != nil {
uri := os.Getenv("DATABASE_URL")
if strings.HasPrefix(uri, "mongodb://") {
mdb, err := db.NewMongoDB(uri)
if err != nil {
return err
}
err = mdb.CreateCollection()
if err != nil {
return err
}
defer mdb.Close()
return err
} else if strings.HasPrefix(uri, "postgres://") || strings.HasPrefix(uri, "postgresql://") {
pg, err := db.NewPostgreSQL(u, postgresSchema, nil)
if err != nil {
return err
}
defer pg.Close()
return pg.CreateTable()
} else {
return fmt.Errorf("A URL não contém 'mongodb' nem 'postgres'")

Check failure on line 89 in cmd/cmd.go

View workflow job for this annotation

GitHub Actions / lint

error strings should not be capitalized (ST1005)
mbnunes marked this conversation as resolved.
Show resolved Hide resolved
}
defer pg.Close()
return pg.CreateTable()
},
}

Expand All @@ -83,12 +99,27 @@
if err != nil {
return err
}
pg, err := db.NewPostgreSQL(u, postgresSchema, nil)
if err != nil {
uri := os.Getenv("DATABASE_URL")
if strings.HasPrefix(uri, "mongodb://") {
mdb, err := db.NewMongoDB(uri)
if err != nil {
return err
}
err = mdb.DropCollection()
if err != nil {
return err
}
return err
} else if strings.HasPrefix(uri, "postgres://") || strings.HasPrefix(uri, "postgresql://") {
pg, err := db.NewPostgreSQL(u, postgresSchema, nil)
if err != nil {
return err
}
defer pg.Close()
return pg.DropTable()
} else {
return fmt.Errorf("A URL não contém 'mongodb' nem 'postgres'")

Check failure on line 121 in cmd/cmd.go

View workflow job for this annotation

GitHub Actions / lint

error strings should not be capitalized (ST1005)
Copy link
Owner

Choose a reason for hiding this comment

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

Essa implementação precisa mudar usando a interface, para evitar repetição.

mbnunes marked this conversation as resolved.
Show resolved Hide resolved
}
defer pg.Close()
return pg.DropTable()
},
}

Expand All @@ -98,7 +129,7 @@
}

func addDatabase(c *cobra.Command) *cobra.Command {
c.Flags().StringVarP(&databaseURI, "database-uri", "u", "", "PostgreSQL URI (default DATABASE_URL environment variable)")
c.Flags().StringVarP(&databaseURI, "database-uri", "u", "", "Database URI (default DATABASE_URL environment variable)")
c.Flags().StringVarP(&postgresSchema, "postgres-schema", "s", "public", "PostgreSQL schema")
return c
}
Expand Down
42 changes: 33 additions & 9 deletions cmd/transform.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cmd

import (
"fmt"
"strings"

"github.com/cuducos/minha-receita/db"
"github.com/cuducos/minha-receita/transform"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -35,21 +38,42 @@
if err != nil {
return err
}
pg, err := db.NewPostgreSQL(u, postgresSchema, nil)
if err != nil {
return err
}
defer pg.Close()

if cleanUp {
if err := pg.DropTable(); err != nil {
if strings.HasPrefix(u, "mongodb://") {
mdb, err := db.NewMongoDB(u)
if err != nil {
return err
}
if err := pg.CreateTable(); err != nil {
if cleanUp {
err = mdb.DropCollection()
if err != nil {
return err
}
err = mdb.CreateCollection()
if err != nil {
return err
}
}
return transform.Transform(dir, &mdb, maxParallelDBQueries, batchSize, !noPrivacy)
} else if strings.HasPrefix(u, "postgres://") || strings.HasPrefix(u, "postgresql://") {
pg, err := db.NewPostgreSQL(u, postgresSchema, nil)
if err != nil {
return err
}
defer pg.Close()
if cleanUp {
if err := pg.DropTable(); err != nil {
return err
}
if err := pg.CreateTable(); err != nil {
return err
}
}
return transform.Transform(dir, &pg, maxParallelDBQueries, batchSize, !noPrivacy)
} else {
return fmt.Errorf("A URL não contém 'mongodb' nem 'postgres'")

Check failure on line 74 in cmd/transform.go

View workflow job for this annotation

GitHub Actions / lint

error strings should not be capitalized (ST1005)
mbnunes marked this conversation as resolved.
Show resolved Hide resolved
}
return transform.Transform(dir, &pg, maxParallelDBQueries, batchSize, !noPrivacy)

},
}

Expand Down
Loading
Loading