active_currency icon indicating copy to clipboard operation
active_currency copied to clipboard

Install migration into project instead of running from gem

Open olso opened this issue 2 years ago • 2 comments

Hey,

First of all, thanks for this library. I'm a newbie to Rails.

Running rake db:migrate runs migration from this gem, but I would like to have the migration in my project (which seems like a good practice in general)

Right now its hidden inside this gem and I will definitely forget about it or any new member to this project.

So I've ran rake active_currency:install:migrations which placed the files into my project like so

# 20230211200817_create_active_currency_rates.active_currency.rb

# frozen_string_literal: true

# This migration comes from active_currency (originally 20180911202100)
require 'active_currency/migration'

class CreateActiveCurrencyRates < ActiveCurrency::Migration
  def change
    create_table :active_currency_rates do |t|
      t.string :from
      t.string :to
      t.float :value
      t.datetime :created_at
    end

    reversible do |dir|
      dir.up do
        add_index :active_currency_rates,
                  %i[from to created_at],
                  name: 'index_active_currency_rates'
      end
      dir.down do
        remove_index :active_currency_rates, 'index_active_currency_rates'
      end
    end
  end
end

But now when I run rake db:migrate, it still tries to run this gem migration, causing

root@a864e105f4e5:/usr/src/app# ./bin/rake db:migrate
rake aborted!
ActiveRecord::DuplicateMigrationNameError: 

Multiple migrations have the name CreateActiveCurrencyRates.

/usr/local/bundle/gems/activerecord-7.0.4.2/lib/active_record/migration.rb:1384:in `validate'
/usr/local/bundle/gems/activerecord-7.0.4.2/lib/active_record/migration.rb:1257:in `initialize'
/usr/local/bundle/gems/activerecord-7.0.4.2/lib/active_record/migration.rb:1117:in `new'
/usr/local/bundle/gems/activerecord-7.0.4.2/lib/active_record/migration.rb:1117:in `up'
/usr/local/bundle/gems/activerecord-7.0.4.2/lib/active_record/migration.rb:1092:in `migrate'
/usr/local/bundle/gems/activerecord-7.0.4.2/lib/active_record/tasks/database_tasks.rb:262:in `migrate'
/usr/local/bundle/gems/activerecord-7.0.4.2/lib/active_record/railties/databases.rake:92:in `block (2 levels) in <main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

Any suggestions?

olso avatar Feb 11 '23 20:02 olso