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
65
66
67
68
69
70
71
72
|
# File 'lib/rmap/commands.rb', line 33
def self.migration(name, *columns, options)
if !Rmap.const_defined? :CONF_ROOT
raise "Could not find a conf.rmap.rb file in your path."
end
Gengin.new do
source_root File.expand_path("../generator_templates/", __FILE__)
destination_root "#{Rmap::CONF_ROOT}/migrations"
@name = name
@columns = columns
up_code_buf = []
down_code_buf = []
if name.match(/\Aadd_.*_to_(.*?)\Z/)
table_name = $1
@columns.each do |column|
(column_name, type) = column.split(/:/)
up_code_buf << " #{table_name}.add :#{column_name}, :#{type}"
down_code_buf << " #{table_name}.remove :#{column_name}"
end
end
if name.match(/\Aremove_.*_from_(.*?)\Z/)
table_name = $1
@columns.each do |column|
(column_name, type) = column.split(/:/)
up_code_buf << " #{table_name}.remove :#{column_name}"
down_code_buf << " #{table_name}.add :#{column_name}, :#{type}"
end
end
@up_code = up_code_buf.join("\n")
@down_code = down_code_buf.join("\n")
copy "migration.rmap.rb", "#{Time.new.to_i}_#{name.downcase.strip.gsub(/\W+/, "_")}.migration.rmap.rb", :erb => true
end
end
|