【问题标题】:Fill development DB with production data with my Heroku app用我的 Heroku 应用程序用生产数据填充开发数据库
【发布时间】:2017-01-13 00:33:03
【问题描述】:

我正在尝试从我的 Heroku 应用程序中获取最新的生产数据,并将其恢复到我在本地主机上的开发数据库中。我用

备份了我的生产数据库
heroku pg:backups capture --app <my-app-name>

并使用下载它

curl -o latest.dump `heroku pg:backups public-url`

听起来我需要将它从 PostgreSQL 转换为 Sqlite3,可能使用类似http://manuelvanrijn.nl/blog/2012/01/18/convert-postgresql-to-sqlite/ 的过程,但我不是 100% 的。有没有更好的方法将二进制latest.dump 数据恢复到我的development.sqlite3 中?

编辑:听起来我需要做的就是在我的机器上设置 Postgres,重新配置我的 RoR 应用程序设置以使用 Postgres,并使用 pg_restore 恢复生产数据库。由于我的 database.yml 文件,我有点困惑:

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  database: db/production.sqlite3

heroku 是否忽略了生产设置并仍然使用 Postgres?

【问题讨论】:

  • 在理想情况下,您应该使用Postgres,恕我直言,尝试使用Postgresql 进行开发,并使用常规postgres 命令转储数据。请不要花时间在这些事情上。您可以轻松跳过这部分并学习更重要的内容。

标签: ruby-on-rails postgresql heroku sqlite


【解决方案1】:

更好的选择是通过在测试、开发和生产中使用相同的数据库来确保您拥有所谓的dev/production parity

使用不同的数据库可能会让错误进入生产环境,这很尴尬,而且代价高昂。

在本地机器上设置 Postgres 并使用 pg_restore 镜像生产数据库。

添加

Heroku 不支持 SQLite,因为它非常不适合:

SQLite 在内存中运行,并将其数据存储备份到磁盘上的文件中。 虽然这种策略非常适合开发,但 Heroku 的 Cedar 堆栈 有一个临时文件系统。你可以写它,你可以读 从它,但内容将定期清除。如果你要 在 Heroku 上使用 SQLite,您至少会丢失整个数据库 每 24 小时一次。 https://devcenter.heroku.com/articles/sqlite3

坦率地说,SQLite 非常适合需要在设备上集成数据库的移动应用程序,或者只是用于轻松启动和运行“hello world”rails 应用程序,但对于云中的现实世界 Web 应用程序来说不太好。

Heroku 也将override any of the settings you use in database.yml with the ENV[:DATABASE_URL] variable。所以是的,你是正确的,因为它无论如何都使用 Postgres。

有几个easy installers for Postgres, and its available for every package installer imaginable

关注How To Setup Ruby on Rails with Postgres 。这是开箱即用的最小 database.ymlPostgres.app 一起使用:

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: hermes_development

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: hermes_test

# Don't add a production section! It is set by ENV vars anyways.

【讨论】:

  • 您有关于如何执行此操作的参考吗?从我发现的参考资料(digitalocean.com/community/tutorials/…stackoverflow.com/questions/6710654/…)看来,不需要进行太多更改 - 但我想小心。
  • 已编辑 - 您的路径完全错误。你应该在你的开发中设置 Postgres,而不是试图让 sqlite 在生产中工作。
  • Max,很抱歉不清楚 - 这正是我一看到你的初始帖子就想做的事情。
猜你喜欢
  • 2012-04-08
  • 2012-02-25
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
  • 2013-02-28
  • 2011-09-28
  • 2011-02-08
  • 1970-01-01
相关资源
最近更新 更多