sunny / active_currency

Rails plugin to store currency rates in the database

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Install migration into project instead of running from gem

olso opened this issue · 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?

Maybe it doesn't matter, if schema.rb is source of truth?

Hi @olso!

I’ve used the gem and several others that work of the box without requiring to call the *:install:migrations task. Indeed schema.rb is the source of truth. However, I agree that there are less gems these days that follow this pattern, as it tends to hide the migrations a bit.

So perhaps it’s time to change this gem to ask users for this small extra step on install.
To do that we would need to:

  • update documentation to ask to call bin/rails active_currency:install:migrations
  • stop appending the gem’s migrations inside the Rails Engine

Would you be willing to start a pull-request with these changes? 🙏🏻