Ruby on Rails将数据库和数据从SQLite3迁移到Postgres

时间:2015-05-06 16:57:32

标签: ruby-on-rails sqlite postgresql ruby-on-rails-4 database-migration

我正在尝试将一个小的SQLite数据库迁移到PostgreSQL,以便我可以将它放在Heroku上。计划是将本地数据库迁移到PostgreSQL实例。一旦我知道工作正常,我就会将数据库迁移到heroku。

事实证明这是非常困难的。我不习惯使用Ruby on Rails,所以可能缺少所有技巧。

我在MAC上运行Ruby 2.2.0 / Rails 4.2.0

关于问题是什么/

我正在运行以下命令从SQLite3迁移到PostgreSQL。请注意Db:migrate工作并且数据库架构已就绪。我现在只需要将数据输入它。

taps pull postgres://user@localhost/db/development http://user:secret@localhost:5000

然后我得到以下输出:

Receiving schema Unable to fetch tables information from http://user:secret@localhost:5000. Please check the server log.

在我启动服务的另一个终端中,我得到以下内容,其中包含引用表的错误:

Rack::Utils::OkJson::Error - cannot encode Symbol: :schema_migrations::


$ **taps server sqlite://db/development.sqlite3 USER secret**
== Sinatra/1.0 has taken the stage on 5000 for production with backup from WEBrick
[2015-05-06 17:28:09] INFO  WEBrick 1.3.1
[2015-05-06 17:28:09] INFO  ruby 2.2.0 (2014-12-25) [x86_64-darwin14]
[2015-05-06 17:28:09] INFO  WEBrick::HTTPServer#start: pid=8274 port=5000
127.0.0.1 - USER [06/May/2015:17:28:57 +0100] "GET / HTTP/1.1" 200 - 0.0010
localhost - - [06/May/2015:17:28:57 BST] "GET / HTTP/1.1" 200 31
- -> /
127.0.0.1 - USER [06/May/2015:17:28:57 +0100] "POST /sessions HTTP/1.1" 200 - 0.0158
localhost - - [06/May/2015:17:28:57 BST] "POST /sessions HTTP/1.1" 200 46
- -> /sessions
Rack::Utils::OkJson::Error - cannot encode Symbol: :schema_migrations:
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/rack-1.6.0/lib/rack/utils/okjson.rb:427:in `valenc'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/rack-1.6.0/lib/rack/utils/okjson.rb:438:in `block in arrenc'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/rack-1.6.0/lib/rack/utils/okjson.rb:438:in `map'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/rack-1.6.0/lib/rack/utils/okjson.rb:438:in `arrenc'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/rack-1.6.0/lib/rack/utils/okjson.rb:410:in `encode'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/taps-0.3.24/lib/taps/server.rb:157:in `block in <class:Server>'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/sinatra-1.0/lib/sinatra/base.rb:865:in `call'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/sinatra-1.0/lib/sinatra/base.rb:865:in `block in route'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/sinatra-1.0/lib/sinatra/base.rb:521:in `instance_eval'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/sinatra-1.0/lib/sinatra/base.rb:521:in `route_eval'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/sinatra-1.0/lib/sinatra/base.rb:500:in `block (2 levels) in route!'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/sinatra-1.0/lib/sinatra/base.rb:497:in `catch'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/sinatra-1.0/lib/sinatra/base.rb:497:in `block in route!'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/sinatra-1.0/lib/sinatra/base.rb:476:in `each'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/sinatra-1.0/lib/sinatra/base.rb:476:in `route!'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/sinatra-1.0/lib/sinatra/base.rb:601:in `dispatch!'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/sinatra-1.0/lib/sinatra/base.rb:411:in `block in call!'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/sinatra-1.0/lib/sinatra/base.rb:566:in `instance_eval'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/sinatra-1.0/lib/sinatra/base.rb:566:in `block in invoke'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/sinatra-1.0/lib/sinatra/base.rb:566:in `catch'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/sinatra-1.0/lib/sinatra/base.rb:566:in `invoke'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/sinatra-1.0/lib/sinatra/base.rb:411:in `call!'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/sinatra-1.0/lib/sinatra/base.rb:399:in `call'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/rack-1.6.0/lib/rack/deflater.rb:35:in `call'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/rack-1.6.0/lib/rack/auth/basic.rb:25:in `call'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/rack-1.6.0/lib/rack/commonlogger.rb:33:in `call'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/sinatra-1.0/lib/sinatra/base.rb:979:in `block in call'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/sinatra-1.0/lib/sinatra/base.rb:1005:in `synchronize'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/sinatra-1.0/lib/sinatra/base.rb:979:in `call'
 /Users/USER/.rvm/gems/ruby-2.2.0/gems/rack-1.6.0/lib/rack/handler/webrick.rb:89:in `service'
 /Users/USER/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
 /Users/USER/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
 /Users/USER/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
ERROR: Rack::Utils::OkJson::Error: cannot encode Symbol: :schema_migrations
[2015-05-06 17:28:57] ERROR NoMethodError: undefined method `blank?' for {}:Hash
...........

localhost - - [06 / May / 2015:17:28:57 BST]“GET / sessions / 1568289369 / pull / table_names HTTP / 1.1”500 317 - - &gt; /会话/ 1568289369 /拉/ table_names

2 个答案:

答案 0 :(得分:1)

不要使用水龙头,请使用sequel命令:

sequel -C sqlite://db/development.sqlite3 postgres://pgusername:pguserpassword@localhost/development_db

仅适用于google =)

答案 1 :(得分:0)

确保在这种情况下使用正确版本的ruby 2.0.0,如果您还没有安装和使用RVM,我建议您使用它。

RVM use 2.0.0

打开两个终端窗口,确保两个终端窗口都指向ruby 2.0.0,因为打开新终端将恢复为默认版本的ruby,请通过

进行检查
ruby -v 

将您的gemfile更改为:

gem 'rack','1.0.1'
gem 'sqlite3'
gem 'pg'
gem 'taps'

并运行

bundle

在项目的一个终端上运行sqlite服务器:

taps server sqlite://db/development.sqlite3 user password

在项目运行的第二个终端上:

taps pull postgres://user@localhost/db/development http://user:secret@localhost:5000

这应该这样做。

然后替换你的gem文件并使用你想要的ruby版本!