Class: MaglevRecord::Migration

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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(timestamp, name, &block)
  @timestamp = timestamp
  @name = name
  @done = false
  instance_eval &block unless block.nil?
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



34
35
36
# File 'lib/maglev_record/migration/migration.rb', line 34

def logger
  @logger
end

#nameObject (readonly)

Returns the value of attribute name.



36
37
38
# File 'lib/maglev_record/migration/migration.rb', line 36

def name
  @name
end

#sourceObject

Returns the value of attribute source.



34
35
36
# File 'lib/maglev_record/migration/migration.rb', line 34

def source
  @source
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



35
36
37
# File 'lib/maglev_record/migration/migration.rb', line 35

def timestamp
  @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)
  timestamp = time.to_s
  Time.parse(timestamp) # 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('#{
                      timestamp.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(timestamp, name)
  [timestamp.month, timestamp.day, timestamp.hour, timestamp.min, timestamp.sec].reduce(timestamp.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(timestamp, name)
  timestamp = Time.parse(timestamp) if timestamp.kind_of? String
  id = id_for(timestamp, name)
  migration = super(timestamp, 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 = timestamp <=> other.timestamp
  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

#doObject



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

Returns:

  • (Boolean)


74
75
76
# File 'lib/maglev_record/migration/migration.rb', line 74

def done?
  @done
end

#downObject



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

#hashObject



49
50
51
# File 'lib/maglev_record/migration/migration.rb', line 49

def hash
  id.hash
end

#idObject



45
46
47
# File 'lib/maglev_record/migration/migration.rb', line 45

def id
  self.class.id_for(timestamp, name)
end

#inspectObject



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_sObject



59
60
61
# File 'lib/maglev_record/migration/migration.rb', line 59

def to_s
  self.class.name + "<\"#{timestamp.year}-#{timestamp.month.to_s.rjust(2, '0')}-#{timestamp.day.to_s.rjust(2, '0')} #{timestamp.hour.to_s.rjust(2, '0')}:#{timestamp.min.to_s.rjust(2, '0')}:#{timestamp.sec.to_s.rjust(2, '0')}\", \"#{name}\">"
end

#undoObject



83
84
85
86
# File 'lib/maglev_record/migration/migration.rb', line 83

def undo
  down if done?
  @done = false
end