Friday, May 26, 2006

Dump db to fixtures (generate fixtures from db)

This is a quite interesting thread on rails ml with the solution to my problem.
I wanted to generate fixtures automatically from my db. This is a rake action that does the job well. Just put it into lib/tasks/ and call it dump_fixtures.rake

desc 'Dump a database to yaml fixtures. Set environment variables DB
and DEST to specify the target database and destination path for the
fixtures. DB defaults to development and DEST defaults to RAILS_ROOT/
test/fixtures.'

task :dump_fixtures => :environment do
    path = ENV['DEST'] || "#{RAILS_ROOT}/test/fixtures"
    db   = ENV['DB']   || 'development'
    sql  = 'SELECT * FROM %s'

    ActiveRecord::Base.establish_connection(db)
    ActiveRecord::Base.connection.select_values('show tables').each do |table_name|
        i = '000'
        File.open("#{path}/#{table_name}.yml", 'wb') do |file|
            file.write ActiveRecord::Base.connection.select_all(sql %
table_name).inject({}) { |hash, record|
                hash["#{table_name}_#{i.succ!}"] = record
                hash
            }.to_yaml
        end
    end
end
# ActiveRecord::Base.connection.select_values('show tables')
# is mysql specific
# SQLite:  ActiveRecord::Base.connection.select_values('.table')
# Postgres
# table_names = ActiveRecord::Base.connection.select_values(<<-end_sql)
#    SELECT c.relname
#    FROM pg_class c
#      LEFT JOIN pg_roles r     ON r.oid = c.relowner
#      LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
#    WHERE c.relkind IN ('r','')
#      AND n.nspname IN ('myappschema', 'public')
#      AND pg_table_is_visible(c.oid)
# end_sql

No comments: