Add the manage_fixtures plugin for easy database migration

This commit is contained in:
Jacques Distler 2007-09-20 00:36:07 -05:00
parent c54a78c026
commit e8769c0b83
8 changed files with 294 additions and 0 deletions

View file

@ -0,0 +1,15 @@
-----
2.1.0
- Added table import functionality
- Changed default db:fixtures:import_all to use tables instead of models
- db:fixtures:import_for_tables TABLES=foos,bars
2.0.0
- Added two new export tasks
- db:fixtures:import_all
- db:fixtures:import_for_models MODELS=Foo,Bar
1.0.0
- imported original code by Chris McGrath [octopod]
- added namespace support for the tasks [nshb]

View file

@ -0,0 +1,21 @@
Copyright (c) 2006 Chris McGrath
The MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

68
vendor/plugins/manage_fixtures/README vendored Normal file
View file

@ -0,0 +1,68 @@
= Description
This plugin is a super lightweight tool used to manage all your fixtures, whether it is exporting or importing them.
So if you want to export all your data from your production server into your development environment, this will simplify the process without having to load up your database manager.
Similarly, if you want to import a selected set of Models, you can do that using the appropriate tasks.
= INSTALLATION
[%] script/plugin discover
[%] script/plugin install manage_fixtures
= USAGE
Create YAML test fixtures from data in an existing database. Defaults to development database. Set RAILS_ENV to override.
[%] rake db:fixtures:export_all
Create YAML test fixtures for a specific table(s) from data in an existing database. Defaults to development database. Set RAILS_ENV to override.
[%] rake db:fixtures:export_for_tables TABLES=foos[,bars]
Create YAML text fixtures based on a specific SQL query
[%] rake db:fixtures:export_using_query SQL="select * from foo where id='bar'" FIXTURE_NAME=foo
Import the YAML test fixtures for specific models from data in an existing database. Defaults to development database. Set RAILS_ENV to override.
[%] rake db:fixtures:import_for_models MODELS=Foo[,Bar,Land]
Import all YAML test fixtures for all of the tables from data in an existing database. Defaults to development database. Set RAILS_ENV to override.
[%] rake db:fixtures:import_all
Import all YAML test fixtures for all of the tables from data in an existing database. Defaults to development database. Set RAILS_ENV to override.
[%] rake db:fixtures:import_for_tables TABLES=foos[,bars,land]
= AUTHORS
Nathaniel Brown - nshb@inimit.com
Chris McGrath
= BUGS
Please report any bugs or feature enhancements to http://dev.toolbocks.com
= LICENSE
Copyright (c) 2006 Nathaniel Brown
Copyright (c) 2006 Chris McGrath
This is the MIT license, the license Ruby on Rails itself is licensed
under.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the
following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View file

@ -0,0 +1,13 @@
<html><head><title>Revision 427: /plugins/manage_fixtures</title></head>
<body>
<h2>Revision 427: /plugins/manage_fixtures</h2>
<ul>
<li><a href="../">..</a></li>
<li><a href="CHANGELOG">CHANGELOG</a></li>
<li><a href="MIT-LICENSE">MIT-LICENSE</a></li>
<li><a href="README">README</a></li>
<li><a href="lib/">lib/</a></li>
<li><a href="tasks/">tasks/</a></li>
</ul>
<hr noshade><em>Powered by <a href="http://subversion.tigris.org/">Subversion</a> version 1.4.4 (r25188).</em>
</body></html>

View 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>

View 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

View file

@ -0,0 +1,83 @@
require File.join(File.dirname(__FILE__), '..', 'lib', 'manage_fixtures.rb')
desc "use rake db:fixtures:export_using_query SQL=\"select * from foo where id='bar'\" FIXTURE_NAME=foo"
namespace :db do
namespace :fixtures do
task :export_using_query => :environment do
write_yaml_fixtures_to_file(ENV['SQL'], ENV['FIXTURE_NAME'])
end
end
end
desc 'use rake db:fixtures:export_for_tables TABLES=foos[,bars,lands] Create YAML test fixtures for a specific table(s) from data in an existing database. Defaults to development database. Set RAILS_ENV to override. '
namespace :db do
namespace :fixtures do
task :export_for_tables => :environment do
sql = "SELECT * FROM %s"
tables = ENV['TABLES']
ActiveRecord::Base.establish_connection
tables.each do |table_name|
write_yaml_fixtures_to_file(sql % table_name, table_name)
end
end
end
end
desc ' Create YAML test fixtures from data in an existing database. Defaults to development database. Set RAILS_ENV to override. '
namespace :db do
namespace :fixtures do
task :export_all => :environment do
sql = "SELECT * FROM %s"
skip_tables = ["schema_info"]
ActiveRecord::Base.establish_connection
(ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
i = "000"
File.open("#{RAILS_ROOT}/test/fixtures/#{table_name}.yml", 'w' ) do |file|
write_yaml_fixtures_to_file(sql % table_name, table_name)
end
end
end
end
end
desc 'use rake db:fixtures:import_for_models MODELS=Foo[,Bar,Land] to import the YAML test fixtures for a specific models from data in an existing database. Defaults to development database. Set RAILS_ENV to override. '
namespace :db do
namespace :fixtures do
task :import_for_models => :environment do
models = ENV['MODELS']
ActiveRecord::Base.establish_connection
models.each do |model_name|
import_model_fixture(model_name)
end
end
end
end
desc 'use rake db:fixtures:import_for_tables TABLES=foos[,bars,lands] to import the YAML test fixtures for a specific tables from data in an existing database. Defaults to development database. Set RAILS_ENV to override. '
namespace :db do
namespace :fixtures do
task :import_for_tables => :environment do
tables = ENV['TABLES']
ActiveRecord::Base.establish_connection
tables.each do |table_name|
import_table_fixture(table_name)
end
end
end
end
desc 'use rake db:fixtures:import_all to import all YAML test fixtures for all of the tables from data in an existing database. Defaults to development database. Set RAILS_ENV to override. '
namespace :db do
namespace :fixtures do
task :import_all => :environment do
ActiveRecord::Base.establish_connection
Dir.glob(File.join(RAILS_ROOT,'test','fixtures',"*.yml")).each do |f|
table_name = f.gsub(File.join(RAILS_ROOT,'test','fixtures', ''), '').gsub('.yml', '')
import_table_fixture(table_name)
end
end
end
end

View file

@ -0,0 +1,9 @@
<html><head><title>Revision 427: /plugins/manage_fixtures/tasks</title></head>
<body>
<h2>Revision 427: /plugins/manage_fixtures/tasks</h2>
<ul>
<li><a href="../">..</a></li>
<li><a href="fixtures.rake">fixtures.rake</a></li>
</ul>
<hr noshade><em>Powered by <a href="http://subversion.tigris.org/">Subversion</a> version 1.4.4 (r25188).</em>
</body></html>