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

Ruby on Rails migrate database and data from SQLite3 to Postgres

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

事实证明这是非常困难的。我不习惯在 Rails 上 Ruby,所以可能错过了所有技巧。

我 运行 Ruby 2.2.0 / Rails 4.2.0 MAC

关于问题的任何想法/

我运行下面的命令从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.

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

的错误
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 -> /sessions/1568289369/pull/table_names

确保使用正确的 ruby 版本(在本例中为 2.0.0),如果您还没有这样做,我建议您安装和使用 RVM。

RVM use 2.0.0

打开两个终端 windows,确保它们都指向 ruby 2.0.0,因为打开一个新终端将恢复到 ruby 的默认版本,通过 [=16] 检查=]

ruby -v 

将您的 gem 文件更改为:

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 版本!

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

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

仅供 google = )