Proper interface for the database
This commit is contained in:
parent
4d510e5c51
commit
b39ce645d6
2 changed files with 11 additions and 2 deletions
|
@ -10,6 +10,15 @@ import (
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "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
|
// This struct is a wrapper type that holds the database connection
|
||||||
// Internally DB holds a connection pool, so it's safe for concurrent use
|
// Internally DB holds a connection pool, so it's safe for concurrent use
|
||||||
type Db struct {
|
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 = ?"
|
const projectInsert = "INSERT INTO projects (name, description, user_id) SELECT ?, ?, id FROM users WHERE username = ?"
|
||||||
|
|
||||||
// DbConnect connects to the database
|
// DbConnect connects to the database
|
||||||
func DbConnect(dbpath string) *Db {
|
func DbConnect(dbpath string) Database {
|
||||||
// Open the database
|
// Open the database
|
||||||
db, err := sqlx.Connect("sqlite3", dbpath)
|
db, err := sqlx.Connect("sqlite3", dbpath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
|
|
||||||
// Tests are not guaranteed to be sequential
|
// Tests are not guaranteed to be sequential
|
||||||
|
|
||||||
func setupState() (*Db, error) {
|
func setupState() (Database, error) {
|
||||||
db := DbConnect(":memory:")
|
db := DbConnect(":memory:")
|
||||||
err := db.Migrate("../../migrations")
|
err := db.Migrate("../../migrations")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue