Skip to content

Commit

Permalink
Revert gorm changes (#33)
Browse files Browse the repository at this point in the history
* Revert "Use gorm for DB interactions (#18)"

This reverts commit 1ad6d6e.
  • Loading branch information
ajlake authored Feb 12, 2018
1 parent 467be24 commit 24fbe22
Show file tree
Hide file tree
Showing 84 changed files with 7,471 additions and 12,643 deletions.
17 changes: 5 additions & 12 deletions Gopkg.lock

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

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

# 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 @@ -6,10 +29,18 @@
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 @@ -41,7 +72,3 @@
[[constraint]]
name = "gopkg.in/yaml.v2"
revision = "eb3733d160e74a9c7e442f435eb3bea458e1d19f"

[[constraint]]
name = "github.com/jinzhu/gorm"
version = "1.0.0"
61 changes: 61 additions & 0 deletions persist/repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2017 Palantir Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package persist

import (
"fmt"
"strings"

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

type Repository struct {
ID int `db:"id"`
Name string `db:"name"`
EnabledBy string `db:"enabled_by"`
EnabledAt int64 `db:"enabled_at"`
HookID int `db:"hook_id"`
}

func (*Repository) InsertStmt() string {
return "INSERT INTO REPOS (id, name, enabled_by, enabled_at, hook_id) VALUES (:id, :name, :enabled_by, :enabled_at, :hook_id)"
}

func (*Repository) DeleteStmt() string {
return "DELETE FROM REPOS WHERE id = :id"
}

func (*Repository) UpdateStmt() string {
return "TODO"
}

func GetRepositoryByID(db *sqlx.DB, repoID int) (*Repository, error) {
r := &Repository{}

q := fmt.Sprintf("SELECT * FROM REPOS WHERE id=%d", repoID)
row := db.QueryRowx(q)
err := row.StructScan(r)

if err != nil {
if strings.Contains(err.Error(), "no rows in result set") {
return nil, nil
}

return nil, errors.Wrapf(err, "cannot get repo %d", repoID)
}

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

import (
"time"

"github.com/jinzhu/gorm"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
)

type Repository struct {
gorm.Model
GitHubID int `gorm:"column:github_id"`
Name string
EnabledBy User
EnabledAt time.Time
HookID int
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
}

// 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
}

// 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 User struct {
gorm.Model
GitHubID int `gorm:"column:github_id"`
Name string
Token string
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
}

// InitializeSchema initializes the schema for storing artifact data
func InitializeSchema(db *gorm.DB) {
db.AutoMigrate(&Repository{}, &User{})
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 :(")
}
78 changes: 78 additions & 0 deletions persist/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2017 Palantir Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package persist

import (
"fmt"
"strings"

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

type User struct {
GithubID int `db:"github_id"`
Name string `db:"name"`
Token string `db:"token"`
}

func (*User) InsertStmt() string {
return "INSERT INTO USERS (github_id, name, token) VALUES (:github_id, :name, :token)"
}

func (*User) DeleteStmt() string {
return "DELETE FROM USERS WHERE github_id = :github_id"
}

func (*User) UpdateStmt() string {
return "UPDATE USERS SET token = :token WHERE github_id = :github_id"
}

func GetUserByName(db *sqlx.DB, name string) (*User, error) {
u := &User{}

q := fmt.Sprintf("SELECT * FROM USERS WHERE name='%s'", name)
row := db.QueryRowx(q)
err := row.StructScan(u)

if err != nil {
if strings.Contains(err.Error(), "no rows in result set") {
return nil, nil
}

return nil, errors.Wrapf(err, "cannot get user %s", name)
}

return u, nil
}

func GetUserByID(db *sqlx.DB, id int) (*User, error) {
u := &User{}

q := fmt.Sprintf("SELECT * FROM USERS WHERE github_id='%d'", id)
row := db.QueryRowx(q)
err := row.StructScan(u)

if err != nil {
return nil, errors.Wrapf(err, "cannot get user %d", id)
}

return u, nil
}

func UpdateUserToken(db *sqlx.DB, id int, token string) error {
q := fmt.Sprintf("UPDATE USERS SET token='%s' WHERE github_id='%d'", token, id)
return db.QueryRowx(q).Err()
}
Loading

0 comments on commit 24fbe22

Please sign in to comment.