【问题标题】:Where is the appropriate location to define a rails migration method?定义 Rails 迁移方法的合适位置在哪里?
【发布时间】:2018-10-30 06:49:48
【问题描述】:

我想定义一个简单的方法来在通过迁移删除特定表时确认用户输入,如下所示。执行此操作的适当文件在哪里?

  def confirm_drop(table)
    if table_exists?(table)
      puts "Warning: Did not drop table to protect data."
      puts "Drop the table? [y]es | [n]o | [c]ancel"
      answer = STDIN.gets.chomp
      if (answer == "y" or answer == "yes")
        drop_table :table
      elsif (answer == "c" or answer == "cancel")
        raise exception "Canceled Migration"
      elsif (answer != "n" or answer == "no")
        raise exception "Input was something other than. [y]es | [n]o | [c]ancel"
      end
    end
  end

【问题讨论】:

    标签: ruby-on-rails migration dry


    【解决方案1】:

    我不建议根据用户输入进行迁移,因为它在开发中可能工作正常,但在发布到生产环境时则不行。特别是如果您使用 CI 服务器。

    【讨论】:

    • 谢谢,不知道。有没有什么特别的原因它不好?我认为当有人在终端中运行它时,最好有一个简单的故障保险来保护数据,而无需在迁移之前进行潜在的大量备份。
    • 在开发中这听起来可能是个好主意,但是当您通过 CI 服务器在生产环境中运行迁移时,就没有这种可能性了。在我看来,这就像一个错误的故障安全机制,因为数据完整性的责任现在取决于用户,而不是应用程序。
    猜你喜欢
    • 1970-01-01
    • 2017-07-02
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多