feat: generate db init query into src
This commit is contained in:
parent
fa20e5ce76
commit
d000f23fba
|
|
@ -2,3 +2,4 @@
|
||||||
gin-bin
|
gin-bin
|
||||||
lishwist.db
|
lishwist.db
|
||||||
.env
|
.env
|
||||||
|
db/init_sql.go
|
||||||
|
|
|
||||||
9
db/db.go
9
db/db.go
|
|
@ -1,8 +1,9 @@
|
||||||
|
//go:generate go run gen_init_sql.go
|
||||||
|
|
||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"os"
|
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
|
@ -19,11 +20,7 @@ func Open() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Init() error {
|
func Init() error {
|
||||||
initStmt, err := os.ReadFile("./db/init.sql")
|
_, err := database.Exec(InitQuery)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = database.Exec(string(initStmt))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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))
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue