Raphael Bauduin | 6 Nov 16:36

extract fixtures from database?

Hi,

I wondered if there was a way to generate fixtures from an existing database.

Thanks

raph
Jeremy Kemper | 7 Nov 01:31
Gravatar

Re: extract fixtures from database?


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
> raph

Sure, 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
(Continue reading)

Raphael Bauduin | 8 Nov 20:16

Re: extract fixtures from database?

> # 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@...
> http://lists.rubyonrails.org/mailman/listinfo/rails
(Continue reading)

Jeremy Kemper | 8 Nov 20:58
Gravatar

Re: extract fixtures from database?


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
Raphael Bauduin | 8 Nov 21:22

Re: extract fixtures from database?

On 11/8/05, Jeremy Kemper <jeremy@...> 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_sql

Thanks. Time for me to upgrade my postgres to  8.1 :-)
(Continue reading)

Peter Michaux | 8 Nov 21:37

Re: extract fixtures from database?

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?

Thanks,
Peter

_______________________________________________
Rails mailing list
Rails@...
http://lists.rubyonrails.org/mailman/listinfo/rails
Jeremy Kemper | 8 Nov 21:55
Gravatar

Re: extract fixtures from database?


On Nov 8, 2005, at 12:37 PM, Peter Michaux wrote:
> On 11/8/05, Jeremy Kemper <jeremy@...> 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

Gmane