Proper interface for the database

This commit is contained in:
Imbus 2024-02-29 20:33:20 +01:00
parent 4d510e5c51
commit b39ce645d6
2 changed files with 11 additions and 2 deletions

View file

@ -10,6 +10,15 @@ import (
_ "github.com/mattn/go-sqlite3"
)
// Interface for the database
type Database interface {
AddUser(username string, password string) error
RemoveUser(username string) error
GetUserId(username string) (int, error)
AddProject(name string, description string, username string) error
Migrate(dirname string) error
}
// This struct is a wrapper type that holds the database connection
// Internally DB holds a connection pool, so it's safe for concurrent use
type Db struct {
@ -23,7 +32,7 @@ const userInsert = "INSERT INTO users (username, password) VALUES (?, ?)"
const projectInsert = "INSERT INTO projects (name, description, user_id) SELECT ?, ?, id FROM users WHERE username = ?"
// DbConnect connects to the database
func DbConnect(dbpath string) *Db {
func DbConnect(dbpath string) Database {
// Open the database
db, err := sqlx.Connect("sqlite3", dbpath)
if err != nil {

View file

@ -6,7 +6,7 @@ import (
// Tests are not guaranteed to be sequential
func setupState() (*Db, error) {
func setupState() (Database, error) {
db := DbConnect(":memory:")
err := db.Migrate("../../migrations")
if err != nil {