Module: XMigra::DeclarativeMigration::ChainSupport

Included in:
MigrationChain
Defined in:
lib/xmigra/declarative_migration.rb

Instance Method Summary collapse

Instance Method Details

#check_declaratives_current!Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/xmigra/declarative_migration.rb', line 72

def check_declaratives_current!
  unless unimplemented_declaratives.empty?
    raise(
      MissingImplementationError,
      "Declaratives without migration implementing current state:\n" +
      unimplemented_declaratives.collect {|df| "    #{df.basename('.yaml')}\n"}.join("")
    )
  end
  
  questionable_migrations = latest_declarative_implementations.values.select {|m| m.questionable?}
  unless questionable_migrations.empty?
    raise(
      QuestionableImplementationError,
      "Implementing migrations with questionable SQL:\n" +
      questionable_migrations.collect {|m| "    #{m.file_path}\n"}.join("")
    )
  end
  
  questionable_migrations = latest_declarative_implementations.values.each do |m|
    next unless m.management_migration?
    raise(
      QuestionableImplementationError,
      "#{m.file_path} cannot execute SQL for a declarative #{m.goal}"
    ) unless m.sql.nil? || m.sql.empty?
  end
end

#latest_declarative_implementationsObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/xmigra/declarative_migration.rb', line 32

def latest_declarative_implementations
  @latest_declarative_implementations ||= Hash.new do |h, file_path|
    ext_path = Pathname(file_path).expand_path
    if h.has_key? ext_path
      next h[ext_path]
    end
    raise Error, (
      "Unexpected file path '#{file_path}', known file paths:" +
      h.keys.collect {|kp| "    #{kp}\n"}.join('')
    )
  end.tap do |files|
    each do |migration|
      # Skip non-declarative migrations
      next unless migration.is_a? DeclarativeMigration
      if (
        [:renunciation, :destruction].include?(migration.goal) &&
        migration.declarative_status == :missing
      )
        files.delete(migration.declarative_file_path)
      else
        files[migration.declarative_file_path] = migration
      end
    end
    
    Dir.glob(Pathname(path).join(SUBDIR, '*.yaml').to_s) do |decl_file|
      decl_file_path = Pathname(decl_file).expand_path
      next if files.has_key?(decl_file_path)
      files[decl_file_path] = DeclarativeMigration::Missing
    end
    
    files.freeze
  end
end

#unimplemented_declarativesObject



66
67
68
69
70
# File 'lib/xmigra/declarative_migration.rb', line 66

def unimplemented_declaratives
  @unimplemented_declaratives ||= latest_declarative_implementations.reject do |file_path, migration|
    [:equal, :older].include? migration.declarative_status
  end.keys
end