Skip to content

Commit

Permalink
CockroachDB driver improvements
Browse files Browse the repository at this point in the history
    - Correctly return database.ErrLocked when the DB is locked
    - Group imports
    - gofmt goodness
  • Loading branch information
dhui committed May 31, 2018
1 parent c984265 commit a890d44
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions database/cockroachdb/cockroachdb.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package cockroachdb

import (
"context"
"database/sql"
"fmt"
"io"
"io/ioutil"
nurl "net/url"
"regexp"
"strconv"
)

import (
"github.com/cockroachdb/cockroach-go/crdb"
"github.com/lib/pq"
)

import (
"github.com/golang-migrate/migrate"
"github.com/golang-migrate/migrate/database"
"regexp"
"strconv"
"context"
)

func init() {
Expand All @@ -33,8 +38,8 @@ var (

type Config struct {
MigrationsTable string
LockTable string
ForceLock bool
LockTable string
ForceLock bool
DatabaseName string
}

Expand Down Expand Up @@ -126,8 +131,8 @@ func (c *CockroachDb) Open(url string) (database.Driver, error) {
px, err := WithInstance(db, &Config{
DatabaseName: purl.Path,
MigrationsTable: migrationsTable,
LockTable: lockTable,
ForceLock: forceLock,
LockTable: lockTable,
ForceLock: forceLock,
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -159,11 +164,11 @@ func (c *CockroachDb) Lock() error {
// If row exists at all, lock is present
locked := rows.Next()
if locked && !c.config.ForceLock {
return database.Error{Err: "lock could not be acquired; already locked", Query: []byte(query)}
return database.ErrLocked
}

query = "INSERT INTO " + c.config.LockTable + " (lock_id) VALUES ($1)"
if _, err := tx.Exec(query, aid) ; err != nil {
if _, err := tx.Exec(query, aid); err != nil {
return database.Error{OrigErr: err, Err: "failed to set migration lock", Query: []byte(query)}
}

Expand Down Expand Up @@ -228,7 +233,7 @@ func (c *CockroachDb) SetVersion(version int, dirty bool) error {
}

if version >= 0 {
if _, err := tx.Exec(`INSERT INTO "` + c.config.MigrationsTable + `" (version, dirty) VALUES ($1, $2)`, version, dirty); err != nil {
if _, err := tx.Exec(`INSERT INTO "`+c.config.MigrationsTable+`" (version, dirty) VALUES ($1, $2)`, version, dirty); err != nil {
return err
}
}
Expand Down Expand Up @@ -316,7 +321,6 @@ func (c *CockroachDb) ensureVersionTable() error {
return nil
}


func (c *CockroachDb) ensureLockTable() error {
// check if lock table exists
var count int
Expand Down

0 comments on commit a890d44

Please sign in to comment.