Ruby-on-rails 从 rails 控制台运行迁移

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7287250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:54:38  来源:igfitidea点击:

Run migrations from rails console

ruby-on-railsruby-on-rails-3.1

提问by rafamvc

Is there a way to run rake commands for db:migrate and db:rollback on the console?

有没有办法在控制台上为 db:migrate 和 db:rollback 运行 rake 命令?

It sucks to wait for the rails environment to load!

等待 Rails 环境加载很糟糕!

回答by Homan

In the console:

在控制台中:

ActiveRecord::Migration.remove_column :table_name, :column_name

To update your schema.rbfile after running migrations from the console, you must run rails db:migrate

schema.rb在从控制台运行迁移后更新文件,您必须运行rails db:migrate

回答by Benoit Garret

Rails <= 4

导轨 <= 4

This will allow you to migrate without reloading the whole rails environment:

这将允许您在不重新加载整个 rails 环境的情况下进行迁移:

ActiveRecord::Migrator.migrate "db/migrate"

and rollback:

和回滚:

# 3 is the number of migration to rollback, optional, defaults to 1
ActiveRecord::Migrator.rollback "db/migrate", 3

Rails >= 5 (thanks to @gssbzn, his answer is below)

Rails >= 5(感谢@gssbzn,他的回答如下)

ActiveRecord::MigrationContext.new("db/migrate").migrate

回答by kizzx2

Another way that I find neater to just run some migration command from console is this:

我发现从控制台运行一些迁移命令更简洁的另一种方法是:

ActiveRecord::Schema.define do
  create_table :foo do |t|
    t.string  :bar
    t.timestamps
  end
end

This has the advantage that the contents inside the block is compatible with just copy and pasting random contents from a real migration file / schema.rb.

这样做的优点是块内的内容与仅从真实迁移文件 / 复制和粘贴随机内容兼容schema.rb

回答by gssbzn

For rails 5.2 the accepted answer has been removed and replaced with

对于 rails 5.2,已接受的答案已被删除并替换为

ActiveRecord::MigrationContext.new("db/migrate").migrate

Please be aware as this may also change for future versions of rails as they work to add multiple database connections

请注意,这也可能会因 Rails 的未来版本而改变,因为它们可以添加多个数据库连接

回答by dexter

You can use the %x[command]

您可以使用 %x[command]

%x[rake db:migrate]

回答by grosser

I needed to pretend a migration was run to unblock a deploy, this can be done with:

我需要假装运行迁移以解除部署的阻塞,这可以通过以下方式完成:

class Mig < ActiveRecord::Base; self.table_name = 'schema_migrations';end
Mig.create! version: '20180611172637'

回答by Josh

I created a method in my .irbrc file that runs migrations then reloads the console:

我在我的 .irbrc 文件中创建了一个运行迁移然后重新加载控制台的方法:

def migrate
  if defined? Rails::Console # turn off info logging for Rails 3
    old_log_level = ActiveRecord::Base.logger.try(:sev_threshold)
    ActiveRecord::Base.logger.sev_threshold = Logger::WARN
  end
  reload! && migations_ran = true if ActiveRecord::Migrator.migrate(Rails.root.join("db/migrate")).any?
  ActiveRecord::Base.logger.sev_threshold = old_log_level if defined? old_log_level
  migations_ran ||= nil # useful exit status
end

See the entire file here: https://gist.github.com/imme5150/6548368

在此处查看整个文件:https: //gist.github.com/imme5150/6548368

回答by Shai Coleman

For Rails 5 and Rails 6:

对于 Rails 5 和 Rails 6:

ActiveRecord::Base.connection.migration_context.migrate

For Rails 3 and Rails 4:

对于 Rails 3 和 Rails 4:

ActiveRecord::Migrator.migrate 'db/migrate'