Skip to content

Commit c3e2e29

Browse files
committed
refactor: move database init to function, reduce gocyclo limit
1 parent 03cb7fe commit c3e2e29

File tree

2 files changed

+42
-27
lines changed

2 files changed

+42
-27
lines changed

.golangci.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
issues:
22
fix: true
33

4+
# Relax rules for tests
5+
exclude-rules:
6+
- path: _test\.go
7+
linters:
8+
- gocyclo
9+
- errcheck
10+
- dupl
11+
412
linters:
513
enable:
614
- gocyclo
@@ -16,6 +24,8 @@ linters-settings:
1624
gofumpt:
1725
lang-version: "1.18"
1826
extra-rules: true
27+
gocyclo:
28+
min-complexity: 15
1929

2030
godot:
2131
exclude:

main.go

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,40 +46,16 @@ func main() {
4646
}
4747
log.Logger = log.Output(output).With().Logger()
4848

49-
// Create data directory
50-
dataDir := filepath.Join(".", "data")
51-
err := os.MkdirAll(dataDir, os.ModePerm)
52-
if err != nil {
53-
log.Fatal().Msg(err.Error())
54-
}
55-
56-
// Connect to the database
57-
err = database.ConnectDatabase(sqlite.Open, "data/gorm.db?_pragma=foreign_keys(1)")
58-
if err != nil {
59-
log.Fatal().Msg(err.Error())
60-
}
61-
62-
// Drop unused constraint in https://github.com/envelope-zero/backend/pull/274
63-
// Can be removed after the 1.0.0 release (we will require everyone to upgrade to 1.0.0 and then to further releases).
64-
err = database.DB.Migrator().DropConstraint(&models.Allocation{}, "month_valid")
65-
if err != nil {
66-
log.Debug().Err(err).Msg("Could not drop month_valid constraint on allocations")
67-
}
68-
69-
// Migrate all models so that the schema is correct
70-
err = database.DB.AutoMigrate(models.Budget{}, models.Account{}, models.Category{}, models.Envelope{}, models.Transaction{}, models.Allocation{})
71-
if err != nil {
72-
log.Fatal().Msg(err.Error())
73-
}
49+
databaseInit()
7450

7551
r, err := router.Router()
7652
if err != nil {
7753
log.Fatal().Msg(err.Error())
7854
}
7955

8056
// Set the port to the env variable, default to 8080
81-
var port string
82-
if port = os.Getenv("PORT"); port == "" {
57+
port := os.Getenv("PORT")
58+
if port == "" {
8359
port = ":8080"
8460
}
8561

@@ -113,3 +89,32 @@ func main() {
11389
}
11490
log.Info().Str("event", "Backend stopped").Msg("Router")
11591
}
92+
93+
// databaseInit initializes the data directory and database.
94+
func databaseInit() {
95+
// Create data directory
96+
dataDir := filepath.Join(".", "data")
97+
err := os.MkdirAll(dataDir, os.ModePerm)
98+
if err != nil {
99+
log.Fatal().Msg(err.Error())
100+
}
101+
102+
// Connect to the database
103+
err = database.ConnectDatabase(sqlite.Open, "data/gorm.db?_pragma=foreign_keys(1)")
104+
if err != nil {
105+
log.Fatal().Msg(err.Error())
106+
}
107+
108+
// Drop unused constraint in https://github.com/envelope-zero/backend/pull/274
109+
// Can be removed after the 1.0.0 release (we will require everyone to upgrade to 1.0.0 and then to further releases).
110+
err = database.DB.Migrator().DropConstraint(&models.Allocation{}, "month_valid")
111+
if err != nil {
112+
log.Debug().Err(err).Msg("Could not drop month_valid constraint on allocations")
113+
}
114+
115+
// Migrate all models so that the schema is correct
116+
err = database.DB.AutoMigrate(models.Budget{}, models.Account{}, models.Category{}, models.Envelope{}, models.Transaction{}, models.Allocation{})
117+
if err != nil {
118+
log.Fatal().Msg(err.Error())
119+
}
120+
}

0 commit comments

Comments
 (0)