annotate lib/tasks/extract_fixtures.rake @ 159:4e485928a26b cannam-pre-20110113-merge

* Simplify table construction, with name and description in the same cell and some layout tidying. Eliminates minor bug that made name and description highlight separately on mouseover.
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Fri, 28 Jan 2011 11:01:43 +0000
parents 513646585e45
children cbce1fd3b1b7
rev   line source
Chris@0 1 desc 'Create YAML test fixtures from data in an existing database.
Chris@0 2 Defaults to development database. Set RAILS_ENV to override.'
Chris@0 3
Chris@0 4 task :extract_fixtures => :environment do
Chris@0 5 sql = "SELECT * FROM %s"
Chris@0 6 skip_tables = ["schema_info"]
Chris@0 7 ActiveRecord::Base.establish_connection
Chris@0 8 (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
Chris@0 9 i = "000"
Chris@0 10 File.open("#{RAILS_ROOT}/#{table_name}.yml", 'w' ) do |file|
Chris@0 11 data = ActiveRecord::Base.connection.select_all(sql % table_name)
Chris@0 12 file.write data.inject({}) { |hash, record|
Chris@0 13
Chris@0 14 # cast extracted values
Chris@0 15 ActiveRecord::Base.connection.columns(table_name).each { |col|
Chris@0 16 record[col.name] = col.type_cast(record[col.name]) if record[col.name]
Chris@0 17 }
Chris@0 18
Chris@0 19 hash["#{table_name}_#{i.succ!}"] = record
Chris@0 20 hash
Chris@0 21 }.to_yaml
Chris@0 22 end
Chris@0 23 end
Chris@0 24 end