Skip to content

Commit

Permalink
add db seeder
Browse files Browse the repository at this point in the history
  • Loading branch information
szarbartosz committed Nov 7, 2024
1 parent 444aeb1 commit 3ed9b00
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ FROM alpine:3

WORKDIR /
COPY --from=builder /binary /main
COPY --from=builder /app/initializers/seeds /seeds

EXPOSE 8080

Expand Down
79 changes: 79 additions & 0 deletions initializers/dbSeeder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package initializers

import (
"encoding/json"
"log"
"main/models"
"os"
)

func SeedDB() {
LoadStatusesFromJSON("./seeds/statuses.json")
LoadOfficesFromJSON("./seeds/offices.json")
}

func LoadStatusesFromJSON(filename string) {
var statuses []models.Status

data, err := os.ReadFile(filename)
if err != nil {
log.Panicf("Error reading JSON file: %s", err)
}

err = json.Unmarshal(data, &statuses)
if err != nil {
log.Panicf("Error unmarshalling JSON: %s", err)
}

for _, status := range statuses {
if result := DB.FirstOrCreate(&status, models.Status{ID: status.ID}); result.Error != nil {
log.Printf("Error inserting/updating status [%d] %s: %s", status.ID, status.Name, result.Error)
} else {
log.Printf("Successfully inserted/updated status [%d] %s", status.ID, status.Name)
}
}
}

func LoadAddressesFromJSON(filename string) {
var addresses []models.Address

data, err := os.ReadFile(filename)
if err != nil {
log.Panicf("Error reading JSON file: %s", err)
}

err = json.Unmarshal(data, &addresses)
if err != nil {
log.Panicf("Error unmarshalling JSON: %s", err)
}

for _, address := range addresses {
if result := DB.FirstOrCreate(&address, models.Address{ID: address.ID}); result.Error != nil {
log.Printf("Error inserting/updating address [%d] %s: %s", address.ID, address.City, result.Error)
} else {
log.Printf("Successfully inserted/updated address [%d] %s", address.ID, address.City)
}
}
}

func LoadOfficesFromJSON(filename string) {
var offices []models.Office

data, err := os.ReadFile(filename)
if err != nil {
log.Panicf("Error reading JSON file: %s", err)
}

err = json.Unmarshal(data, &offices)
if err != nil {
log.Panicf("Error unmarshalling JSON: %s", err)
}

for _, office := range offices {
if result := DB.FirstOrCreate(&office, models.Office{ID: office.ID}); result.Error != nil {
log.Printf("Error inserting/updating office [%d] %s: %s", office.ID, office.Name, result.Error)
} else {
log.Printf("Successfully inserted/updated office [%d] %s", office.ID, office.Name)
}
}
}
14 changes: 14 additions & 0 deletions initializers/seeds/offices.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"id": 1,
"name": "Wydział Kształtowania Środowiska",
"addressId": 1,
"address": {
"id": 1,
"city": "Kraków",
"street": "Piastowska",
"number": "10",
"zipCode": "31-085"
}
}
]
12 changes: 12 additions & 0 deletions initializers/seeds/statuses.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{ "id": 1, "name": "Nowa inwestycja" },
{ "id": 2, "name": "Inwentaryzacja terenowa" },
{ "id": 3, "name": "Inwentaryzacja kameralna" },
{ "id": 4, "name": "Złożenie wniosku" },
{ "id": 5, "name": "Uzupełnienie braków" },
{ "id": 6, "name": "Oględziny" },
{ "id": 7, "name": "Decyzja" },
{ "id": 8, "name": "Wycinka" },
{ "id": 8, "name": "Nasadzenia" },
{ "id": 8, "name": "Inwestycja zakończona" }
]
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func init() {
initializers.LoadEnvVariables()
initializers.ConnectToDB()
initializers.SyncDB()
initializers.SeedDB()
gin.SetMode(os.Getenv("GIN_MODE"))
}

Expand Down

0 comments on commit 3ed9b00

Please sign in to comment.