summaryrefslogtreecommitdiff
path: root/golang/pkg/database
diff options
context:
space:
mode:
authorJulien Dessaux2024-05-06 00:18:22 +0200
committerJulien Dessaux2024-05-06 00:18:22 +0200
commit427cc77fa3633aca7c4c3377fa26b0e70921a7b5 (patch)
treecc8de99037ab539a0786f568cf217b680ac99ec3 /golang/pkg/database
parent[node] fix shortest path to only hop through refueling points (diff)
downloadspacetraders-427cc77fa3633aca7c4c3377fa26b0e70921a7b5.tar.gz
spacetraders-427cc77fa3633aca7c4c3377fa26b0e70921a7b5.tar.bz2
spacetraders-427cc77fa3633aca7c4c3377fa26b0e70921a7b5.zip
[golang] bootstrapped a client in yet another language
Diffstat (limited to '')
-rw-r--r--golang/pkg/database/migrations.go78
-rw-r--r--golang/pkg/database/sql/000_init.sql7
-rw-r--r--golang/pkg/database/tokens.go14
3 files changed, 99 insertions, 0 deletions
diff --git a/golang/pkg/database/migrations.go b/golang/pkg/database/migrations.go
new file mode 100644
index 0000000..94207a5
--- /dev/null
+++ b/golang/pkg/database/migrations.go
@@ -0,0 +1,78 @@
+package database
+
+import (
+ "context"
+ "database/sql"
+ "embed"
+ "io/fs"
+
+ _ "github.com/mattn/go-sqlite3"
+)
+
+type DB struct {
+ ctx context.Context
+ db *sql.DB
+}
+
+//go:embed sql/*.sql
+var schemaFiles embed.FS
+
+func DBInit(ctx context.Context, url string) (myDB *DB, err error) {
+ var db *sql.DB
+ if db, err = sql.Open("sqlite3", url); err != nil {
+ return nil, err
+ }
+ defer func() {
+ if err != nil {
+ _ = db.Close()
+ }
+ }()
+
+ if _, err = db.ExecContext(ctx, "PRAGMA foreign_keys = ON"); err != nil {
+ return nil, err
+ }
+ if _, err = db.ExecContext(ctx, "PRAGMA journal_mode = WAL"); err != nil {
+ return nil, err
+ }
+
+ var version int
+ if err = db.QueryRowContext(ctx, `SELECT version FROM schema_version;`).Scan(&version); err != nil {
+ if err.Error() == "no such table: schema_version" {
+ version = 0
+ } else {
+ return nil, err
+ }
+ }
+
+ statements := make([]string, 0)
+ err = fs.WalkDir(schemaFiles, ".", func(path string, d fs.DirEntry, err error) error {
+ if d.IsDir() || err != nil {
+ return err
+ }
+ var stmts []byte
+ if stmts, err = schemaFiles.ReadFile(path); err != nil {
+ return err
+ } else {
+ statements = append(statements, string(stmts))
+ }
+ return nil
+ })
+ if err != nil {
+ return nil, err
+ }
+
+ for version < len(statements) {
+ if _, err = db.ExecContext(ctx, statements[version]); err != nil {
+ return nil, err
+ }
+ version++
+ }
+ if _, err = db.ExecContext(ctx, `DELETE FROM schema_version; INSERT INTO schema_version (version) VALUES (?);`, version); err != nil {
+ return nil, err
+ }
+ return &DB{ctx: ctx, db: db}, nil
+}
+
+func (db *DB) Close() error {
+ return db.db.Close()
+}
diff --git a/golang/pkg/database/sql/000_init.sql b/golang/pkg/database/sql/000_init.sql
new file mode 100644
index 0000000..c06d2d3
--- /dev/null
+++ b/golang/pkg/database/sql/000_init.sql
@@ -0,0 +1,7 @@
+CREATE TABLE schema_version (
+ version INTEGER NOT NULL
+);
+CREATE TABLE tokens (
+ id INTEGER PRIMARY KEY,
+ data TEXT NOT NULL
+);
diff --git a/golang/pkg/database/tokens.go b/golang/pkg/database/tokens.go
new file mode 100644
index 0000000..16bda2b
--- /dev/null
+++ b/golang/pkg/database/tokens.go
@@ -0,0 +1,14 @@
+package database
+
+func (db DB) AddToken(token string) error {
+ _, err := db.db.ExecContext(db.ctx, `INSERT INTO tokens(data) VALUES (?);`, token)
+ return err
+}
+
+func (db DB) GetToken() (string, error) {
+ var token string
+ if err := db.db.QueryRowContext(db.ctx, `SELECT data FROM tokens;`).Scan(&token); err != nil {
+ return "", err
+ }
+ return token, nil
+}