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.
C3e347393bfc91e02e52656bbea76140?d=identicon&s=25 magic_hat (Guest)
on 2009-05-16 18: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.
Bee69cfed999cd13e3bff73d472a39ee?d=identicon&s=25 Hassan Schroeder (Guest)
on 2009-05-16 19:22
(Received via mailing list)
On Sat, May 16, 2009 at 9:11 AM, magic_hat <magic_hat60622@yahoo.com>
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 Schroeder ------------------------ hassan.schroeder@gmail.com
Bce1d1b7c3ec7b577dcb42e254899e6b?d=identicon&s=25 Michael Schuerig (Guest)
on 2009-05-16 22: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 Schuerig
mailto:michael@schuerig.de
http://www.schuerig.de/michael/
C3e347393bfc91e02e52656bbea76140?d=identicon&s=25 magic_hat (Guest)
on 2009-05-17 04: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 Schroeder <hassan.schroe...@gmail.com>
C3e347393bfc91e02e52656bbea76140?d=identicon&s=25 magic_hat (Guest)
on 2009-05-17 04: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.
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-05-17 09: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 <magic_hat60622@yahoo.com>
72ea925c0ca3d19fdd2f12fa76681624?d=identicon&s=25 Stephan Wehner (stephanwehner)
on 2009-05-17 16: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.
Bee69cfed999cd13e3bff73d472a39ee?d=identicon&s=25 Hassan Schroeder (Guest)
on 2009-05-17 16:40
(Received via mailing list)
On Sat, May 16, 2009 at 7:29 PM, magic_hat <magic_hat60622@yahoo.com>
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 Schroeder ------------------------ hassan.schroeder@gmail.com
3a58bc7aefaa7a7b07d6311396a35e05?d=identicon&s=25 Bjørn Arild Mæland (Guest)
on 2009-05-17 17: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.