9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'app/controllers/schema_designer/schema_controller.rb', line 9
def save
new_schema = JSON.parse params[:tables]
old_schema = parse_schema
@change = []
@up = []
@down = []
new_schema.each do |table|
id = table["id"]
corresponding_table = old_schema.find(&by_id(id))
if corresponding_table.nil?
new_table(table)
else
old_ids = corresponding_table[:columns].map{|t| t[:id]}
table["columns"].each do |col|
if old_ids.include? col["id"]
unless (old_col = corresponding_table[:columns].find(&by_id(col["id"]))) == col.symbolize_keys
column_change(table, old_col, col)
end
old_ids.delete col["id"]
else
new_column(table, col)
end
end
old_ids.each do |id|
delete_column table, corresponding_table[:columns].find(&by_id(id))
end
end
end
migration = "class #{params[:name].camelize} < ActiveRecord::Migration\n"
migration << "\n def up\n #{@up.join("\n ")}\n end\n" if @up.length > 0
migration << "\n def down\n #{@down.join("\n ")}\n end\n" if @down.length > 0
migration << "\n def change\n #{@change.join("\n ")}\n end\n" if @change.length > 0
migration << "\nend"
File.write(File.join(Rails.root, 'db', 'migrate', "#{Time.now.to_i}_#{params[:name].underscore}.rb"), migration, mode: 'w')
head :ok
end
|