aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/db/setup.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/db/setup.go')
-rw-r--r--pkg/db/setup.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/pkg/db/setup.go b/pkg/db/setup.go
new file mode 100644
index 0000000..e57b046
--- /dev/null
+++ b/pkg/db/setup.go
@@ -0,0 +1,47 @@
+package db
+
+import (
+ "database/sql"
+ "errors"
+ "log"
+ "os"
+
+ _ "github.com/mattn/go-sqlite3"
+)
+
+func SetupDB(path string) (*sql.DB, error) {
+ stmt_base := `DROP TABLE IF EXISTS datamap;
+ CREATE TABLE datamap(id INTEGER PRIMARY KEY, name TEXT, date_created TEXT);
+ DROP TABLE IF EXISTS datamap_line;
+
+ CREATE TABLE datamap_line(
+ id INTEGER PRIMARY KEY,
+ dm_id INTEGER,
+ key TEXT NOT NULL,
+ sheet TEXT NOT NULL,
+ cellref TEXT,
+ FOREIGN KEY (dm_id)
+ REFERENCES datamap(id)
+ ON DELETE CASCADE
+ );
+ `
+ os.Create(path)
+ db, err := sql.Open("sqlite3", path)
+ if err != nil {
+ return db, errors.New("Cannot open that damn database file")
+ }
+
+ // We probably don't need pragma here but we have it for later.
+ pragma := "PRAGMA foreign_keys = ON;"
+ _, err = db.Exec(pragma)
+ if err != nil {
+ log.Printf("%q: %s\n", err, pragma)
+ }
+
+ _, err = db.Exec(stmt_base)
+ if err != nil {
+ log.Printf("%q: %s\n", err, stmt_base)
+ }
+
+ return db, nil
+}