summaryrefslogtreecommitdiff
path: root/internal/database
diff options
context:
space:
mode:
Diffstat (limited to 'internal/database')
-rw-r--r--internal/database/migrations/001_init.down.sql2
-rw-r--r--internal/database/migrations/001_init.up.sql6
-rw-r--r--internal/database/migrations/002_defaults.down.sql5
-rw-r--r--internal/database/migrations/002_defaults.up.sql11
-rw-r--r--internal/database/repos/defaults.go25
-rw-r--r--internal/database/repos/main.go1
6 files changed, 46 insertions, 4 deletions
diff --git a/internal/database/migrations/001_init.down.sql b/internal/database/migrations/001_init.down.sql
index 1b8680f..41cc5c9 100644
--- a/internal/database/migrations/001_init.down.sql
+++ b/internal/database/migrations/001_init.down.sql
@@ -1,4 +1,4 @@
-BEGIN;
+BEGIN TRANSACTION;
DROP SCHEMA public CASCADE;
CREATE SCHEMA public;
COMMIT;
diff --git a/internal/database/migrations/001_init.up.sql b/internal/database/migrations/001_init.up.sql
index f7e41c1..141a045 100644
--- a/internal/database/migrations/001_init.up.sql
+++ b/internal/database/migrations/001_init.up.sql
@@ -1,5 +1,5 @@
-BEGIN;
-CREATE TABLE user_score (
+BEGIN TRANSACTION;
+CREATE TABLE IF NOT EXISTS user_score (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
@@ -8,7 +8,7 @@ CREATE TABLE user_score (
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-CREATE TABLE action (
+CREATE TABLE IF NOT EXISTS action(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
magnitude SMALLINT NOT NULL DEFAULT 1,
diff --git a/internal/database/migrations/002_defaults.down.sql b/internal/database/migrations/002_defaults.down.sql
new file mode 100644
index 0000000..3e38fa2
--- /dev/null
+++ b/internal/database/migrations/002_defaults.down.sql
@@ -0,0 +1,5 @@
+BEGIN TRANSACTION;
+
+DROP TABLE defaults;
+
+COMMIT;
diff --git a/internal/database/migrations/002_defaults.up.sql b/internal/database/migrations/002_defaults.up.sql
new file mode 100644
index 0000000..4bf42b7
--- /dev/null
+++ b/internal/database/migrations/002_defaults.up.sql
@@ -0,0 +1,11 @@
+BEGIN TRANSACTION;
+
+CREATE TABLE IF NOT EXISTS defaults (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ key TEXT UNIQUE NOT NULL,
+ value TEXT NOT NULL,
+ description TEXT NOT NULL DEFAULT '',
+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
+);
+
+COMMIT;
diff --git a/internal/database/repos/defaults.go b/internal/database/repos/defaults.go
new file mode 100644
index 0000000..e0b5e52
--- /dev/null
+++ b/internal/database/repos/defaults.go
@@ -0,0 +1,25 @@
+package repos
+
+type DefaultsRepo interface {
+ DBGetDefaultsMap() (map[string]string, error)
+}
+
+func (p *Provider) DBGetDefaultsMap() (map[string]string, error) {
+ rows, err := p.db.Queryx(`SELECT key, value
+ FROM defaults;
+ `)
+ if err != nil {
+ return nil, err
+ }
+ res := make(map[string]string)
+ for rows.Next() {
+ keyval, err := rows.SliceScan()
+ if err != nil {
+ return nil, err
+ }
+ key := keyval[0].(string)
+ value := keyval[1].(string)
+ res[key] = value
+ }
+ return res, nil
+}
diff --git a/internal/database/repos/main.go b/internal/database/repos/main.go
index 25eeec4..72860dd 100644
--- a/internal/database/repos/main.go
+++ b/internal/database/repos/main.go
@@ -7,6 +7,7 @@ import (
type FullRepo interface {
ActionRepo
UserScoreRepo
+ DefaultsRepo
}
type Provider struct {