Class: XMigra::MigrationChain

Inherits:
Array show all
Includes:
DeclarativeMigration::ChainSupport
Defined in:
lib/xmigra/migration_chain.rb

Direct Known Subclasses

GitSpecifics::RepoStoredMigrationChain

Constant Summary collapse

HEAD_FILE =
'head.yaml'
LATEST_CHANGE =
'latest change'
MIGRATION_FILE_PATTERN =
/^\d{4}-\d\d-\d\d.*\.yaml$/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DeclarativeMigration::ChainSupport

#check_declaratives_current!, #latest_declarative_implementations, #unimplemented_declaratives

Methods inherited from Array

#insert_after

Constructor Details

#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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



55
56
57
# File 'lib/xmigra/migration_chain.rb', line 55

def path
  @path
end

Instance Method Details

#complete?Boolean

Test if the chain reaches back to the empty database

Returns:

  • (Boolean)


58
59
60
# File 'lib/xmigra/migration_chain.rb', line 58

def complete?
  length == 0 || self[0].follows.nil?
end

#includes_all?Boolean

Test if the chain encompasses all migration-like filenames in the path

Returns:

  • (Boolean)


63
64
65
# File 'lib/xmigra/migration_chain.rb', line 63

def includes_all?
  @other_migrations.empty?
end