Class: SchemaEvolutionManager::Scripts

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

Constant Summary collapse

SCRIPTS =
"scripts"
BOOTSTRAP_SCRIPTS =
"bootstrap_scripts"
VALID_TABLE_NAMES =
[BOOTSTRAP_SCRIPTS, SCRIPTS]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, table_name) ⇒ Scripts

we have processed. Will be one of ‘scripts’ or ‘bootstrap_scripts’

Parameters:

  • db

    Instance of Db class

  • table_name

    Name of the table used to record which scripts



14
15
16
17
18
19
# 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

Class Method Details

.all(dir) ⇒ Object

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



23
24
25
26
27
28
29
30
31
# 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

Instance Method Details

#each_pending(dir) ⇒ Object

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
# 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) ⇒ Object

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



75
76
77
78
79
80
# 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