Railsエンジンでの移動?
-
13-10-2019 - |
質問
複数のRailsアプリケーションが同じバックエンドと話しているので、移行を共有してほしいと思います。
Rails Engine(EngineXを使用)をセットアップし、何でも共有できます(コントローラー、ビュー、モデルなど)が移行はありません。私はそれを機能させることができません!
ファイルdb/migrate/my_migration.rbを作成しようとしましたが、メインアプリケーションでは、
rake db:migrate
それらをロードしません。
いくつかのグーグルの後、いくつかがあるように見えます 最近の作品 これについて、それは思われます これ Rails Masterに合併しました。私はRails 3.0.3を使用しています。
ありがとう !
解決
私がしていることは、追加することです 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
.
お役に立てれば。
他のヒント
Rails 3.1では、このコマンドを使用してそれを行うことができ、エンジン名が example
:
# Note that you append _engine to the name
rake example_engine:install:migrations
3.1以下では、config/application.rbを変更して次のことを行うことにより、移行をインストールせずに共有できます。
# Our migrations live exclusively w/in the Commons project
config.paths['db/migrate'] = Commons::Engine.paths['db/migrate'].existent
Rails 3.1のように、ソリューションは次のように見えます。
bundle exec rake railties:install:migrations
特定のRailtieからのみコピーしたい場合は、
bundle exec rake railties:install:migrations FROM=foo_engine
名前は、あなたが宝石が_Engineという名前の任意のものです。したがって、宝石が「foo」の場合、名前はfoo_engineです。
レール4の使用:
initializer :append_migrations do |app|
unless app.root.to_s.match root.to_s
config.paths["db/migrate"].expanded.each do |expanded_path|
app.config.paths["db/migrate"] << expanded_path
end
end
end
https://content.pivotal.io/blog/leave-yourmigrations-in your-rails-engines
Leviの回答から外れるために、アプリケーションではなく、実際にエンジンのエンジンファイルでこのようなことをすることもできます。
したがって、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
編集:これを行った後に人々の移行履歴を台無しにしないように注意してください。変更が必要な場合は新しい移行を追加してください。