-
Notifications
You must be signed in to change notification settings - Fork 117
/
database.go
77 lines (64 loc) · 2.11 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package database
import (
"context"
"errors"
"fmt"
"time"
)
// Drivers is a registry of drivers
var Drivers = make(map[string]Driver)
// Register registers a new driver
func Register(name string, driver Driver) {
if Drivers[name] != nil {
panic(fmt.Errorf("already registered database driver with name '%s'", name))
}
Drivers[name] = driver
}
// Open opens a new database connection
func Open(driver string, dsn string) (DB, error) {
d, ok := Drivers[driver]
if !ok {
return nil, fmt.Errorf("unknown database driver: %s", driver)
}
db, err := d.Open(dsn)
if err != nil {
return nil, err
}
return db, nil
}
// Driver is the interface for DB drivers
type Driver interface {
Open(dsn string) (DB, error)
}
// DB is the interface for a database connection
type DB interface {
Close() error
Migrate(ctx context.Context) error
FindMigrationVersion(ctx context.Context) (int, error)
FindOrganizations(ctx context.Context) ([]*Organization, error)
FindOrganizationByName(ctx context.Context, name string) (*Organization, error)
CreateOrganization(ctx context.Context, name string, description string) (*Organization, error)
UpdateOrganization(ctx context.Context, name string, description string) (*Organization, error)
DeleteOrganization(ctx context.Context, name string) error
FindProjects(ctx context.Context, orgName string) ([]*Project, error)
FindProjectByName(ctx context.Context, orgName string, name string) (*Project, error)
CreateProject(ctx context.Context, orgID string, name string, description string) (*Project, error)
UpdateProject(ctx context.Context, id string, description string) (*Project, error)
DeleteProject(ctx context.Context, id string) error
}
var ErrNotFound = errors.New("database: not found")
type Organization struct {
ID string
Name string
Description string
CreatedOn time.Time `db:"created_on"`
UpdatedOn time.Time `db:"updated_on"`
}
type Project struct {
ID string
OrganizationID string `db:"organization_id"`
Name string
Description string
CreatedOn time.Time `db:"created_on"`
UpdatedOn time.Time `db:"updated_on"`
}