简体   繁体   English

跨多个迁移重用代码:辅助模块和辅助方法

[英]Reusing code across multiple migrations: helper modules and helper methods

I need to reuse the same method across many migrations.我需要在许多迁移中重复使用相同的方法。 My goal is to avoid code duplication.我的目标是避免代码重复。 I tried to do it as shown below, by putting the shared method into file lib/migration_helper.rb and using include MigrationHelper in the migrations that use the shared method.我尝试如下所示,通过将共享方法放入文件lib/migration_helper.rb并在使用共享方法的迁移中使用include MigrationHelper

Is there a more standard way of sharing code in different migrations?是否有更标准的方式在不同的迁移中共享代码?

In particular, I put the helper file into lib directory - is this the correct place?特别是,我将帮助程序文件放入lib目录 - 这是正确的位置吗?

## lib/migration_helper.rb

# Methods shared across migrations.

module MigrationHelper
  def my_shared_method
    # some shared code
  end
end
## db/migrate/do_something.rb

class DoSomething < ActiveRecord::Migration[5.2]
  include MigrationHelper
  # rubocop:disable Metrics/MethodLength
  def up
    # some code
    my_shared_method
  end
  # rubocop:enable Metrics/MethodLength

  def down
    # more code
    my_shared_method
  end

SEE ALSO:也可以看看:

I got a few ideas from these questions, but they do not fully answer my question:我从这些问题中得到了一些想法,但它们并没有完全回答我的问题:
Custom helper methods for Rails 3.2 Migrations Rails 3.2 迁移的自定义辅助方法
Rails share code between migrations (aka concerns) Rails 在迁移之间共享代码(又名关注点)
Accessing custom helper methods in rails 3 migrations在 Rails 3 迁移中访问自定义辅助方法

This repo has examples of a much more complex version of what I want, with a whole hierarchy of helpers.这个 repo 有一个我想要的更复杂版本的例子,有一个完整的助手层次结构。 I need a simpler solution:我需要一个更简单的解决方案:
https://gitlab.com/gitlab-org/gitlab-foss/blob/master/lib/gitlab/database/migration_helpers.rb https://gitlab.com/gitlab-org/gitlab-foss/blob/master/lib/gitlab/database/migration_helpers.rb
https://gitlab.com/gitlab-org/gitlab-foss/-/blob/master/db/migrate/20220808133824_add_timestamps_to_project_statistics.rb https://gitlab.com/gitlab-org/gitlab-foss/-/blob/master/db/migrate/20220808133824_add_timestamps_to_project_statistics.rb

just tested, works in rails 7 (and probably earlier version)刚刚测试过,适用于 Rails 7(可能还有更早的版本)

What you could do is:你可以做的是:

1 - create your file/class kinda wherever 1 - 在任何地方创建你的文件/类

  • app/lib/migration/something.rb应用程序/lib/迁移/something.rb
  • db/concerns/something.rb db/concerns/something.rb
  • ... ...
# db/concerns/create_column_alias.rb
module CreateColumnAlias
  def create_column_alias(*args)
    add_column(*args)
  end
end

2 - create an initializer to inject your new helper in the migrations classes (per this gist ) 2 - 创建一个初始化程序以在迁移类中注入你的新助手(根据这个要点

# initializers/extend_migration_with_custom_helpers.rb

require_relative "../../db/concerns/create_column_alias"

ActiveRecord::ConnectionAdapters::AbstractAdapter.send(:include, CreateColumnAlias)

3 - profit. 3 - 利润。

>$ bin/rails g my_migration

# db/migrate/123345456_my_migration.rb
class MyMigration < ActiveRecord::Migration[7.0]
  def change
    create_column_alias :tasks, :done, :boolean
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM