Skip to content

Commit 03a483f

Browse files
committed
more documentation, typos fixes, and small code fixes
1 parent 3115a87 commit 03a483f

File tree

8 files changed

+115
-105
lines changed

8 files changed

+115
-105
lines changed

README.md

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@ A basic database migration library. Status: ALPHA - API may change
44

55
The algorithm used works as follows:
66

7-
To migrate
7+
To apply one/all migration(s)
8+
9+
- find all possible migrations
10+
- find all previously applied migrations
11+
- apply the next or all the migrations (depending on if one migration is to be applied or all should be applied) in alphanumeric order by migration version that have not been previously applied
12+
13+
To rollback one version
14+
15+
- find all possible migrations
16+
- find all previously applied migrations
17+
- find the highest migration version already applied that is in the list of all possible migrations
818

919
### Installation
1020

@@ -15,7 +25,7 @@ To migrate
1525
```go
1626
// error handling omitted
1727
db, err := sql.Open("postgres", "postgres://user@localhost/test?sslmode=disable")
18-
adaptor, err := adaptor.NewPostgres(db)
28+
adapter, err := adapter.NewPostgres(db)
1929
storage := storage.NewFileStorage("/path/to/migrations/directory")
2030
/*
2131
assume a directory with with the following:
@@ -25,33 +35,33 @@ assume a directory with with the following:
2535
0002_another_migration_name_apply.sql
2636
0002_another_migration_name_rollback.sql
2737
*/
28-
m, err := dbmigrate.New(adaptor, storage)
38+
m, err := dbmigrate.New(adapter, storage)
2939
err = m.ApplyAll()
30-
# or
40+
// or
3141
err = m.ApplyOne()
32-
# or
42+
// or
3343
err = m.RollbackLatest()
3444
```
3545

36-
Both migration storage and database adaptors are pluggable. Their interfaces are simple:
46+
Both migration storage and database adapters are pluggable. Their interfaces are simple:
3747

3848
```go
3949
type Storage interface {
4050
GetMigrationPairs() ([]MigrationPair, error)
4151
}
4252
```
4353

44-
[Included Storage Adaptors](./storage/README.md)
54+
[Included Storage Adapters](./storage/README.md)
4555

4656
```go
47-
type VendorAdaptor interface {
57+
type VendorAdapter interface {
4858
GetAppliedMigrationsOrderedAsc() ([]string, error)
4959
ApplyMigration(pair MigrationPair) error
5060
RollbackMigration(pair MigrationPair) error
5161
}
5262
```
5363

54-
[Included Database Adaptors](./adaptor/README.md)
64+
[Included Database Adapters](./adapter/README.md)
5565

5666
### Goals
5767

@@ -72,8 +82,8 @@ type VendorAdaptor interface {
7282

7383
### TODO
7484

75-
- Add more database adaptors?
85+
- Add more database adapters?
7686
- Add more storage options?
7787
- More tests
7888
- Force migration at a specific version?
79-
- Add a changelog
89+
- Add a changelog

adapter/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# DB Migrate Database Adapters
2+
3+
### Postgres Usage
4+
5+
```go
6+
import "bitbucket.org/braindev/dbmigrate/adapter"
7+
8+
// NewPostgres(db *sql.DB) (*PostgresAdapter, error)
9+
pgadapter, err := adapter.NewPostgres(db)
10+
```
11+
12+
### MySQL Usage
13+
14+
```go
15+
import "bitbucket.org/braindev/dbmigrate/adapter"
16+
17+
// NewMySQL(db *sql.DB) (*MySQLAdapter, error)
18+
mysqladapter, err := adapter.NewMySQL(db)
19+
```
20+
21+
### Custom Adapter
22+
23+
To create a custom adapter implement the following interface:
24+
25+
```go
26+
type VendorAdapter interface {
27+
GetAppliedMigrationsOrderedAsc() ([]string, error)
28+
ApplyMigration(pair MigrationPair) error
29+
RollbackMigration(pair MigrationPair) error
30+
}
31+
```

adaptor/mysql.go renamed to adapter/mysql.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
package adaptor
1+
package adapter
22

33
import (
44
"database/sql"
55

66
"bitbucket.org/braindev/dbmigrate"
77
)
88

9-
// MySQLAdaptor is a PostgreSQL DBMigrate adaptor
10-
type MySQLAdaptor struct {
9+
// MySQLAdapter is a PostgreSQL DBMigrate adapter
10+
type MySQLAdapter struct {
1111
db *sql.DB
1212
}
1313

14-
// NewMySQL creates a new PostgresAdaptor
15-
func NewMySQL(db *sql.DB) (*MySQLAdaptor, error) {
16-
a := &MySQLAdaptor{
14+
// NewMySQL creates a new PostgresAdapter
15+
func NewMySQL(db *sql.DB) (*MySQLAdapter, error) {
16+
a := &MySQLAdapter{
1717
db: db,
1818
}
1919
return a, a.createMigrationsTable()
2020
}
2121

2222
// GetAppliedMigrationsOrderedAsc returns an ordered slice of string versions
2323
// of migrations that have been previously applied
24-
func (a *MySQLAdaptor) GetAppliedMigrationsOrderedAsc() ([]string, error) {
24+
func (a *MySQLAdapter) GetAppliedMigrationsOrderedAsc() ([]string, error) {
2525
const query = "SELECT `version` FROM `dbmigrations` ORDER BY `version` ASC"
2626
rows, err := a.db.Query(query)
2727
if err != nil {
@@ -42,14 +42,14 @@ func (a *MySQLAdaptor) GetAppliedMigrationsOrderedAsc() ([]string, error) {
4242

4343
// createMigrationsTable conditionally creates the migrations table if it
4444
// doesn't yet exist
45-
func (a *MySQLAdaptor) createMigrationsTable() error {
45+
func (a *MySQLAdapter) createMigrationsTable() error {
4646
const query = "CREATE TABLE IF NOT EXISTS `dbmigrations`(`version` varchar NOT NULL PRIMARY KEY)"
4747
_, err := a.db.Exec(query)
4848
return err
4949
}
5050

5151
// ApplyMigration applies the specified migration
52-
func (a *MySQLAdaptor) ApplyMigration(pair dbmigrate.MigrationPair) error {
52+
func (a *MySQLAdapter) ApplyMigration(pair dbmigrate.MigrationPair) error {
5353
_, err := a.db.Exec(pair.ApplyBody)
5454
if err != nil {
5555
return err
@@ -59,11 +59,11 @@ func (a *MySQLAdaptor) ApplyMigration(pair dbmigrate.MigrationPair) error {
5959
}
6060

6161
// RollbackMigration rolls back the specifified migration
62-
func (a *MySQLAdaptor) RollbackMigration(pair dbmigrate.MigrationPair) error {
62+
func (a *MySQLAdapter) RollbackMigration(pair dbmigrate.MigrationPair) error {
6363
_, err := a.db.Exec(pair.RollbackBody)
6464
if err != nil {
6565
return err
6666
}
6767
_, err = a.db.Exec("DELETE FROM `dbmigrations` WHERE `version` = ?", pair.Version)
68-
return nil
68+
return err
6969
}

adaptor/postgres.go renamed to adapter/postgres.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
package adaptor
1+
package adapter
22

33
import (
44
"database/sql"
55

66
"bitbucket.org/braindev/dbmigrate"
77
)
88

9-
// PostgresAdaptor is a PostgreSQL DBMigrate adaptor
10-
type PostgresAdaptor struct {
9+
// PostgresAdapter is a PostgreSQL DBMigrate adapter
10+
type PostgresAdapter struct {
1111
db *sql.DB
1212
}
1313

14-
// NewPostgres creates a new PostgresAdaptor
15-
func NewPostgres(db *sql.DB) (*PostgresAdaptor, error) {
16-
a := &PostgresAdaptor{
14+
// NewPostgres creates a new PostgresAdapter
15+
func NewPostgres(db *sql.DB) (*PostgresAdapter, error) {
16+
a := &PostgresAdapter{
1717
db: db,
1818
}
1919
return a, a.createMigrationsTable()
2020
}
2121

2222
// GetAppliedMigrationsOrderedAsc returns an ordered slice of string versions
2323
// of migrations that have been previously applied
24-
func (a *PostgresAdaptor) GetAppliedMigrationsOrderedAsc() ([]string, error) {
24+
func (a *PostgresAdapter) GetAppliedMigrationsOrderedAsc() ([]string, error) {
2525
const query = `SELECT "version" FROM "dbmigrations" ORDER BY "version" ASC`
2626
rows, err := a.db.Query(query)
2727
if err != nil {
@@ -42,14 +42,14 @@ func (a *PostgresAdaptor) GetAppliedMigrationsOrderedAsc() ([]string, error) {
4242

4343
// createMigrationsTable conditionally creates the migrations table if it
4444
// doesn't yet exist
45-
func (a *PostgresAdaptor) createMigrationsTable() error {
45+
func (a *PostgresAdapter) createMigrationsTable() error {
4646
const query = `CREATE TABLE IF NOT EXISTS "dbmigrations"("version" varchar NOT NULL PRIMARY KEY)`
4747
_, err := a.db.Exec(query)
4848
return err
4949
}
5050

5151
// ApplyMigration applies the specified migration
52-
func (a *PostgresAdaptor) ApplyMigration(pair dbmigrate.MigrationPair) error {
52+
func (a *PostgresAdapter) ApplyMigration(pair dbmigrate.MigrationPair) error {
5353
_, err := a.db.Exec(pair.ApplyBody)
5454
if err != nil {
5555
return err
@@ -59,11 +59,11 @@ func (a *PostgresAdaptor) ApplyMigration(pair dbmigrate.MigrationPair) error {
5959
}
6060

6161
// RollbackMigration rolls back the specifified migration
62-
func (a *PostgresAdaptor) RollbackMigration(pair dbmigrate.MigrationPair) error {
62+
func (a *PostgresAdapter) RollbackMigration(pair dbmigrate.MigrationPair) error {
6363
_, err := a.db.Exec(pair.RollbackBody)
6464
if err != nil {
6565
return err
6666
}
6767
_, err = a.db.Exec(`DELETE FROM "dbmigrations" WHERE "version" = $1`, pair.Version)
68-
return nil
68+
return err
6969
}

adaptor/README.md

Lines changed: 0 additions & 31 deletions
This file was deleted.

dbmigrate.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"sort"
66
)
77

8-
// VendorAdaptor is the set of functionality needed to implement a compatibility with a database
8+
// VendorAdapter is the set of functionality needed to implement a compatibility with a database
99
// vendor
10-
type VendorAdaptor interface {
10+
type VendorAdapter interface {
1111
GetAppliedMigrationsOrderedAsc() ([]string, error)
1212
ApplyMigration(pair MigrationPair) error
1313
RollbackMigration(pair MigrationPair) error
@@ -18,7 +18,7 @@ type Storage interface {
1818
GetMigrationPairs() ([]MigrationPair, error)
1919
}
2020

21-
// MigrationPair stores the a pair of apply/rollback migrations
21+
// MigrationPair stores a pair of apply/rollback migrations
2222
type MigrationPair struct {
2323
Version string
2424
Name string
@@ -31,7 +31,7 @@ type DBMigrate struct {
3131
sortedMigrationPairs []MigrationPair
3232
migrationPairs map[string]MigrationPair
3333
allVersions []string
34-
adaptor VendorAdaptor
34+
adapter VendorAdapter
3535
storage Storage
3636
verbose bool // TODO
3737
}
@@ -43,9 +43,9 @@ func (a migrationPairs) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
4343
func (a migrationPairs) Less(i, j int) bool { return a[i].Version < a[j].Version }
4444

4545
// New returns an initialized DBMigrate instance.
46-
func New(adaptor VendorAdaptor, storage Storage) (*DBMigrate, error) {
46+
func New(adapter VendorAdapter, storage Storage) (*DBMigrate, error) {
4747
dbMigrate := DBMigrate{
48-
adaptor: adaptor,
48+
adapter: adapter,
4949
storage: storage,
5050
}
5151
if migrationPairs, err := storage.GetMigrationPairs(); err == nil {
@@ -76,7 +76,7 @@ func (dbMigrate *DBMigrate) ApplyOne() error {
7676
}
7777

7878
func (dbMigrate *DBMigrate) apply(all bool) error {
79-
appliedVersions, err := dbMigrate.adaptor.GetAppliedMigrationsOrderedAsc()
79+
appliedVersions, err := dbMigrate.adapter.GetAppliedMigrationsOrderedAsc()
8080
if err != nil {
8181
return err
8282
}
@@ -93,7 +93,7 @@ func (dbMigrate *DBMigrate) apply(all bool) error {
9393

9494
for _, version := range versionsToApply {
9595
fmt.Println("Applying migration", dbMigrate.migrationPairs[version].Name)
96-
err = dbMigrate.adaptor.ApplyMigration(dbMigrate.migrationPairs[version])
96+
err = dbMigrate.adapter.ApplyMigration(dbMigrate.migrationPairs[version])
9797
if err != nil {
9898
return err
9999
}
@@ -107,14 +107,14 @@ func (dbMigrate *DBMigrate) apply(all bool) error {
107107

108108
// RollbackLatest rolls back the latest applied migration
109109
func (dbMigrate *DBMigrate) RollbackLatest() error {
110-
appliedVersions, err := dbMigrate.adaptor.GetAppliedMigrationsOrderedAsc()
110+
appliedVersions, err := dbMigrate.adapter.GetAppliedMigrationsOrderedAsc()
111111
if err != nil {
112112
return err
113113
}
114114
for i := len(dbMigrate.sortedMigrationPairs) - 1; i >= 0; i-- {
115115
if stringInSlice(dbMigrate.sortedMigrationPairs[i].Version, appliedVersions) {
116116
fmt.Println("rolling back migration", dbMigrate.sortedMigrationPairs[i].Name)
117-
err = dbMigrate.adaptor.RollbackMigration(dbMigrate.sortedMigrationPairs[i])
117+
err = dbMigrate.adapter.RollbackMigration(dbMigrate.sortedMigrationPairs[i])
118118
if err != nil {
119119
return err
120120
}

0 commit comments

Comments
 (0)