class SchemaEvolutionManager::Scripts

Constants

BOOTSTRAP_SCRIPTS
SCRIPTS
VALID_TABLE_NAMES

Public Class Methods

all(dir) click to toggle source

Returns a sorted list of the full file paths to any sql scripts in the specified directory

# File lib/schema-evolution-manager/scripts.rb, line 23
def Scripts.all(dir)
  Preconditions.assert_class(dir, String)

  if File.directory?(dir)
    Dir.glob("#{dir}/*.sql").sort
  else
    []
  end
end
new(db, table_name) click to toggle source

@param db Instance of Db class @param table_name Name of the table used to record which scripts we have processed. Will be one of ‘scripts’ or ‘bootstrap_scripts’

# File lib/schema-evolution-manager/scripts.rb, line 14
def initialize(db, table_name)
  @db = Preconditions.assert_class(db, Db)
  @table_name = Preconditions.assert_class(table_name, String)
  Preconditions.check_state(VALID_TABLE_NAMES.include?(@table_name),
                            "Invalid table name[%s]. Must be one of: %s" % [@table_name, VALID_TABLE_NAMES.join(", ")])
end

Public Instance Methods

each_pending(dir) { |filename, files| ... } click to toggle source

For each sql script that needs to be applied to this database, yields a pair of |filename, fullpath| in proper order

db = Db.new(host, user, name) scripts = Scripts.new(db) scripts.each_pending do |filename, path|

puts filename

end

# File lib/schema-evolution-manager/scripts.rb, line 41
def each_pending(dir)
  files = {}
  Scripts.all(dir).each do |path|
    name = File.basename(path)
    files[name] = path
  end

  scripts_previously_run(files.keys).each do |filename|
    files.delete(filename)
  end

  files.keys.sort.each do |filename|
    ## We have to recheck if this script is still pending. Some
    ## upgrade scripts may modify the scripts table themselves. This
    ## is actually useful in cases like when we migrated gilt from
    ## util_schema => schema_evolution_manager schema
    if !has_run?(filename)
      yield filename, files[filename]
    end
  end
end
has_run?(filename) click to toggle source

True if this script has already been applied to the db. False otherwise.

# File lib/schema-evolution-manager/scripts.rb, line 65
def has_run?(filename)
  if @db.schema_schema_evolution_manager_exists?
    query = "select count(*) from %s.%s where filename = '%s'" % [Db.schema_name, @table_name, filename]
    @db.psql_command(query).to_i > 0
  else
    false
  end
end
record_as_run!(filename) click to toggle source

Inserts a record to indiciate that we have loaded the specified file.

# File lib/schema-evolution-manager/scripts.rb, line 75
def record_as_run!(filename)
  Preconditions.check_state(filename.match(/^\d\d\d\d\d\d+\-\d\d\d\d\d\d\.sql$/),
                            "Invalid filename[#{filename}]. Must be like: 20120503-173242.sql")
  command = "insert into %s.%s (filename) select '%s' where not exists (select 1 from %s.%s where filename = '%s')" % [Db.schema_name, @table_name, filename, Db.schema_name, @table_name, filename]
  @db.psql_command(command)
end

Private Instance Methods

scripts_previously_run(scripts) click to toggle source

Fetch the list of scripts that have already been applied to this database.

# File lib/schema-evolution-manager/scripts.rb, line 85
def scripts_previously_run(scripts)
  if scripts.empty? || !@db.schema_schema_evolution_manager_exists?
    []
  else
    sql = "select filename from %s.%s where filename in (%s)" % [Db.schema_name, @table_name, "'" + scripts.join("', '") + "'"]
    @db.psql_command(sql).strip.split
  end
end