rails-console.md#

rails console
>> ActiveRecord::Base.establish_connection
>> ActiveRecord::Base.connection.tables

# [schema_info] is an internal table to keep track of migration versions
# Query each table, convert each row to yaml

>> ActiveRecord::Base.connection.select_all("select * from people")

>> puts ActiveRecord::Base.connection.select_all("select * from people").map do |row|
>>    row.to_yaml 
>> end

Create task to extract fixtures#

# lib/tasks/extract_fixtures.rake
desc 'Create YAML test fixtures from existing DB 
defaults to development DB, set RAILS_ENV to override'

task :extract_fixtures => :environment do
    sql = "SELECT * FROM %s"
    skips_tables = ["schema_info]"
    ActiveRecord::Base.establish_connection
    (ActiveRecord::Base.tables - skip_tables).each do |table_name|
        i = "000"
        File.open("#{RAILS_ROOT}/test/fixtures/#{table_name}.yml", "w") do |file|
            data = ActiveRecord::Base.connection.select_all(sql % table_name)
            file.write data.inject({}) {|hash, record|}
                hash["#{table_name}_#{i.succ!}"] = record
                hash
            }.to_yml
        end
    end
end