Skip to content

Commit

Permalink
Use gorm for DB interactions (#18)
Browse files Browse the repository at this point in the history
* Swap to using golang/dep as dependency manager

* Run dep prune

* Vendor github.com/jinzhu/gorm

* Swap raw sqlx logic for gorm
  • Loading branch information
ungureanuvladvictor authored Oct 2, 2017
1 parent ed7b103 commit 1ad6d6e
Show file tree
Hide file tree
Showing 84 changed files with 12,643 additions and 7,471 deletions.
17 changes: 12 additions & 5 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 4 additions & 31 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@

# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"


[[constraint]]
name = "github.com/google/go-github"
revision = "511f540f1887d30b88cee4a2fcd1f2922754acf4"
Expand All @@ -29,18 +6,10 @@
name = "github.com/ipfans/echo-session"
version = "3.1.1"

[[constraint]]
name = "github.com/jmoiron/sqlx"
revision = "d9bd385d68c068f1fabb5057e3dedcbcbb039d0f"

[[constraint]]
name = "github.com/labstack/echo"
version = "3.2.3"

[[constraint]]
name = "github.com/lib/pq"
revision = "b77235e3890a962fe8a6f8c4c7198679ca7814e7"

[[constraint]]
name = "github.com/pkg/errors"
version = "0.8.0"
Expand Down Expand Up @@ -72,3 +41,7 @@
[[constraint]]
name = "gopkg.in/yaml.v2"
revision = "eb3733d160e74a9c7e442f435eb3bea458e1d19f"

[[constraint]]
name = "github.com/jinzhu/gorm"
version = "1.0.0"
61 changes: 0 additions & 61 deletions persist/repo.go

This file was deleted.

114 changes: 17 additions & 97 deletions persist/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,108 +15,28 @@
package persist

import (
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
)

var metaSchema = `
CREATE TABLE IF NOT EXISTS schema (
version INTEGER PRIMARY KEY CHECK (version > 0)
);
CREATE UNIQUE INDEX IF NOT EXISTS schema_one_row ON schema((TRUE));`

var currSchemaVersion = 1

var schema = `
CREATE TABLE IF NOT EXISTS USERS (
github_id INTEGER PRIMARY KEY UNIQUE,
name TEXT,
token TEXT
);
CREATE TABLE IF NOT EXISTS REPOS (
id INTEGER PRIMARY KEY UNIQUE,
name TEXT,
enabled_by TEXT,
enabled_at BIGINT,
hook_id INTEGER
);
`

// Persistable are structs that can be persisted to a DB and
// are compatible with associated utility methods in this package
type Persistable interface {
InsertStmt() string
DeleteStmt() string
UpdateStmt() string
}
"time"

// Put persists a Persistable to the given DB
func Put(db *sqlx.DB, p Persistable) error {
_, err := db.NamedExec(p.InsertStmt(), p)
if err != nil {
return errors.Wrapf(err, "failed persisting %v", p)
}
return nil
}
"github.com/jinzhu/gorm"
)

// Delete deletes a Persistable from the given DB
func Delete(db *sqlx.DB, p Persistable) error {
_, err := db.NamedExec(p.DeleteStmt(), p)
if err != nil {
return errors.Wrapf(err, "failed deleting %v", p)
}
return nil
type Repository struct {
gorm.Model
GitHubID int `gorm:"column:github_id"`
Name string
EnabledBy User
EnabledAt time.Time
HookID int
}

func Update(db *sqlx.DB, p Persistable) error {
_, err := db.NamedExec(p.UpdateStmt(), p)
if err != nil {
return errors.Wrapf(err, "failed updating %v", p)
}
return nil
type User struct {
gorm.Model
GitHubID int `gorm:"column:github_id"`
Name string
Token string
}

// InitializeSchema initializes the schema for storing artifact data
func InitializeSchema(db *sqlx.DB) error {
version, err := getSchemaVersion(db)
if err != nil {
return errors.Wrapf(err, "failed to determine database schema version")
}
if version != currSchemaVersion {
err := migrateSchema(db, currSchemaVersion)
if err != nil {
return errors.Wrapf(err, "failed migrating database schema from version %d to %d", version, currSchemaVersion)
}
}
_, err = db.Exec(schema)
if err != nil {
return errors.Wrapf(err, "failed initializing database schema")
}
return nil
}

func getSchemaVersion(db *sqlx.DB) (int, error) {
_, err := db.Exec(metaSchema)
if err != nil {
return 0, errors.Wrapf(err, "failed initializing schema table")
}
var version []int
err = db.Select(&version, "SELECT version FROM schema")
if err != nil {
return 0, errors.Wrapf(err, "failed querying schema table")
}
if len(version) == 0 {
_, err := db.Exec("INSERT INTO schema (version) VALUES ($1)", currSchemaVersion)
if err != nil {
return 0, errors.Wrapf(err, "failed setting schema version in database")
}
version = append(version, currSchemaVersion)
}
return version[0], nil
}

func migrateSchema(db *sqlx.DB, schemaVersion int) error {
return errors.New("SCHEMA MIGRATION NOT IMPLEMENTED AT THIS TIME :(")
func InitializeSchema(db *gorm.DB) {
db.AutoMigrate(&Repository{}, &User{})
}
78 changes: 0 additions & 78 deletions persist/user.go

This file was deleted.

Loading

0 comments on commit 1ad6d6e

Please sign in to comment.