Hi, I wondered if there was a way to generate fixtures from an existing database. Thanks raph
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Nov 6, 2005, at 7:39 AM, Raphael Bauduin wrote:> Hi, > I wondered if there was a way to generate fixtures from an existing > database. > Thanks > raphSure, raph: $ cat > lib/tasks/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.table_names.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 # dump dev db to test/fixtures $ rake dump_fixtures # dump foobar db to ./foobar $ rake dump_fixtures DB=foobar DEST=./foobar jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (Darwin) iD8DBQFDbqBvAQHALep9HFYRAiZOAKDP8HwjsB94JRwOsSOFez2PPX07VwCgrpfV HbfLTIlTjHSZndxdPYseoz4=YHL9 -----END PGP SIGNATURE-----
> # dump foobar db to ./foobar > $ rake dump_fixtures DB=foobar DEST=./foobar >I get this error: rake aborted! undefined method `table_names'' for #<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:0xb75e3cf4> I don''t find the table_names method anywhere in the api. I looked for such a method in myslq but didn''t either. Am I missing something obvious? thanks. raph> > jeremy > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.2 (Darwin) > > iD8DBQFDbqBvAQHALep9HFYRAiZOAKDP8HwjsB94JRwOsSOFez2PPX07VwCgrpfV > HbfLTIlTjHSZndxdPYseoz4> =YHL9 > -----END PGP SIGNATURE----- > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Nov 8, 2005, at 11:16 AM, Raphael Bauduin wrote:>> # dump foobar db to ./foobar >> $ rake dump_fixtures DB=foobar DEST=./foobar > I get this error: > > rake aborted! > undefined method `table_names'' for > #<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:0xb75e3cf4> > > I don''t find the table_names method anywhere in the api. I looked for > such a method in myslq but didn''t either. Am I missing something > obvious?Oops! That''s a method I defined. For PostgreSQL: 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 jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (Darwin) iD8DBQFDcQNwAQHALep9HFYRAptzAKC3mho6d1635McnAR/DKjV5phuzGACfaJxn XZEDeAT4eZxbkZsQs75hQHQ=Ra0A -----END PGP SIGNATURE-----
On 11/8/05, Jeremy Kemper <jeremy-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org> wrote:> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > On Nov 8, 2005, at 11:16 AM, Raphael Bauduin wrote: > >> # dump foobar db to ./foobar > >> $ rake dump_fixtures DB=foobar DEST=./foobar > > I get this error: > > > > rake aborted! > > undefined method `table_names'' for > > #<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:0xb75e3cf4> > > > > I don''t find the table_names method anywhere in the api. I looked for > > such a method in myslq but didn''t either. Am I missing something > > obvious? > > Oops! That''s a method I defined. For PostgreSQL: > > 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_sqlThanks. Time for me to upgrade my postgres to 8.1 :-) Raph> > > jeremy > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.2 (Darwin) > > iD8DBQFDcQNwAQHALep9HFYRAptzAKC3mho6d1635McnAR/DKjV5phuzGACfaJxn > XZEDeAT4eZxbkZsQs75hQHQ> =Ra0A > -----END PGP SIGNATURE----- > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 11/8/05, Jeremy Kemper <jeremy-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org> wrote:> > Oops! That''s a method I defined. For PostgreSQL: > > table_namesKnow any way to do this same thing for mysql? Thanks, Peter _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Nov 8, 2005, at 12:37 PM, Peter Michaux wrote:> On 11/8/05, Jeremy Kemper <jeremy-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org> wrote: > Oops! That''s a method I defined. For PostgreSQL: > > table_names > > Know any way to do this same thing for mysql?MySQL: ActiveRecord::Base.connection.select_values(''show tables'') SQLite: ActiveRecord::Base.connection.select_values(''.table'') jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (Darwin) iD8DBQFDcRC/AQHALep9HFYRAiy5AJ91B9x3Y5yBgIU7NdBw5JDjvDqbWQCg0+7S 7IDsUTqqJal4oPy+ziGPIpk=Fkw5 -----END PGP SIGNATURE-----