如何将现有的 rails 应用程序移至 heroku?(sqlite 到 postgres) [英] How do I move my existing rails app onto heroku? (sqlite to postgres)

查看:37
本文介绍了如何将现有的 rails 应用程序移至 heroku?(sqlite 到 postgres)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的 Ruby on Rails 应用,其中已经加载了数据.

I have an existing Ruby on Rails app that already has data loaded into it.

我使用了默认的 SQLite 数据库设置,所以我的所有数据都在那里,但我需要我的所有数据进入我在 heroku 上的 Postgres 数据库.

I used the default SQLite database setup, so that is where all my data is located, but I need all my data to go into my Postgres database on heroku.

我该怎么做?

推荐答案

10 分钟从本地 SQLite 迁移到 Heroku Postgres

-- 在此过程中将您的本地开发人员更新为 postgres --

这假设您在 sqlite 中有一个开发数据库,​​并且您想将结构和数据移动到 heroku.您将首先将本地环境更改为 postgres,然后将其全部升级.

10 Minutes Move from Local SQLite to a Heroku Postgres

-- updates your local dev to postgres along the way --

This is assuming you have a development database in sqlite and you want to move the structure and data to heroku. You will be first changing your local environment to postgres, then moving it all up.

为什么要改变?您应该始终让您的开发环境反映您的生产环境.使用 Postgres 是 heroku 上的默认设置.

Why change? You should always have your development environment mirror your production environment. Using Postgres is the default on heroku.

您需要首先使用拥有您用户名的用户在本地安装和配置 Postgres

所需软件:postgresql、pgloader、heroku-cli

在您的开发环境中从 SQLite 迁移到 Postgres

  1. 安装 heroku/pgloader/postgres,并确保 postgresql 在您的系统上运行
  2. 备份 sqlite - 将 development.sql 复制到 development_old.sql
  3. gem 'pg' 添加到 Gemfile 的主要部分
  4. 捆绑安装
  5. 更新 config/database.yml(参见下面的示例)
  6. 耙数据库:设置
  7. cd [应用程序根目录]
  8. 用数据加载 postgres 数据库 - pgloader ./db/development.sqlite3 postgresql:///[postgres dev db 的名称]
  9. 删除 gem 'sqlite3'
  10. 捆绑安装
  11. 启动服务器 - rails 服务器
  12. 通过访问 localhost:3000 的应用程序进行测试
  1. install heroku / pgloader / postgres, and make sure postgresql is running on your system
  2. backup sqlite - copy development.sql to development_old.sql
  3. add gem 'pg' to main section of your Gemfile
  4. bundle install
  5. update config/database.yml (see sample below)
  6. rake db:setup
  7. cd [application root]
  8. load postgres db with data - pgloader ./db/development.sqlite3 postgresql:///[name of postgres dev db]
  9. remove gem 'sqlite3'
  10. bundle install
  11. start server - rails server
  12. test by visiting app at localhost:3000

在 heroku 上设置新应用

遵循 heroku 中的这些说明

将数据移动到heroku

  1. 查找 heroku 数据库信息 - heroku pg:info
  2. 擦除并重置远程数据库 - heroku pg:reset DATABASE_URL --app [应用名称]
  3. 将本地数据推送到heroku - heroku pg:push [postgres dev db 的名称] DATABASE_URL --app [应用的名称]

注意:如果该数据库的行数超过 10k,您还需要升级到 heroku 上的业余爱好层

将 Heroku 升级到 Hobby Tier Basic

  1. 创建新层 - `heroku addons:create heroku-postgresql:hobby-basic --app [应用程序名称]
  2. 获取新的数据库 url - heroku pg:info
  3. 开启维护 - heroku maintenance:on --app [应用名称]
  4. 复制数据 - heroku pg:copy DATABASE_URL [HEROKU_POSTGRESQL_COLOR_URL] --app [应用名称]
  5. promote new db - heroku pg:promote [HEROKU_POSTGRESQL_COLOR_URL] --app [应用名称]
  6. 关闭维护
  7. 通过访问 heroku 应用程序进行测试

<小时>

如果您遇到问题或边缘情况,这里有一些资源可以提供帮助.


In case you run into issues or edge cases, here are some resources to help.

database_sample.yml

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

development:
  <<: *default
  database: [name of app]_dev

test:
  <<: *default
  database: [name of app]_test

staging:
  <<: *default
  database: [name of app]

production:
  <<: *default
  database: [name of app]

这篇关于如何将现有的 rails 应用程序移至 heroku?(sqlite 到 postgres)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆