41

同じバックエンドと通信する複数のRailsアプリケーションがあり、それらにいくつかの移行を共有してもらいたい。
Railsエンジン(enginexを使用)をセットアップしました。何でも(コントローラー、ビュー、モデルなど)共有できますが、移行はできません。動かせない!

ファイルdb/migrate / my_migration.rbを作成しようとしましたが、メインアプリケーションで作成した場合:

  rake db:migrate

それらをロードしません。

いくつかのグーグルの後、これに関するいくつかの最近の作業があったようであり、これはRailsマスターにマージされたようです。私はRails3.0.3を使用していますが、これを機能させる方法はありますか?

ありがとう !

4

6 に答える 6

44

Rails 3.1 では、このコマンドを使用して実行できます。エンジン名はexample次のとおりです。

# Note that you append _engine to the name
rake example_engine:install:migrations
于 2011-11-07T00:20:08.610 に答える
34

私がするInstallGeneratorことは、Rails サイト自体にマイグレーションを追加する を追加することです。あなたが言及したものとまったく同じ動作ではありませんが、今のところ、私にとっては十分です。

ちょっとしたハウツー:

まず、フォルダーを作成し、そのフォルダー内に次のコードでlib\generators\<your-gem-name>\install呼び出されるファイルを作成します。install_generator.rb

require 'rails/generators/migration'

module YourGemName
  module Generators
    class InstallGenerator < ::Rails::Generators::Base
      include Rails::Generators::Migration
      source_root File.expand_path('../templates', __FILE__)
      desc "add the migrations"

      def self.next_migration_number(path)
        unless @prev_migration_nr
          @prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
        else
          @prev_migration_nr += 1
        end
        @prev_migration_nr.to_s
      end

      def copy_migrations
        migration_template "create_something.rb", "db/migrate/create_something.rb"
        migration_template "create_something_else.rb", "db/migrate/create_something_else.rb"
      end
    end
  end
end

その中にlib/generators/<your-gem-name>/install/templates、移行を含む 2 つのファイルを追加します。たとえば、次の名前のファイルを使用しますcreate_something.rb

class CreateAbilities < ActiveRecord::Migration
  def self.up
    create_table :abilities do |t|
      t.string  :name
      t.string  :description
      t.boolean :needs_extent      
      t.timestamps
    end
  end

  def self.down
    drop_table :abilities
  end
end

次に、宝石がアプリに追加されたら、次のことができます

rails g <your_gem_name>:install

これで移行が追加されますrake db:migrate

お役に立てれば。

于 2011-01-18T15:38:12.353 に答える
23

3.1 では、config/application.rb を次のように変更することで、インストールせずに移行を共有できます。

# Our migrations live exclusively w/in the Commons project
config.paths['db/migrate'] = Commons::Engine.paths['db/migrate'].existent
于 2011-12-03T00:37:47.037 に答える
22

Rails 3.1 の時点で、解決策は次のようになります。

bundle exec rake railties:install:migrations

特定の railtie からのみコピーする場合は、次のようにします。

bundle exec rake railties:install:migrations FROM=foo_engine

名前は、gem の名前に _engine を加えたものであることに注意してください。したがって、gem が「foo」の場合、名前は foo_engine になります。

于 2011-08-23T14:32:31.520 に答える
13

リーバイスの答えから外れるには、アプリケーションではなく、実際のエンジンのエンジン ファイルでこのようなことを行うこともできます。

したがって、lib/commons/engine.rb

module Commons
  class Engine < Rails::Engine
    initializer "commons.load_app_instance_data" do |app|
      Commons.setup do |config|
        config.app_root = app.root
      end
      app.class.configure do 
        #Pull in all the migrations from Commons to the application
        config.paths['db/migrate'] += Commons::Engine.paths['db/migrate'].existent
      end
    end
    initializer "commons.load_static_assets" do |app|
      app.middleware.use ::ActionDispatch::Static, "#{root}/public"
    end
  end
end

編集:これを行った後、人の移行履歴を台無しにしないように注意してください。変更が必要な場合は、新しい移行を追加してください。そうしないと、誰かに醜いロールバックを強制する可能性があります。

于 2012-06-26T18:05:57.933 に答える