2

I want to create a Ruby Gem to integrate specific functionality into my Rails app or any other Rails app that could use this functionality. The functionality will require its own database tables. How could I have a Ruby Gem generate the require database migrations in the Rails application?

My initial ideas were (1) to have a rake task in the Gem call the rails generate migration .. commands required for the migrations or (2) to have pre-written migration files which a rake task in the Gem just copies from the Gem into the Rails migrations folder.

I feel like with idea (1) I would be less reliant on a specific version of the migration syntax but I am not sure if it is possible to create more complex schemas using the rails generate commands.

To reiterate the question: How could I have a Ruby Gem generate the require database migrations in the Rails application?

ThriceGood
  • 1,633
  • 3
  • 25
  • 43
  • 5
    Best bet is sort of a combination of 1 and 2. You control the rails version syntax based on your own .gemspec dependencies. You can create anything that you can create manually you just have to build your own [generators](https://guides.rubyonrails.org/generators.html) to generate the migration file (not exactly copying). Depending on what you are trying to do this may not be a trivial task. For instance here is how [devise](https://github.com/heartcombo/devise/tree/master/lib/generators/active_record) deals with migrations. – engineersmnky May 20 '21 at 13:35

0 Answers0