diff options
author | xengineering <me@xengineering.eu> | 2023-12-27 12:39:10 +0100 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-12-27 12:42:25 +0100 |
commit | e7bbadc8f469d5cbfbc012932c83d1d440b6e8a3 (patch) | |
tree | 22bf2b46ac8fa770e107e6eb04410cae73357cbf | |
parent | c452ca52b9c18303045db838c6cead62c1454172 (diff) | |
download | ceres-e7bbadc8f469d5cbfbc012932c83d1d440b6e8a3.tar ceres-e7bbadc8f469d5cbfbc012932c83d1d440b6e8a3.tar.zst ceres-e7bbadc8f469d5cbfbc012932c83d1d440b6e8a3.zip |
model: Add initial SQL migration
This provides the basic table structure to the database.
-rw-r--r-- | model/database.go | 24 | ||||
-rw-r--r-- | model/sql/migrate.sql | 14 |
2 files changed, 38 insertions, 0 deletions
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() { diff --git a/model/sql/migrate.sql b/model/sql/migrate.sql new file mode 100644 index 0000000..6ca4d0f --- /dev/null +++ b/model/sql/migrate.sql @@ -0,0 +1,14 @@ +CREATE TABLE 'recipes' ( + 'id' integer PRIMARY KEY AUTOINCREMENT, + 'title' text DEFAULT '', + 'portions' text DEFAULT '', -- FIXME has to be integer + 'url' text DEFAULT '', + 'notes' text DEFAULT '' +); + +CREATE TABLE 'recipe_steps' ( + 'id' integer PRIMARY KEY, + 'recipe_id' integer DEFAULT NULL, + 'rank' integer DEFAULT NULL, + 'text' text DEFAULT '' +); |