From e7bbadc8f469d5cbfbc012932c83d1d440b6e8a3 Mon Sep 17 00:00:00 2001 From: xengineering Date: Wed, 27 Dec 2023 12:39:10 +0100 Subject: model: Add initial SQL migration This provides the basic table structure to the database. --- model/database.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'model/database.go') diff --git a/model/database.go b/model/database.go index ed9a985..8ce1897 100644 --- a/model/database.go +++ b/model/database.go @@ -2,6 +2,7 @@ package model import ( "database/sql" + "embed" "log" "path/filepath" @@ -10,6 +11,18 @@ import ( var db *sql.DB +//go:embed sql/*.sql +var sqlCode embed.FS + +func GetSql(code string) (string, error) { + query, err := sqlCode.ReadFile("sql/" + code + ".sql") + if err != nil { + return "", err + } + + return string(query), nil +} + func InitDatabase() { var err error @@ -24,6 +37,17 @@ func InitDatabase() { if err != nil { log.Fatal(err) } + + for _, v := range []string{`migrate`} { + query, err := GetSql(v) + if err != nil { + log.Fatal(err) + } + _, err = db.Exec(query) + if err != nil { + log.Fatal(err) + } + } } func CloseDatabase() { -- cgit v1.2.3-70-g09d2