使用 MySQL 将 rails 应用程序部署到使用 ClearDB 插件的 Heroku

Deploy a rails app using MySQL to Heroku using ClearDB addon

我有一个名为 "enrollment_app" 的 Rails 应用程序,它使用 MySQL 种子文件初始化并填充数据库中的所有表。我构建了应用程序,添加了一些迁移并将我的应用程序推送到 Heroku。但是,由于 Heroku 使用 Postgres,我需要一种方法使我的 MySQL 数据库与 Heroku 兼容,所以我使用的是 ClearDB 插件。

当我尝试打开应用程序时,我收到消息:

Application Error
An error occurred in the application and your page could not be served. Please try again in a few moments.

If you are the application owner, check your logs for details.

所以,我检查了日志并看到了这个错误:

PG::UndefinedTable: ERROR:  relation "enrollments" does not exist

我一直在关注这个 tutorial 但显然我不知道如何使 ClearDB 看起来像我的本地 MySQL 数据库,因为我遇到了上面的错误。我怎样才能对生产 ClearDB 数据库执行 rake db:seed MySQL 种子文件和 rake db:migrate 的等效操作?

已更新 - Gemfile:

source 'https://rubygems.org'

gem 'rails', '4.2.1'
gem 'mysql2'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'bootstrap-sass', '~> 3.3.5'
gem 'bootswatch-rails'
gem 'ransack'
gem 'jquery-turbolinks'
gem 'kaminari'
gem 'bootstrap-kaminari-views'
gem 'jquery-ui-rails'
gem 'espinita'
gem 'mysqltopostgres', git: "https://github.com/maxlapshin/mysql2postgres.git"

group :development, :test do
  gem 'byebug'
  gem 'web-console', '~> 2.0'
  gem 'spring'
  gem 'rspec-rails'
  gem 'launchy'
  gem 'pry'
  gem 'pry-nav'
  gem 'shoulda-matchers'
  gem 'factory_girl_rails'
  gem 'capybara'
  gem 'newrelic_rpm'
  gem 'poltergeist'
  gem 'database_cleaner'
end

group :production do
  gem 'rails_12factor'
end

这不是 ClearDB 本身的问题,看起来您还没有完全脱离 PostgreSQL 的默认设置。我会检查:

  • 是否在您的 Gemfile 中定义了 pg?不应该的。而是确保 mysql2 存在。
  • 您是否为此应用安装了 Heroku ClearDB 附加组件,并将其设为您的应用将使用的默认数据库?您是否卸载了 Heroku Postgres 数据库? See here 获取完整说明。

一旦您的 Heroku 应用程序可以正确连接到 ClearDB 数据库,您应该能够毫无问题地自行设置数据库:

heroku run rake db:create
heroku run rake db:migrate
heroku run rake db:seed