Skip to content

Commit

Permalink
fix database initialization error
Browse files Browse the repository at this point in the history
  • Loading branch information
tsirysndr committed Jun 5, 2022
1 parent 3f2e258 commit 263f387
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cmd/communes.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ to quickly create a Cobra application.`,
panic(err)
}

index, err := mada.InitializeBleve()
index, err := mada.InitializeBleve(db)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/districts.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ to quickly create a Cobra application.`,
panic(err)
}

index, err := mada.InitializeBleve()
index, err := mada.InitializeBleve(db)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/fokontany.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ to quickly create a Cobra application.`,
panic(err)
}

index, err := mada.InitializeBleve()
index, err := mada.InitializeBleve(db)
if err != nil {
panic(err)
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
mada.Init()
db, err := mada.OpenDatabaseConnection()

if err != nil {
panic(err)
}
mada.Init(db)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/regions.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ to quickly create a Cobra application.`,
panic(err)
}

index, err := mada.InitializeBleve()
index, err := mada.InitializeBleve(db)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ to quickly create a Cobra application.`,
panic(err)
}

index, err := mada.InitializeBleve()
index, err := mada.InitializeBleve(db)
if err != nil {
panic(err)
}
Expand Down
15 changes: 1 addition & 14 deletions mada/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,31 +62,18 @@ type Geometry struct {
//go:embed shp/*
var Assets embed.FS

func Init() (bleve.Index, error) {
func Init(db *sql.DB) (bleve.Index, error) {

index, err := CreateOrOpenBleve()

if err != nil {
log.Fatal(err)
}

var db *sql.DB

if os.Getenv("MADA_POSTGRES_URL") != "" {
db, err = OpenPostgresConnection()

if err != nil {
log.Fatal(err)
}
createPostgresTables(db)
addPostgresGeometryColumns(db)
} else {
db, err = OpenSQLiteConnection()

if err != nil {
log.Fatal(err)
}

initializeSpatialMetadata(db)
createTables(db)
addGeometryColumns(db)
Expand Down
4 changes: 2 additions & 2 deletions mada/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ func (s *SearchService) Search(term string, opt types.SearchOptions) (*types.Sea
return &types.SearchResult{Result: searchResults}, err
}

func InitializeBleve() (bleve.Index, error) {
func InitializeBleve(db *sql.DB) (bleve.Index, error) {
if _, err := os.Stat(DATABASE_PATH); os.IsNotExist(err) {
return Init()
return Init(db)
}
return bleve.Open(DATABASE_PATH)
}
Expand Down
2 changes: 1 addition & 1 deletion mada/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func StartHttpServer(db *sql.DB) {
MaxAge: 300, // Maximum value not ignored by any of major browsers
})

index, err := InitializeBleve()
index, err := InitializeBleve(db)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 263f387

Please sign in to comment.