From e8769c0b838328ef7240e143238e62b739d38fa3 Mon Sep 17 00:00:00 2001 From: Jacques Distler Date: Thu, 20 Sep 2007 00:36:07 -0500 Subject: [PATCH] Add the manage_fixtures plugin for easy database migration --- vendor/plugins/manage_fixtures/CHANGELOG | 15 ++++ vendor/plugins/manage_fixtures/MIT-LICENSE | 21 +++++ vendor/plugins/manage_fixtures/README | 68 +++++++++++++++ vendor/plugins/manage_fixtures/index.html | 13 +++ vendor/plugins/manage_fixtures/lib/index.html | 9 ++ .../manage_fixtures/lib/manage_fixtures.rb | 76 +++++++++++++++++ .../manage_fixtures/tasks/fixtures.rake | 83 +++++++++++++++++++ .../plugins/manage_fixtures/tasks/index.html | 9 ++ 8 files changed, 294 insertions(+) create mode 100644 vendor/plugins/manage_fixtures/CHANGELOG create mode 100644 vendor/plugins/manage_fixtures/MIT-LICENSE create mode 100644 vendor/plugins/manage_fixtures/README create mode 100644 vendor/plugins/manage_fixtures/index.html create mode 100644 vendor/plugins/manage_fixtures/lib/index.html create mode 100644 vendor/plugins/manage_fixtures/lib/manage_fixtures.rb create mode 100644 vendor/plugins/manage_fixtures/tasks/fixtures.rake create mode 100644 vendor/plugins/manage_fixtures/tasks/index.html diff --git a/vendor/plugins/manage_fixtures/CHANGELOG b/vendor/plugins/manage_fixtures/CHANGELOG new file mode 100644 index 00000000..6f4d2881 --- /dev/null +++ b/vendor/plugins/manage_fixtures/CHANGELOG @@ -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] diff --git a/vendor/plugins/manage_fixtures/MIT-LICENSE b/vendor/plugins/manage_fixtures/MIT-LICENSE new file mode 100644 index 00000000..24691313 --- /dev/null +++ b/vendor/plugins/manage_fixtures/MIT-LICENSE @@ -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. \ No newline at end of file diff --git a/vendor/plugins/manage_fixtures/README b/vendor/plugins/manage_fixtures/README new file mode 100644 index 00000000..7f0068e7 --- /dev/null +++ b/vendor/plugins/manage_fixtures/README @@ -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. diff --git a/vendor/plugins/manage_fixtures/index.html b/vendor/plugins/manage_fixtures/index.html new file mode 100644 index 00000000..fe03bde2 --- /dev/null +++ b/vendor/plugins/manage_fixtures/index.html @@ -0,0 +1,13 @@ +Revision 427: /plugins/manage_fixtures + +

Revision 427: /plugins/manage_fixtures

+ +
Powered by Subversion version 1.4.4 (r25188). + \ No newline at end of file diff --git a/vendor/plugins/manage_fixtures/lib/index.html b/vendor/plugins/manage_fixtures/lib/index.html new file mode 100644 index 00000000..6f0bdecf --- /dev/null +++ b/vendor/plugins/manage_fixtures/lib/index.html @@ -0,0 +1,9 @@ +Revision 427: /plugins/manage_fixtures/lib + +

Revision 427: /plugins/manage_fixtures/lib

+ +
Powered by Subversion version 1.4.4 (r25188). + \ No newline at end of file diff --git a/vendor/plugins/manage_fixtures/lib/manage_fixtures.rb b/vendor/plugins/manage_fixtures/lib/manage_fixtures.rb new file mode 100644 index 00000000..6db66372 --- /dev/null +++ b/vendor/plugins/manage_fixtures/lib/manage_fixtures.rb @@ -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 diff --git a/vendor/plugins/manage_fixtures/tasks/fixtures.rake b/vendor/plugins/manage_fixtures/tasks/fixtures.rake new file mode 100644 index 00000000..6a6438fd --- /dev/null +++ b/vendor/plugins/manage_fixtures/tasks/fixtures.rake @@ -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 + diff --git a/vendor/plugins/manage_fixtures/tasks/index.html b/vendor/plugins/manage_fixtures/tasks/index.html new file mode 100644 index 00000000..ad4524c2 --- /dev/null +++ b/vendor/plugins/manage_fixtures/tasks/index.html @@ -0,0 +1,9 @@ +Revision 427: /plugins/manage_fixtures/tasks + +

Revision 427: /plugins/manage_fixtures/tasks

+ +
Powered by Subversion version 1.4.4 (r25188). + \ No newline at end of file