Rails 引擎中的迁移? [英] Migrations in Rails Engine?

查看:49
本文介绍了Rails 引擎中的迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个 Rails 应用程序与同一个后端通信,我希望它们共享一些迁移.
我设置了一个 rails 引擎(使用 enginex),我可以共享任何东西(控制器、视图、模型……),但不能进行迁移.我不能让它工作!

I have multiple rails applications talking to the same backend and I'd like them to share some migrations.
I setup a rails engine (with enginex), I can share anything (controllers, views, models,...) but no migrations. I can't make it work !

我尝试创建一个文件 db/migrate/my_migration.rb,但如果我这样做,则在我的主应用程序中:

I tried to create a file db/migrate/my_migration.rb but in my main application if I do :

  rake db:migrate

它不会加载它们.

经过一些谷歌搜索后,似乎有一些 最近的工作 似乎已合并到 rails master.我在使用 rails 3.0.3 你有什么办法可以让这个工作吗?

After some googling it appears there was some recent work on this and it seems this has been merge to rails master. I'm with rails 3.0.3 do you see any way to make this work ?

谢谢!

推荐答案

我所做的是添加一个 InstallGenerator 将迁移添加到 Rails 站点本身.它的行为与您提到的不完全相同,但就目前而言,对我来说已经足够了.

What i do, is add an InstallGenerator that will add the migrations to the Rails site itself. It has not quite the same behavior as the one you mentioned, but for now, for me, it is good enough.

一个小方法:

首先,创建文件夹 lib\generators\\install 并在该文件夹中创建一个名为 install_generator.rb 的文件,内容如下代码:

First, create the folder lib\generators\<your-gem-name>\install and inside that folder create a file called install_generator.rb with the following code:

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//install/templates 中添加包含迁移的两个文件,例如取名为 create_something.rb 的那个:

and inside the lib/generators/<your-gem-name>/install/templates add your two files containing the migrations, e.g. take the one named 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

然后,当你的 gem 添加到某个应用时,你就可以了

Then, when your gem is added to some app, you can just do

rails g <your_gem_name>:install

这将添加迁移,然后您就可以执行rake db:migrate.

and that will add the migrations, and then you can just do rake db:migrate.

希望这会有所帮助.

这篇关于Rails 引擎中的迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆