Add the manage_fixtures plugin for easy database migration
This commit is contained in:
parent
c54a78c026
commit
e8769c0b83
8 changed files with 294 additions and 0 deletions
9
vendor/plugins/manage_fixtures/lib/index.html
vendored
Normal file
9
vendor/plugins/manage_fixtures/lib/index.html
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
<html><head><title>Revision 427: /plugins/manage_fixtures/lib</title></head>
|
||||
<body>
|
||||
<h2>Revision 427: /plugins/manage_fixtures/lib</h2>
|
||||
<ul>
|
||||
<li><a href="../">..</a></li>
|
||||
<li><a href="manage_fixtures.rb">manage_fixtures.rb</a></li>
|
||||
</ul>
|
||||
<hr noshade><em>Powered by <a href="http://subversion.tigris.org/">Subversion</a> version 1.4.4 (r25188).</em>
|
||||
</body></html>
|
76
vendor/plugins/manage_fixtures/lib/manage_fixtures.rb
vendored
Normal file
76
vendor/plugins/manage_fixtures/lib/manage_fixtures.rb
vendored
Normal file
|
@ -0,0 +1,76 @@
|
|||
def write_yaml_fixtures_to_file(sql, fixture_name)
|
||||
i = "000"
|
||||
File.open("#{RAILS_ROOT}/test/fixtures/#{fixture_name}.yml", 'w' ) do |file|
|
||||
data = ActiveRecord::Base.connection.select_all(sql)
|
||||
file.write data.inject({}) { |hash, record|
|
||||
hash["#{fixture_name}_#{i.succ!}"] = record
|
||||
hash
|
||||
}.to_yaml
|
||||
end
|
||||
end
|
||||
|
||||
def import_table_fixture(table)
|
||||
filename = File.join(RAILS_ROOT,'test','fixtures',table + '.yml')
|
||||
success = Hash.new
|
||||
records = YAML::load( File.open(filename))
|
||||
|
||||
records.sort.each do |r|
|
||||
row = r[1]
|
||||
columns = []
|
||||
values = []
|
||||
|
||||
row.each_pair do |column, value|
|
||||
if column.to_sym
|
||||
columns << ActiveRecord::Base.connection.quote_column_name(column)
|
||||
values << ActiveRecord::Base.connection.quote(value)
|
||||
else
|
||||
p "Column not found" + column.to_s
|
||||
end
|
||||
end
|
||||
|
||||
insert_sql = "INSERT INTO #{table} (" + columns.join(', ') + ") VALUES (" + values.join(', ') + ")"
|
||||
|
||||
begin
|
||||
if ActiveRecord::Base.connection.execute(insert_sql)
|
||||
success[table.to_sym] = (success[table.to_sym] ? success[table.to_sym] + 1 : 1)
|
||||
end
|
||||
rescue
|
||||
p "#{table} failed to import: " + insert_sql
|
||||
end
|
||||
end
|
||||
|
||||
p "Total of #{success[table.to_sym]} #{table} records imported successfully"
|
||||
end
|
||||
|
||||
def import_model_fixture(model)
|
||||
filename = File.join(RAILS_ROOT,'test','fixtures',model.tableize + '.yml')
|
||||
success = Hash.new
|
||||
records = YAML::load( File.open(filename))
|
||||
@model = Class.const_get(model)
|
||||
@model.transaction do
|
||||
records.sort.each do |r|
|
||||
row = r[1]
|
||||
@new_model = @model.new
|
||||
|
||||
row.each_pair do |column, value|
|
||||
if column.to_sym
|
||||
@new_model.send(column + '=', value)
|
||||
else
|
||||
p "Column not found" + column.to_s
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
begin
|
||||
if @new_model.save
|
||||
success[model.to_sym] = (success[model.to_sym] ? success[model.to_sym] + 1 : 1)
|
||||
end
|
||||
rescue
|
||||
p "#{@new_model.class.to_s} failed to import: " + r.inspect
|
||||
p @new_model.errors.inspect
|
||||
end
|
||||
end
|
||||
|
||||
p "Total of #{success[model.to_sym]} #{@new_model.class.to_s} records imported successfully"
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue