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

时间:2020-12-21 15:16:20

I'm trying to migrate a small SQLite database to PostgreSQL so that I can put it on Heroku. The plan is to migrate the local database to a instance of PostgreSQL. Once I know that works fine I'll migrate the database to heroku.

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

It is proving to be extremely difficult. I'm not use to Ruby on Rails, so probably missing all the tricks.

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

I'm running Ruby 2.2.0 / Rails 4.2.0 on MAC

我在MAC上运行Ruby 2.2.0 / Rails 4.2.0

Any thoughs on what the issue is/

关于问题是什么/

I'm running the following command to migrate from SQLite3 to PostgreSQL. Note the Db:migrate worked and the database schema is in place. I just need to get the data into it now.

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

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

I then get the following output:

然后我得到以下输出:

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

In another terminal where I started the service I get the following which contains an error referring to a 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

localhost - - [06 / May / 2015:17:28:57 BST]“GET / sessions / 1568289369 / pull / table_names HTTP / 1.1”500 317 - - > / sessions / 1568289369 / pull / table_names

2 个解决方案

#1


Make sure to use the correct version of ruby in this case 2.0.0, I would recommend installing and using RVM if you have not done so already.

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

RVM use 2.0.0

Have two terminal windows open ensuring both are pointing to ruby 2.0.0 as opening a new terminal will revert to your default version of ruby, check by

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

ruby -v 

change your gemfile to:

将您的gemfile更改为:

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

and run

bundle

on one terminal within your project run the sqlite server:

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

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

on the second terminal within your project run:

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

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

This should do it.

这应该做到这一点。

Then replace your gem file and use the version of ruby you want!

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

#2


dont use taps, use sequel command like:

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

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

just for google = )

仅适用于google =)

#1


Make sure to use the correct version of ruby in this case 2.0.0, I would recommend installing and using RVM if you have not done so already.

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

RVM use 2.0.0

Have two terminal windows open ensuring both are pointing to ruby 2.0.0 as opening a new terminal will revert to your default version of ruby, check by

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

ruby -v 

change your gemfile to:

将您的gemfile更改为:

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

and run

bundle

on one terminal within your project run the sqlite server:

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

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

on the second terminal within your project run:

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

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

This should do it.

这应该做到这一点。

Then replace your gem file and use the version of ruby you want!

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

#2


dont use taps, use sequel command like:

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

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

just for google = )

仅适用于google =)