title: Rails 5 - generate and run migrations inside a rake task
body: <p>So I've got an app that when I create a new user, I setup columns in some other databases related to that specific user. I realize this isn't best practice, but for my use-case it's much faster than serializing an array which holds all user info for that table.</p>
  
  <p>What I'm trying to do is setup a rake task which creates the <code>User</code>, as well as doing the necessary migrations to the tables. </p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>  desc "Adds User and creates correct DB entries."
    task add_user: :environment do
      username = ENV['username'].to_s
      email = ENV['email'].to_s
      password = ENV['password'].to_s
      initials = ENV['initials'].to_s
      if username and email and password and initials
        User.create! :username =&gt; username, :email =&gt; email, :password =&gt; password, :password_confirmation =&gt; password, :initials =&gt; initials
        Rake::Task['generate migration AddPay' + initials + 'ToShoppingLists pay' + initials + ':decimal'].invoke
        Rake::Task['generate migration AddPay' + initials + 'ToPayments pay' + initials + ':decimal'].invoke
        Rake::Task['db:migrate'].invoke
      end
    end
  </code></pre>
  
  <p>My issues is that in <code>Rails 5</code>, I have to run <code>rails g migration</code> not <code>rake g migration</code> so I'm unsure how to call <code>rails</code> commands from inside a <code>rake</code> task.</p>
  
  <p>Additionally, is there a way to check if a migration has already been created? For example, if I run this in development mode, I don't need to recreate the migration in production mode, just perform <code>db:migrate</code>. </p>