feat: generate db init query into src

This commit is contained in:
Teajey 2024-05-20 22:40:05 +12:00
parent fa20e5ce76
commit d000f23fba
Signed by: Teajey
GPG Key ID: 970E790FE834A713
3 changed files with 34 additions and 6 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
gin-bin
lishwist.db
.env
db/init_sql.go

View File

@ -1,8 +1,9 @@
//go:generate go run gen_init_sql.go
package db
import (
"database/sql"
"os"
_ "github.com/mattn/go-sqlite3"
)
@ -19,11 +20,7 @@ func Open() error {
}
func Init() error {
initStmt, err := os.ReadFile("./db/init.sql")
if err != nil {
return err
}
_, err = database.Exec(string(initStmt))
_, err := database.Exec(InitQuery)
if err != nil {
return err
}

30
db/gen_init_sql.go Normal file
View File

@ -0,0 +1,30 @@
//go:build ignore
package main
import (
"log"
"os"
"text/template"
)
var initTemplate = template.Must(template.New("").Parse("// Code generated DO NOT EDIT.\n" +
"package db\n" +
"\n" +
"const InitQuery = `{{.}}`\n",
))
func main() {
initStmt, err := os.ReadFile("./init.sql")
if err != nil {
log.Fatal(err)
}
f, err := os.Create("./init_sql.go")
if err != nil {
log.Fatal(err)
}
defer f.Close()
initTemplate.Execute(f, string(initStmt))
}