tcocca / acts_as_follower

A Gem to add Follow functionality for models
Other
866 stars 195 forks source link

migrating fails on basic install b/c of new rails5 migration versioning #110

Open RudyOnRails opened 5 years ago

RudyOnRails commented 5 years ago

https://github.com/rails/rails/pull/21538

[following_with_older_gem]~/code/gavelizer: rake db:migrate
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for:

  class ActsAsFollowerMigration < ActiveRecord::Migration[4.2]

Will submit PR shortly.

RudyOnRails commented 5 years ago

And if anyone on rails 5 runs into this that needs to migrate before waiting for fix, just follow the gem's README directions as normal, but after running rails generate acts_as_follower, and before migrating, change the migration to add your rails version (note the added [5.1] or whatever rails version you're on:

class ActsAsFollowerMigration < ActiveRecord::Migration[5.1]
  def self.up
    create_table :follows, force: true do |t|
      t.references :followable, polymorphic: true, null: false
      t.references :follower,   polymorphic: true, null: false
      t.boolean :blocked, default: false, null: false
      t.timestamps
    end

    add_index :follows, ["follower_id", "follower_type"],     name: "fk_follows"
    add_index :follows, ["followable_id", "followable_type"], name: "fk_followables"
  end

  def self.down
    drop_table :follows
  end
end

Then migrate as normal, and you should be 👍

thatosmk commented 4 years ago

I also came across this issue after installing this gem today. I looked at #111 and I noticed that the fixed for this has not been merged. This worked for me also

if ActiveRecord.gem_version >= Gem::Version.new('5.0')
   class ActsAsFollowerMigration < ActiveRecord::Migration["#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"]; end
else
   class ActsAsFollowerMigration < ActiveRecord::Migration; end
end
ActsAsFollowerMigration.class_eval do
def self.up
    create_table :follows, force: true do |t|
      t.references :followable, polymorphic: true, null: false
      t.references :follower,   polymorphic: true, null: false
      t.boolean :blocked, default: false, null: false
      t.timestamps
    end

    add_index :follows, ["follower_id", "follower_type"],     name: "fk_follows"
    add_index :follows, ["followable_id", "followable_type"], name: "fk_followables"
  end

  def self.down
    drop_table :follows
  end
end