úterý 22. ledna 2008

ar_fixture: Database to YAML

We faced problem with storing data from our application to fixtures that we want use to testing.
I found perfect plugin ar_fixtures, but installation process described on official page was broken. Maybe it could be caused by our proxy etc. So better solution is to install this plugin from gems.

This plugin allows download all data in one table to fixture (yaml file with "serialization" of each item).

We need some updates:
1.) select records that fulfil specific conditions
we change original method to_fixture

def to_fixture(limit=nil)
opts = {}
opts[:limit] = limit if limit

by new

def to_fixture(opts = {}, limit=nil)
opts[:limit] = limit if limit

and now, you can use e.g. Amount.to_fixture({:conditions => "value > 500"}) for saving amounts greater than 500 to YAML file

2.) remove bad diacritic from data (see tommorow's post)

3.) export all data
I created this simple method that loads all table names and call to_fixture method over all entity classes

def export_all
tables = ActiveRecord::Base.find_by_sql "select TABLE_NAME as name from INFORMATION_SCHEMA.TABLES"

tables.each do |t|
className = t.name.classify
puts "#{className} done"
puts "#{t.name} is not Entity class" # any relation

Žádné komentáře: