Skip to content

Commit

Permalink
Use counter for migration version information instead of rows
Browse files Browse the repository at this point in the history
  • Loading branch information
dinedal committed Aug 27, 2014
1 parent ca8cd2c commit c7e6d54
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions driver/cassandra/cassandra.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Driver struct {
}

const tableName = "schema_migrations"
const versionRow = 1

// Cassandra Driver URL format:
// cassandra://host:port/keyspace
Expand Down Expand Up @@ -47,7 +48,7 @@ func (driver *Driver) Close() error {
}

func (driver *Driver) ensureVersionTableExists() error {
err := driver.session.Query("CREATE TABLE IF NOT EXISTS " + tableName + " (version bigint primary key);").Exec()
err := driver.session.Query("CREATE TABLE IF NOT EXISTS " + tableName + " (version counter, versionRow bigint primary key);").Exec()
if err != nil {
return err
}
Expand All @@ -63,13 +64,13 @@ func (driver *Driver) Migrate(f file.File, pipe chan interface{}) {
pipe <- f

if f.Direction == direction.Up {
err := driver.session.Query("INSERT INTO "+tableName+" (version) VALUES (?)", f.Version).Exec()
err := driver.session.Query("UPDATE "+tableName+" SET version = version + 1 where versionRow = ?", versionRow).Exec()
if err != nil {
pipe <- err
return
}
} else if f.Direction == direction.Down {
err := driver.session.Query("DELETE FROM "+tableName+" WHERE version = ?", f.Version).Exec()
err := driver.session.Query("UPDATE "+tableName+" SET version = version - 1 where versionRow = ?", versionRow).Exec()
if err != nil {
pipe <- err
return
Expand All @@ -91,6 +92,6 @@ func (driver *Driver) Migrate(f file.File, pipe chan interface{}) {

func (driver *Driver) Version() (uint64, error) {
var version int64
err := driver.session.Query("SELECT version FROM " + tableName + " ORDER BY version DESC LIMIT 1").Scan(&version)
err := driver.session.Query("SELECT version FROM "+tableName+" WHERE versionRow = ?", versionRow).Scan(&version)
return uint64(version), err
}

0 comments on commit c7e6d54

Please sign in to comment.