Class: SchemaEvolutionManager::Versions

Inherits:
Object
  • Object
show all
Defined in:
lib/schema-evolution-manager/versions.rb

Overview

Records and reads the released repository version applied to a database. The version is written at apply time and the latest row (max id) is the deployed schema version.

Constant Summary collapse

MAX_VERSION_LENGTH =
100

Instance Method Summary collapse

Constructor Details

#initialize(db) ⇒ Versions

Returns a new instance of Versions.



32
33
34
# File 'lib/schema-evolution-manager/versions.rb', line 32

def initialize(db)
  @db = Preconditions.assert_class(db, Db)
end

Instance Method Details

#latestObject

Latest recorded version, or nil if none recorded / table absent.



46
47
48
49
50
# File 'lib/schema-evolution-manager/versions.rb', line 46

def latest
  return nil unless versions_table_exists?
  value = @db.psql_command("select version from %s.versions order by id desc limit 1" % Db.schema_name).strip
  value.empty? ? nil : value
end

#record!(version) ⇒ Object

Inserts the version only if it differs from the latest recorded value. Returns true if a row was inserted, false if unchanged.



38
39
40
41
42
43
# File 'lib/schema-evolution-manager/versions.rb', line 38

def record!(version)
  clean = Versions.validate_version!(version)
  return false if latest == clean
  @db.psql_command("insert into %s.versions (version) values ('%s')" % [Db.schema_name, clean])
  true
end