Sha256: 38db8a46093a107e41341a8299c3c0d8f933cb6316d48e79c75dd54bdbe546c9

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

module GoodMigrations
  class PreventsAppLoad
    def self.app_path?(path)
      path.starts_with? File.join(Rails.application.root, "app")
    end

    def self.prevent_load!(path)
      raise GoodMigrations::LoadError, <<~ERROR
        Rails attempted to auto-load:

        #{path}

        Which is in your project's `app/` directory. The good_migrations
        gem was designed to prevent this, because migrations are intended
        to be immutable and safe-to-run for the life of your project, but
        code in `app/` is liable to change at any time.

        The most common reason for this error is that you may be referencing an
        ActiveRecord model inside the migration in order to use the ActiveRecord API
        to implement a data migration by querying and updating objects.

        For instance, if you want to access a model "User" in your migration, it's safer
        to redefine the class inside the migration instead, like this:

        class MakeUsersOlder < ActiveRecord::Migration
          class User < ActiveRecord::Base
            # Define whatever you need on the User beyond what AR adds automatically
          end

          def up
            User.find_each do |user|
              user.update!(:age => user.age + 1)
            end
          end

          def down
            #...
          end
        end

        For more information, visit:

        https://github.com/testdouble/good-migrations

      ERROR
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
good_migrations-0.1.0 lib/good_migrations/prevents_app_load.rb