@@ -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