Method: XMigra::MigrationChain#initialize

Defined in:
lib/xmigra/migration_chain.rb

#initialize(path, options = {}) ⇒ MigrationChain

Returns a new instance of MigrationChain.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/xmigra/migration_chain.rb', line 13

def initialize(path, options={})
  super()
  @path = Pathname(path)
  
  db_specifics = options[:db_specifics]
  vcs_specifics = options[:vcs_specifics]
  
  head_info = yaml_of_file(File.join(path, HEAD_FILE)) || {}
  file = head_info[LATEST_CHANGE]
  prev_file = HEAD_FILE
  files_loaded = []
  
  until file.nil?
    file = XMigra.yaml_path(file)
    fpath = File.join(path, file)
    break if (mig_info = yaml_of_file(fpath)).nil?
    files_loaded << file
    mig_info["id"] = Migration::id_from_filename(file)
    migration = Migration.new(mig_info)
    migration.file_path = File.expand_path(fpath)
    migration.extend(db_specifics) if db_specifics
    migration.extend(vcs_specifics) if vcs_specifics
    if migration.file_path.end_with? ".decl.yaml"
      migration.extend(DeclarativeMigration)
    end
    unshift(migration)
    prev_file = file
    file = migration.follows
    unless file.nil? || MIGRATION_FILE_PATTERN.match(XMigra.yaml_path(file))
      raise XMigra::Error, "Invalid migration file \"#{file}\" referenced from \"#{prev_file}\""
    end
  end
  
  @other_migrations = []
  Dir.foreach(path) do |fname|
    if MIGRATION_FILE_PATTERN.match(fname) && !files_loaded.include?(fname)
      @other_migrations << fname.freeze
    end
  end
  @other_migrations.freeze
end