Class: Dumbo::ExtensionMigrator
- Inherits:
-
Object
- Object
- Dumbo::ExtensionMigrator
- Defined in:
- lib/dumbo/extension_migrator.rb
Constant Summary collapse
- TYPES =
[:types, :functions, :casts, :operators]
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#new_version ⇒ Object
readonly
Returns the value of attribute new_version.
-
#old_version ⇒ Object
readonly
Returns the value of attribute old_version.
Instance Method Summary collapse
- #cast_diff ⇒ Object
- #create ⇒ Object
- #downgrade ⇒ Object
- #function_diff ⇒ Object
-
#initialize(name, old_version, new_version) ⇒ ExtensionMigrator
constructor
A new instance of ExtensionMigrator.
- #object_diff(type, dir) ⇒ Object
- #upgrade ⇒ Object
Constructor Details
#initialize(name, old_version, new_version) ⇒ ExtensionMigrator
Returns a new instance of ExtensionMigrator.
6 7 8 9 10 11 12 |
# File 'lib/dumbo/extension_migrator.rb', line 6 def initialize(name, old_version, new_version) @name = name @old_version = Extension.new(name, old_version).create @old_version.objects @new_version = Extension.new(name, new_version).create @new_version.objects end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/dumbo/extension_migrator.rb', line 3 def name @name end |
#new_version ⇒ Object (readonly)
Returns the value of attribute new_version.
3 4 5 |
# File 'lib/dumbo/extension_migrator.rb', line 3 def new_version @new_version end |
#old_version ⇒ Object (readonly)
Returns the value of attribute old_version.
3 4 5 |
# File 'lib/dumbo/extension_migrator.rb', line 3 def old_version @old_version end |
Instance Method Details
#cast_diff ⇒ Object
41 42 43 |
# File 'lib/dumbo/extension_migrator.rb', line 41 def cast_diff object_diff(@old_version.casts, @new_version.casts) end |
#create ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/dumbo/extension_migrator.rb', line 14 def create File.open("#{name}--#{old_version.version}--#{new_version.version}.sql", 'w') do |f| f.puts upgrade end File.open("#{name}--#{new_version.version}--#{old_version.version}.sql", 'w') do |f| f.puts downgrade end end |
#downgrade ⇒ Object
30 31 32 33 34 35 |
# File 'lib/dumbo/extension_migrator.rb', line 30 def downgrade TYPES.map do |type| diff = object_diff(type, :downgrade) "----#{type}----\n" + diff if diff.present? end.compact.join("\n") end |
#function_diff ⇒ Object
37 38 39 |
# File 'lib/dumbo/extension_migrator.rb', line 37 def function_diff object_diff(@old_version.functions, @new_version.functions) end |
#object_diff(type, dir) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/dumbo/extension_migrator.rb', line 45 def object_diff(type, dir) ids = @old_version.public_send(type).map(&:identify) | @new_version.public_send(type).map(&:identify) sqls = ids.map do |id| n = @new_version.public_send(type).find { |n| n.identify == id } o = @old_version.public_send(type).find { |n| n.identify == id } if n n.public_send(dir, o) elsif o o.public_send(dir, o) end end sqls.join("\n----\n") end |
#upgrade ⇒ Object
23 24 25 26 27 28 |
# File 'lib/dumbo/extension_migrator.rb', line 23 def upgrade TYPES.map do |type| diff = object_diff(type, :upgrade) "----#{type}----\n" + diff if diff.present? end.compact.join("\n") end |