Skip to content

Install migration into project instead of running from gem #26

Open

Description

@olso

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?

Activity

  1. olso commented on Feb 11, 2023

    @olso
    Author

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

  2. sunny commented on Feb 13, 2023

    @sunny
    Member

    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? 🙏🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions