James Britt
2005-Sep-18 17:46 UTC
Automagically creating fixtures from current database contents?
I''m accumulating numerous test scripts, and finding that I often want to work with more prepopulated data. Rather than create fixtures by hand, I''d rather be able to take the current state of the database and serialize it to a set of fixture files that I can reload at a later date for more testing. I''ve been hacking together assorted one-offs that dump to the browser, as YAML text, the current set of values for any specific object, but I would rather just have a single ''save_current_database_as_fixtures'' task. Before I reinvent the wheel, does such a beast exist? Thanks, James Britt -- http://www.ruby-doc.org - The Ruby Documentation Site http://www.rubyxml.com - News, Articles, and Listings for Ruby & XML http://www.rubystuff.com - The Ruby Store for Ruby Stuff http://www.jamesbritt.com - Playing with Better Toys
Joe Van Dyk
2005-Sep-18 18:31 UTC
Re: Automagically creating fixtures from current database contents?
On 9/18/05, James Britt <james.britt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m accumulating numerous test scripts, and finding that I often want to > work with more prepopulated data. > > Rather than create fixtures by hand, I''d rather be able to take the > current state of the database and serialize it to a set of fixture files > that I can reload at a later date for more testing. > > I''ve been hacking together assorted one-offs that dump to the browser, > as YAML text, the current set of values for any specific object, but I > would rather just have a single ''save_current_database_as_fixtures'' task. > > Before I reinvent the wheel, does such a beast exist?I''m currently doing the opposite. I populate the development database from my test fixtures. Here''s the rake task for what I''m doing: desc "Load fixtures data into the development database" task :load_fixtures_data_to_development do require ''active_record/fixtures'' RAILS_ENV = ''development'' require File.dirname(__FILE__) + ''/config/environment'' ActiveRecord::Base.establish_connection() Fixtures.create_fixtures("test/fixtures", %w(amenities amenities_houses communities communities_contractors contacts contents contractors houses images users)) end (there''s probably a better way to specify what fixtures should be loaded though) But it works well for me. I also have a migration that sets up the tables in the database. So, when I want to start up a new development env, I just check out the svn, create the databases, then run ''rake migrate'' and ''rake load_fixtures_data_to_development''.
Nicholas Seckar
2005-Sep-18 20:03 UTC
Re: Automagically creating fixtures from current database contents?
James Britt wrote:> I''ve been hacking together assorted one-offs that dump to the browser, > as YAML text, the current set of values for any specific object, but I > would rather just have a single ''save_current_database_as_fixtures'' task. > > Before I reinvent the wheel, does such a beast exist?def yaml_for_class(cls) records = cls.find(:all) underscored = cls.name.underscore records.inject({}) do |hash, record| key = "#{underscored}_#{hash.length + 1}" hash[key] = record.attributes hash end.to_yaml end It would be nice if the order of the fields could be specified, but whatever.
James Britt
2005-Sep-18 21:22 UTC
Re: Automagically creating fixtures from current database contents?
Joe Van Dyk wrote:> On 9/18/05, James Britt <james.britt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >>I''m accumulating numerous test scripts, and finding that I often want to >>work with more prepopulated data. >> >>Rather than create fixtures by hand, I''d rather be able to take the >>current state of the database and serialize it to a set of fixture files >>that I can reload at a later date for more testing. >> >>I''ve been hacking together assorted one-offs that dump to the browser, >>as YAML text, the current set of values for any specific object, but I >>would rather just have a single ''save_current_database_as_fixtures'' task. >> >>Before I reinvent the wheel, does such a beast exist? > > > I''m currently doing the opposite. I populate the development database > from my test fixtures. >Which is what I do, too, and why I want to be able to grab a snapshot of the database at any time and save it off as a fixture set. It will allow me to create repeatable test scenarios without my having to hand-edit the fixture YAML (an error-prone process). James -- http://www.ruby-doc.org - The Ruby Documentation Site http://www.rubyxml.com - News, Articles, and Listings for Ruby & XML http://www.rubystuff.com - The Ruby Store for Ruby Stuff http://www.jamesbritt.com - Playing with Better Toys
James Britt
2005-Sep-18 21:27 UTC
Re: Automagically creating fixtures from current database contents?
Nicholas Seckar wrote:> James Britt wrote: > >> I''ve been hacking together assorted one-offs that dump to the browser, >> as YAML text, the current set of values for any specific object, but I >> would rather just have a single ''save_current_database_as_fixtures'' task. >> >> Before I reinvent the wheel, does such a beast exist? > > > def yaml_for_class(cls) > records = cls.find(:all) > underscored = cls.name.underscore > records.inject({}) do |hash, record| > key = "#{underscored}_#{hash.length + 1}" > hash[key] = record.attributes > hash > end.to_yaml > end > > It would be nice if the order of the fields could be specified, but > whatever.Order is probably not a big deal. I was thinking of writing a module so that I could mix-in a class method for all my models, then write a master script to loop over the tables in the database and invoke some to_fixture method on each one. Thanks, James -- http://www.ruby-doc.org - The Ruby Documentation Site http://www.rubyxml.com - News, Articles, and Listings for Ruby & XML http://www.rubystuff.com - The Ruby Store for Ruby Stuff http://www.jamesbritt.com - Playing with Better Toys
James Britt
2005-Sep-20 17:28 UTC
Re: Automagically creating fixtures from current database contents?
Nicholas Seckar wrote:> James Britt wrote: > >> I''ve been hacking together assorted one-offs that dump to the browser, >> as YAML text, the current set of values for any specific object, but I >> would rather just have a single ''save_current_database_as_fixtures'' task. >> >> Before I reinvent the wheel, does such a beast exist? > > > def yaml_for_class(cls) > records = cls.find(:all) > underscored = cls.name.underscore > records.inject({}) do |hash, record| > key = "#{underscored}_#{hash.length + 1}" > hash[key] = record.attributes > hash > end.to_yaml > end > > It would be nice if the order of the fields could be specified, but > whatever.Thanks for the code example. It''s a bit cleaner than what I was doing. I''ve taken a stab at a Rake task for creating fixture files from a database, which can be seen at: http://www.jamesbritt.com/index.rb/Development@Rails_Fixtures_from_Database_Contents.txt James Britt -- http://www.ruby-doc.org - The Ruby Documentation Site http://www.rubyxml.com - News, Articles, and Listings for Ruby & XML http://www.rubystuff.com - The Ruby Store for Ruby Stuff http://www.jamesbritt.com - Playing with Better Toys