Skip to content

Commit

Permalink
Move mysql to adapter folder
Browse files Browse the repository at this point in the history
  • Loading branch information
josephspurrier committed Jan 11, 2019
1 parent 3296fab commit 8fcbb10
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 44 deletions.
19 changes: 10 additions & 9 deletions cmd/rove/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"

"github.com/josephspurrier/rove"
"github.com/josephspurrier/rove/pkg/database"
"github.com/josephspurrier/rove/pkg/adapter/mysql"

kingpin "gopkg.in/alecthomas/kingpin.v2"
)
Expand Down Expand Up @@ -35,38 +35,39 @@ func main() {
argList := os.Args[1:]
arg := kingpin.MustParse(app.Parse(argList))

// Create a new MySQL database object.
conn, err := database.NewConnection(*cDBPrefix)
// Create a new MySQL connection.
conn, err := mysql.NewConnection(*cDBPrefix)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

m, err := database.NewMySQL(conn)
// Create a new MySQL database object.
db, err := mysql.New(conn)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

switch arg {
case cDBAll.FullCommand():
r := rove.NewFileMigration(m, *cDBAllFile)
r := rove.NewFileMigration(db, *cDBAllFile)
r.Verbose = true
err = r.Migrate(0)
case cDBUp.FullCommand():
r := rove.NewFileMigration(m, *cDBUpFile)
r := rove.NewFileMigration(db, *cDBUpFile)
r.Verbose = true
err = r.Migrate(*cDBUpCount)
case cDBReset.FullCommand():
r := rove.NewFileMigration(m, *cDBResetFile)
r := rove.NewFileMigration(db, *cDBResetFile)
r.Verbose = true
err = r.Reset(0)
case cDBDown.FullCommand():
r := rove.NewFileMigration(m, *cDBDownFile)
r := rove.NewFileMigration(db, *cDBDownFile)
r.Verbose = true
err = r.Reset(*cDBDownCount)
case cDBStatus.FullCommand():
r := rove.NewFileMigration(m, *cDBDownFile)
r := rove.NewFileMigration(db, *cDBDownFile)
r.Verbose = true
_, err = r.Status()
}
Expand Down
14 changes: 7 additions & 7 deletions cmd/rove/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import (
"os"
"testing"

"github.com/josephspurrier/rove/pkg/testutil"
"github.com/josephspurrier/rove/pkg/adapter/mysql"

"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
)

func TestMigrationAll(t *testing.T) {
_, unique := migrateAll(t)
testutil.TeardownDatabase(unique)
mysql.TeardownDatabase(unique)
}

func migrateAll(t *testing.T) (*sqlx.DB, string) {
db, unique := testutil.SetupDatabase()
db, unique := mysql.SetupDatabase()

// Set the arguments.
os.Args = []string{
Expand Down Expand Up @@ -87,16 +87,16 @@ func TestMigrationReset(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, 0, rows)

testutil.TeardownDatabase(unique)
mysql.TeardownDatabase(unique)
}

func TestMigrationUp(t *testing.T) {
_, unique := migrateUp(t)
testutil.TeardownDatabase(unique)
mysql.TeardownDatabase(unique)
}

func migrateUp(t *testing.T) (*sqlx.DB, string) {
db, unique := testutil.SetupDatabase()
db, unique := mysql.SetupDatabase()

// Set the arguments.
os.Args = []string{
Expand Down Expand Up @@ -168,5 +168,5 @@ func TestMigrationDown(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, 1, rows)

testutil.TeardownDatabase(unique)
mysql.TeardownDatabase(unique)
}
4 changes: 2 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log"

"github.com/josephspurrier/rove"
"github.com/josephspurrier/rove/pkg/database"
"github.com/josephspurrier/rove/pkg/adapter/mysql"
)

func Example() {
Expand All @@ -31,7 +31,7 @@ INSERT INTO user_status (id, status, created_at, updated_at, deleted) VALUES
--rollback TRUNCATE TABLE user_status;`

// Create a new MySQL database object.
db, err := database.NewMySQL(&database.Connection{
db, err := mysql.New(&mysql.Connection{
Hostname: "127.0.0.1",
Username: "root",
Password: "password",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package database
package mysql

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package database
package mysql

import (
"testing"
Expand Down
8 changes: 4 additions & 4 deletions pkg/database/mysql.go → pkg/adapter/mysql/mysql.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package database is a MySQL changelog adapter.
package database
// Package mysql is a MySQL changelog adapter.
package mysql

import (
"database/sql"
Expand Down Expand Up @@ -37,9 +37,9 @@ type MySQL struct {
tablename string
}

// NewMySQL connects to the database and returns an object that satisfies the
// New connects to the database and returns an object that satisfies the
// rove.Migration interface.
func NewMySQL(c *Connection) (m *MySQL, err error) {
func New(c *Connection) (m *MySQL, err error) {
// Connect to the database.
m = new(MySQL)
m.DB, err = c.Connect(true)
Expand Down
9 changes: 4 additions & 5 deletions pkg/testutil/database.go → pkg/adapter/mysql/testutil.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testutil
package mysql

import (
"fmt"
Expand All @@ -8,7 +8,6 @@ import (
"time"

"github.com/josephspurrier/rove"
"github.com/josephspurrier/rove/pkg/database"
"github.com/josephspurrier/rove/pkg/env"

"github.com/jmoiron/sqlx"
Expand Down Expand Up @@ -43,7 +42,7 @@ func unsetEnv(unique string) {

// connectDatabase returns a test database connection.
func connectDatabase(dbSpecificDB bool, unique string) *sqlx.DB {
dbc := new(database.Connection)
dbc := new(Connection)
err := env.Unmarshal(dbc, unique)
if err != nil {
fmt.Println("DB ENV Error:", err)
Expand Down Expand Up @@ -98,11 +97,11 @@ func LoadDatabaseFromFile(file string, usePrefix bool) (*sqlx.DB, string) {
if usePrefix {
db, unique = SetupDatabase()
// Create a new MySQL database object.
m := new(database.MySQL)
m := new(MySQL)
m.DB = db
r = rove.NewFileMigration(m, file)
} else {
m := new(database.MySQL)
m := new(MySQL)
m.DB = db
r = rove.NewFileMigration(m, file)
setEnv(unique)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package database
package mysql

import "database/sql"

Expand Down
27 changes: 13 additions & 14 deletions rove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import (
"testing"

"github.com/josephspurrier/rove"
"github.com/josephspurrier/rove/pkg/database"
"github.com/josephspurrier/rove/pkg/testutil"
"github.com/josephspurrier/rove/pkg/adapter/mysql"

"github.com/stretchr/testify/assert"
)

func TestFileMigration(t *testing.T) {
db, unique := testutil.SetupDatabase()
db, unique := mysql.SetupDatabase()

// Create a new MySQL database object.
m := new(database.MySQL)
m := new(mysql.MySQL)
m.DB = db

// Set up rove.
Expand Down Expand Up @@ -71,14 +70,14 @@ func TestFileMigration(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, "josephspurrier:1", s)

testutil.TeardownDatabase(unique)
mysql.TeardownDatabase(unique)
}

func TestMigrationFailDuplicate(t *testing.T) {
db, unique := testutil.SetupDatabase()
db, unique := mysql.SetupDatabase()

// Create a new MySQL database object.
m := new(database.MySQL)
m := new(mysql.MySQL)
m.DB = db

// Set up rove.
Expand All @@ -94,14 +93,14 @@ func TestMigrationFailDuplicate(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, 2, rows)

testutil.TeardownDatabase(unique)
mysql.TeardownDatabase(unique)
}

func TestInclude(t *testing.T) {
db, unique := testutil.SetupDatabase()
db, unique := mysql.SetupDatabase()

// Create a new MySQL database object.
m := new(database.MySQL)
m := new(mysql.MySQL)
m.DB = db

// Set up rove.
Expand Down Expand Up @@ -153,14 +152,14 @@ func TestInclude(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, 1, rows)

testutil.TeardownDatabase(unique)
mysql.TeardownDatabase(unique)
}

func TestChangesetMigration(t *testing.T) {
db, unique := testutil.SetupDatabase()
db, unique := mysql.SetupDatabase()

// Create a new MySQL database object.
m := new(database.MySQL)
m := new(mysql.MySQL)
m.DB = db

// Set up rove.
Expand Down Expand Up @@ -212,7 +211,7 @@ func TestChangesetMigration(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, 1, rows)

testutil.TeardownDatabase(unique)
mysql.TeardownDatabase(unique)
}

var sSuccess = `
Expand Down

0 comments on commit 8fcbb10

Please sign in to comment.