【问题标题】:Rails 4.2 migration postgres changes not workingRails 4.2迁移postgres更改不起作用
【发布时间】:2016-05-14 20:59:47
【问题描述】:

我在 Digital Ocean 上有一个 Rails 4.2 应用程序。部署 cmets 模型(带有迁移文件)后,它在开发中运行良好,但在生产中无法正常运行。 我一直在使用 Capistrano(根据本教程 - https://www.digitalocean.com/community/tutorials/deploying-a-rails-app-on-ubuntu-14-04-with-capistrano-nginx-and-puma)部署一直运行良好,直到现在。

这是我的迁移文件(在我的部署中丢失)。

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.references :user, index: true, foreign_key: true
      t.text :body
      t.references :path, index: true, foreign_key: true

      t.timestamps null: false
    end
  end
end

我检查并可以在生产和开发匹配中看到我的架构,并参考迁移文件中的最后一个引用。 但是当

当我使用rails控制台生产时,我可以看到cmets表不存在。这是我控制台中的消息。

Loading production environment (Rails 4.2.5)
2.2.1 :001 > Comment.all
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "comments" does not exist
LINE 1: SELECT "comments".* FROM "comments"
                                 ^
: SELECT "comments".* FROM "comments"
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `async_exec'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `block in exec_no_cache'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract_adapter.rb:472:in `block in log'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `log'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `exec_no_cache'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:584:in `execute_and_clear'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql/database_statements.rb:160:in `exec_query'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:355:in `select'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:32:in `select_all'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/query_cache.rb:70:in `select_all'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/querying.rb:39:in `find_by_sql'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/relation.rb:639:in `exec_queries'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/relation.rb:515:in `load'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/relation.rb:243:in `to_a'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/relation.rb:630:in `inspect'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands/console.rb:110:in `start'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands/console.rb:9:in `start'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/app_rails_loader.rb:45:in `require'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/app_rails_loader.rb:45:in `block in exec_app_rails'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/app_rails_loader.rb:34:in `loop'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/app_rails_loader.rb:34:in `exec_app_rails'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/cli.rb:5:in `<top (required)>'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/bin/rails:9:in `require'
    from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/bin/rails:9:in `<top (required)>'
    from /home/laurie/.rvm/gems/ruby-2.2.1/bin/rails:23:in `load'
    from /home/laurie/.rvm/gems/ruby-2.2.1/bin/rails:23:in `<main>'
    from /home/laurie/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `eval'
    from /home/laurie/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `<main>'2.2.1 :002 > 

我也尝试过 sudo-apt-get clean,我已经重置了我的数据库,添加了动作包缓存 gem,并多次重新启动了我的服务器。

我的生产日志列出了未呈现的 cmets 视图文件(因为没有 cmets 表)。

运行 rake db:migrate:status 显示“无文件”

up     20151224215110  Devise create users
up     20151231173736  Add first name last name to users
up     20160112022120  Add role to users
up     20160122204744  Create paths
up     20160123004558  Add attachment image to paths
up     20160124191428  Remove user id from paths
up     20160124191601  Add user id to paths
up     20160124212622  Remove bio from users
up     20160124213023  Add bio to users
up     20160124213311  Remove title from paths
up     20160124213624  Add title content to paths
up     20160204222816  ********** NO FILE **********

检查目录(生产服务器)我看到文件不存在。

另外,删除旧的迁移,检查我的服务器的内存等。出现空白?

【问题讨论】:

    标签: ruby-on-rails postgresql caching dbmigrate


    【解决方案1】:

    我知道他是一个愚蠢的问题,但是您是否在生产环境中使用控制台?

    RAILS_ENV=production rails c
    

    【讨论】:

    • 或者您在 Digital Ocean 机器上拥有的任何环境。
    • 另外,这可能是因为你没有推送最后一次提交(有迁移的那个),你可以验证你是否推送到 capistrano 用来获取的分支服务器中的代码。我只是猜测:(
    • 不是一个愚蠢的问题。是的,我在我的生产服务器中,首先在开发中检查控制台,然后在生产中。我使用 Rails c 生产..
    • 我在 Ubuntu 14.04 上运行
    • 我觉得这个问题是相关的,你查了吗? stackoverflow.com/questions/16041790/…
    猜你喜欢
    • 1970-01-01
    • 2017-10-01
    • 2013-10-09
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多