Class: MaglevRecord::Migration
- Defined in:
- lib/maglev_record/migration/migration.rb
Overview
Migration.new(“2013-02-02 1, 10, 0, 0, 0), ”Change book title“) do
def up
Book.each do |book|
book.title = "A new book title"
end
end
def down
Book.each do |book|
book.title = "Back to old title"
end
end
end
Furthermore, this class offers methods to actually do data migration.
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#source ⇒ Object
Returns the value of attribute source.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
Class Method Summary collapse
- .const_missing(name) ⇒ Object
- .file_content(time, description, upcode = nil, downcode = nil) ⇒ Object
- .id_for(timestamp, name) ⇒ Object
- .new(timestamp, name) ⇒ Object
- .write_to_file(folder, description, upcode = nil, downcode = nil) ⇒ Object
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #delete_class(cls) ⇒ Object
- #do ⇒ Object
- #done? ⇒ Boolean
- #down ⇒ Object
- #hash ⇒ Object
- #id ⇒ Object
-
#initialize(timestamp, name, &block) ⇒ Migration
constructor
A new instance of Migration.
- #inspect ⇒ Object
- #rename_class(old_class, new_name) ⇒ Object
- #to_s ⇒ Object
- #undo ⇒ Object
Constructor Details
#initialize(timestamp, name, &block) ⇒ Migration
Returns a new instance of Migration.
38 39 40 41 42 43 |
# File 'lib/maglev_record/migration/migration.rb', line 38 def initialize(, name, &block) @timestamp = @name = name @done = false instance_eval &block unless block.nil? end |
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
34 35 36 |
# File 'lib/maglev_record/migration/migration.rb', line 34 def logger @logger end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
36 37 38 |
# File 'lib/maglev_record/migration/migration.rb', line 36 def name @name end |
#source ⇒ Object
Returns the value of attribute source.
34 35 36 |
# File 'lib/maglev_record/migration/migration.rb', line 34 def source @source end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
35 36 37 |
# File 'lib/maglev_record/migration/migration.rb', line 35 def @timestamp end |
Class Method Details
.const_missing(name) ⇒ Object
106 107 108 109 |
# File 'lib/maglev_record/migration/migration.rb', line 106 def self.const_missing(name) #logger.warn("class #{name} was not created but migration by migration #{self.id}") if logger MigrationOperations::NullClass.new(name) end |
.file_content(time, description, upcode = nil, downcode = nil) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/maglev_record/migration/migration.rb', line 111 def self.file_content(time, description, upcode = nil, downcode = nil) = time.to_s Time.parse() # make sure there is no error in the string upcode = " # put your transformation code here" if upcode.nil? if downcode.nil? downcode = " # put the code that reverses the code in up here \n" + " # remove the next line that throws he error \n" + " raise IrreversibleMigration, " + "'The migration has no downcode'" end <<-eos require "maglev_record/migration" require "time" MaglevRecord::Migration.new(Time.parse('#{ .escape_single_quotes}'), '#{ description.escape_single_quotes}') do def up #{upcode} end def down #{downcode} end end eos end |
.id_for(timestamp, name) ⇒ Object
53 54 55 56 57 |
# File 'lib/maglev_record/migration/migration.rb', line 53 def self.id_for(, name) [.month, .day, .hour, .min, .sec].reduce(.year.to_s) do |sum, s| sum + s.to_s.rjust(2, '0') end + name.to_s end |
.new(timestamp, name) ⇒ Object
68 69 70 71 72 |
# File 'lib/maglev_record/migration/migration.rb', line 68 def self.new(, name) = Time.parse() if .kind_of? String id = id_for(, name) migration = super(, name) end |
.write_to_file(folder, description, upcode = nil, downcode = nil) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/maglev_record/migration/migration.rb', line 142 def self.write_to_file(folder, description, upcode = nil, downcode = nil) now = Time.now filename = now.strftime("migration_%Y-%m-%b-%d_%H.%M.%S.rb") # TODO the arguments point at this to need to be refactored to an own # class content = file_content(now, description, upcode, downcode) filepath = File.join(folder, filename) File.open(filepath, 'w') { |file| file.write(content) } filepath end |
Instance Method Details
#<=>(other) ⇒ Object
92 93 94 95 96 |
# File 'lib/maglev_record/migration/migration.rb', line 92 def <=>(other) compare = <=> other. return compare if compare != 0 name <=> other.name end |
#delete_class(cls) ⇒ Object
102 103 104 |
# File 'lib/maglev_record/migration/migration.rb', line 102 def delete_class(cls) cls.migration_delete end |
#do ⇒ Object
78 79 80 81 |
# File 'lib/maglev_record/migration/migration.rb', line 78 def do up if respond_to?(:up) && !done? @done = true end |
#done? ⇒ Boolean
74 75 76 |
# File 'lib/maglev_record/migration/migration.rb', line 74 def done? @done end |
#down ⇒ Object
88 89 90 |
# File 'lib/maglev_record/migration/migration.rb', line 88 def down raise IrreversibleMigration, "The migration has no down code specified. Do something about it man. Either give it up or write a down method into the migration definition." end |
#hash ⇒ Object
49 50 51 |
# File 'lib/maglev_record/migration/migration.rb', line 49 def hash id.hash end |
#id ⇒ Object
45 46 47 |
# File 'lib/maglev_record/migration/migration.rb', line 45 def id self.class.id_for(, name) end |
#inspect ⇒ Object
63 64 65 66 |
# File 'lib/maglev_record/migration/migration.rb', line 63 def inspect return source unless source.nil? to_s end |
#rename_class(old_class, new_name) ⇒ Object
98 99 100 |
# File 'lib/maglev_record/migration/migration.rb', line 98 def rename_class(old_class, new_name) old_class.migration_rename_to(new_name) end |
#to_s ⇒ Object
59 60 61 |
# File 'lib/maglev_record/migration/migration.rb', line 59 def to_s self.class.name + "<\"#{.year}-#{.month.to_s.rjust(2, '0')}-#{.day.to_s.rjust(2, '0')} #{.hour.to_s.rjust(2, '0')}:#{.min.to_s.rjust(2, '0')}:#{.sec.to_s.rjust(2, '0')}\", \"#{name}\">" end |
#undo ⇒ Object
83 84 85 86 |
# File 'lib/maglev_record/migration/migration.rb', line 83 def undo down if done? @done = false end |