diff options
Diffstat (limited to '')
-rw-r--r-- | golang/pkg/database/migrations.go | 78 | ||||
-rw-r--r-- | golang/pkg/database/sql/000_init.sql | 7 | ||||
-rw-r--r-- | golang/pkg/database/tokens.go | 14 |
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 +} |