Forum: Ruby on Rails mysql to postgres rake task

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
magic_hat (Guest)
on 2009-05-16 20:12
(Received via mailing list)
Hey all. I'm trying to export my app's data from mysql to postgres in
advance of a database switch.

I've tried this:

  task(:export => :environment) do
    @users=User.find(:all)
    ActiveRecord::Base.establish_connection(:pg)
    for user in @users
      user.save_with_validation(perform_validation = false)
    end
    ActiveRecord::Base.establish_connection(:development)
  end

However, nothing's happening -- no activity in the pg environment log,
no records added.

I've also tried exporting as yaml and then importing, using Tobias
Lutke's approach (http://blog.leetsoft.com/files/code/backup.rake)

However, the import bombs here:
        myFile=YAML.load_file("#{tbl}.yml")
 because of some unexpected quote marks in some of the content.

Any help getting either of these approaches to work would be hugely
appreciated.
Hassan S. (Guest)
on 2009-05-16 21:22
(Received via mailing list)
On Sat, May 16, 2009 at 9:11 AM, magic_hat 
<removed_email_address@domain.invalid>
wrote:
>
> Hey all. I'm trying to export my app's data from mysql to postgres in
> advance of a database switch.

Check out the yaml_db plugin:
  <http://agilewebdevelopment.com/plugins/yamldb>

FWIW,
--
Hassan S. ------------------------ removed_email_address@domain.invalid
Michael S. (Guest)
on 2009-05-17 00:19
(Received via mailing list)
On Saturday 16 May 2009, magic_hat wrote:
> Hey all. I'm trying to export my app's data from mysql to postgres in
> advance of a database switch.

Try exporting and importing YAML first. If that doesn't work, you may
have better luck with mysqldump + mysql2pgsql[*] + psql.

Michael

[*] http://pgfoundry.org/projects/mysql2pgsql/

--
Michael S.
mailto:removed_email_address@domain.invalid
http://www.schuerig.de/michael/
magic_hat (Guest)
on 2009-05-17 06:30
(Received via mailing list)
Hassan,

the yaml_db plugin looked promising, but it's bombing because of
unbalanced quotes in some of the content, much like the code from
Tobias that I posted.

On May 16, 12:06 pm, Hassan S. <removed_email_address@domain.invalid>
magic_hat (Guest)
on 2009-05-17 06:31
(Received via mailing list)
Michael,

the perl script looks great, but I'm getting a segmentation fault
error when I run, perhaps because some of the content involves long
news articles.
Colin L. (Guest)
on 2009-05-17 11:42
(Received via mailing list)
If it just the unbalanced quotes that are causing the problem you could
possibly first convert all the quotes to some escape sequence then
transfer
the database, and then convert the escape sequences back again.

Colin

2009/5/17 magic_hat <removed_email_address@domain.invalid>
Stephan W. (Guest)
on 2009-05-17 18:27
magic_hat wrote:
> Hey all. I'm trying to export my app's data from mysql to postgres in
> advance of a database switch.
>
> I've tried this:
>
>   task(:export => :environment) do
>     @users=User.find(:all)
>     ActiveRecord::Base.establish_connection(:pg)
>     for user in @users
>       user.save_with_validation(perform_validation = false)
>     end
>     ActiveRecord::Base.establish_connection(:development)
>   end
>
> However, nothing's happening -- no activity in the pg environment log,
> no records added.

Did you look at this plugin?

http://agilewebdevelopment.com/plugins/manage_fixtures

  [%] rake db:fixtures:export_all RAILS_ENV=development
  [%] rake db:fixtures:load

When loading specify your new database, RAILS_ENV=new_development or
other.

Stephan

> I've also tried exporting as yaml and then importing, using Tobias
> Lutke's approach (http://blog.leetsoft.com/files/code/backup.rake)
>
> However, the import bombs here:
>         myFile=YAML.load_file("#{tbl}.yml")
>  because of some unexpected quote marks in some of the content.
>
> Any help getting either of these approaches to work would be hugely
> appreciated.
Hassan S. (Guest)
on 2009-05-17 18:40
(Received via mailing list)
On Sat, May 16, 2009 at 7:29 PM, magic_hat 
<removed_email_address@domain.invalid>
wrote:

> the yaml_db plugin looked promising, but it's bombing because of
> unbalanced quotes in some of the content

ouch. Can you post the stack trace and an example data row that's
causing the problem?

--
Hassan S. ------------------------ removed_email_address@domain.invalid
Bjørn Arild Mæland (Guest)
on 2009-05-17 19:32
(Received via mailing list)
>     end
>         myFile=YAML.load_file("#{tbl}.yml")
>  because of some unexpected quote marks in some of the content.
>
> Any help getting either of these approaches to work would be hugely
> appreciated.

Taps (http://adam.blog.heroku.com/past/2009/2/11/
taps_for_easy_database_transfers/) is an excellent solution for this.
Heroku use it for cross-db support. Note that it works over network
connections, which is very handy.
This topic is locked and can not be replied to.