Skip to content

Chaining migrations fails when accessing the same model over again #30

Closed

Description

@fursich

Hi, thanks for developing the awesome gem!

Just happened to encounter a problem when running multiple migrations in a row. (not sure enough, but possibly related with Rails' caching behavior on column information).

Sorry I haven't had enough time to dig this any further until now (am I totally missing something here, or is there any known solutions already?), but just thought it would be worthwhile to raise it.

How to reproduce

0) Create a target model (Product here).

This model could be created separately in advance.

1) Within data method, access Product.

It can be anything that touches the model (e.g. just doing p = Product.where(id: 1) )

class CreateProducts < ActiveRecord::Migration[5.1]
  def change
    create_table :products do |t|
    end
  end

  def data # do whatever operation that requires access to Product model
    Product.create!
  end
end

2) Run another migration and change Product instance.

class AddName < ActiveRecord::Migration[5.1]
  def change
    add_column :products, :name, :string
  end

  def data # calling a instance method for Product (not a class method like update_all )
    product = Product.find(1)
    product.update!(name: 'foobar')
  end
end

3) The record is not updated after migration

> Product.all
[DEBUG]   Product Load (0.3ms)  SELECT `products`.* FROM `products`
=> [#<Product:0x007fd9a61b7c18 id: 1, name: nil>]

This happens only when you run two migrations (1 and 2) all together.

workaround

The quick (and obvious) solution on this would be running the migration one by one, which at times is not practical in some situations where you often have to catch up with the large diff in one run.

Another approach I occasionally found is to insert Product.reset_column_information before the (2) second procedure starts.

Just wanted to know if there is any solutions with this.
At minimum, adding a few caution on README would save someone's life :)

Rails 5.1.4
ruby 2.4.1p111

Activity

  1. ka8725 commented on Oct 3, 2017

    @ka8725
    Owner

    Hi @fursich This workaround is a good way to go in your case. The same things would happen in a basic migration and Rails developer should aware of that because it's already documented in Rails docs.

  2. ka8725 commented on Oct 3, 2017

    @ka8725
    Owner

    Anyway improved README. Thanks for your contribution @fursich !

  3. fursich commented on Oct 4, 2017

    @fursich
    Author

    Hi @ka8725, thanks a lot for your swift answer. Looks like I had had a total misconception that that caching part is also taken care of by the gem.

    A simple notion on README would be really great. Thanks really again!

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