Skip to content
This repository was archived by the owner on Apr 21, 2024. It is now read-only.

Commit 8ba2e88

Browse files
committed
Fix for #9
Add initialisation code for the database tables
1 parent e7492f8 commit 8ba2e88

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

internal/database/init.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package database
2+
3+
import (
4+
"database/sql"
5+
"log"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
)
10+
11+
func Init(db *sql.DB) {
12+
filenames := []string{"student", "faculty", "guild", "group", "course"}
13+
script := []string{}
14+
for _, filename := range filenames {
15+
// Read PostgreSQL script
16+
path := filepath.Join("sql", "schemas", filename+".sql")
17+
file, ioErr := os.ReadFile(path)
18+
if ioErr != nil {
19+
log.Fatalln(ioErr)
20+
}
21+
22+
// Execute PostgreSQL script
23+
script = append(script, string(file))
24+
}
25+
26+
_, err := db.Exec(strings.Join(script, "\n"))
27+
if err != nil {
28+
log.Fatalln(err)
29+
}
30+
}

server.go

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88

99
h "nkssbackend/handlers"
10+
"nkssbackend/internal/database"
1011
m "nkssbackend/middleware"
1112

1213
"github.com/gorilla/mux"
@@ -24,6 +25,8 @@ func NewServer() *server {
2425
if err != nil {
2526
log.Fatalln(err)
2627
}
28+
// !TODO: Make separate interface to initialise database
29+
database.Init(db)
2730

2831
s := server{db: db, router: mux.NewRouter().StrictSlash(true)}
2932
s.setRouters()

0 commit comments

Comments
 (0)