Search code examples
ruby-on-railsrails-migrationsrails-enginesrails-generators

Add Migrations to a Rails Engine's Install Generator


In my rails 5 engine I want to include the installation of the engine's migrations with the custom install generator I have at:

myengine/lib/generators/myengine/install_generator.rb

This generator currently looks like this:

module Myengine
  module Generators

    class InstallGenerator < Rails::Generators::Base
      source_root File.expand_path("../../templates", __FILE__)

      desc "Creates a Myengine initializer and copys template files to your application."

      def copy_initializer
        template "myengine.rb", "config/initializers/myengine.rb"
      end

    end
  end
end

When I add the engine to a rails app, instead of having to call:

rails g myengine:install

then

rails myengine:install:migrations

How can I add the creation of those migrations to the custom generator?


Solution

  • Fun question! And one that I hope I can answer. Let's say you have an engine named "Buttafly" and a generator living at:

    #lib/generators/buttafly/install/install_generator.rb
    

    At the top of your generator, require the 'date' library like so:

    require 'date'
    

    Then in the body of your generator, add a method definition like:

      def copy_migrations
    
        # get an array of the migrations in your engine's db/migrate/ 
        # folder:
    
        migrations = Dir[Buttafly::Engine.root.join("db/migrate/*.rb")]
        migrations.each_with_index do |migration, i|
    
        # The migrations will be created with the same timestamp if you 
        # create them all at once. So if you have more than one migration 
        # in your engine, add one second to the second migration's file
        # timestamp and a third second to the third migration's timestamp 
        # and so on:
    
        seconds = (DateTime.now.strftime("%S").to_i + i).to_s
        seconds = seconds.to_s.length == 2 ? seconds : "0#{seconds}"
        timestamp = (DateTime.now.strftime "%Y%m%d%H%M") + seconds
    
        # get the filename from the engine migration minus the timestamp:
        name = migration.split("/").split("_").last
    
        # See if a the name of your engine migration is already in your
        # host app's db/migrate folder:
    
        if Rails.root.join("db/migrate/*#{name}").exist?
    
          # do nothing:
          puts "Migration #{name} has already been copied to your app"
        else
    
          # copy your engine migration over to the host app with a new 
          # timestamp:
          copy_file m, "db/migrate/#{timestamp}_buttafly_#{name}"
        end
      end
    end