Skip to content

Commit

Permalink
sql: change create schemas signature
Browse files Browse the repository at this point in the history
  • Loading branch information
arekkas committed May 5, 2017
1 parent 0103ce1 commit c0fffe8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
11 changes: 11 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->


- [0.7.0](#070)
- [0.6.0](#060)
- [New location](#new-location)
- [Deprecating Redis and RethinkDB](#deprecating-redis-and-rethinkdb)
Expand All @@ -12,6 +14,15 @@

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## 0.7.0

Version 0.7.0 includes two minor BC breaks in the SQLManager. The method signature `CreateSchemas() ( error)`
was changed to `CreateSchemas(schema, table string) (int, error)` where int now returns the number of migrations applied.
Arguments `schema` and `table` are passed to the migration script, defining which schema and table name should be used
to store and look up migration plans.

To keep the default values from the migrate package, use `CreateSchemas("", "")`. It is safe to reapply all migration
commands with this version - implying that you can choose an arbitrary name and it won't break your schema.

## 0.6.0

Expand Down
10 changes: 6 additions & 4 deletions manager/sql/manager_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,24 @@ func NewSQLManager(db *sqlx.DB, schema []string) *SQLManager {
}

// CreateSchemas creates ladon_policy tables
func (s *SQLManager) CreateSchemas() error {
func (s *SQLManager) CreateSchemas(schema, table string) (int, error) {
var source *migrate.MemoryMigrationSource
switch s.db.DriverName() {
case "postgres", "pgx":
source = migrations["postgres"]
case "mysql":
source = migrations["mysql"]
default:
return errors.Errorf("Database driver %s is not supported", s.db.DriverName())
return 0, errors.Errorf("Database driver %s is not supported", s.db.DriverName())
}

migrate.SetSchema(schema)
migrate.SetTable(table)
n, err := migrate.Exec(s.db.DB, s.db.DriverName(), source, migrate.Up)
if err != nil {
return errors.Wrapf(err, "Could not migrate sql schema, applied %d migrations", n)
return 0, errors.Wrapf(err, "Could not migrate sql schema, applied %d migrations", n)
}
return nil
return n, nil
}

// Create inserts a new policy
Expand Down
4 changes: 2 additions & 2 deletions manager_all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func connectPG(wg *sync.WaitGroup) {
defer wg.Done()
var db = integration.ConnectToPostgres("ladon")
s := NewSQLManager(db, nil)
if err := s.CreateSchemas(); err != nil {
if _, err := s.CreateSchemas("",""); err != nil {
log.Fatalf("Could not create postgres schema: %v", err)
}

Expand All @@ -183,7 +183,7 @@ func connectMySQL(wg *sync.WaitGroup) {
defer wg.Done()
var db = integration.ConnectToMySQL()
s := NewSQLManager(db, nil)
if err := s.CreateSchemas(); err != nil {
if _, err := s.CreateSchemas("",""); err != nil {
log.Fatalf("Could not create mysql schema: %v", err)
}

Expand Down
2 changes: 1 addition & 1 deletion xxx_manager_sql_migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestSQLManagerMigrateFromMajor0Minor6ToMajor0Minor7(t *testing.T) {
// var db = getSqlDatabaseFromSomewhere()
// s := NewSQLManager(db, nil)
//
// if err := s.CreateSchemas(); err != nil {
// if _, err := s.CreateSchemas(); err != nil {
// log.Fatalf("Could not create mysql schema: %v", err)
// }
//
Expand Down

0 comments on commit c0fffe8

Please sign in to comment.