Sha256: d414e2b4c7d01f263a91f87c1dc1b4899b146164e8d7fd7290ab55c65030691a

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

module Landable
  class Migration < ActiveRecord::Migration
    class << self
      def connection_search_path(conn)
        conn.execute('SHOW SEARCH_PATH')[0]['search_path']
      end

      def models
        @models ||= begin
                      classes = Landable.constants.map { |c| "Landable::#{c}".constantize }
                      classes += Landable::Traffic.constants.map { |c| "Landable::Traffic::#{c}".constantize }
                      classes.select { |c| c.is_a?(Class) && c.ancestors.include?(ActiveRecord::Base) }
                    end
      end

      def clear_cache!
        models.each(&:reset_primary_key)
        ActiveRecord::Base.connection.schema_cache.clear!
      end
    end

    def exec_migration(conn, direction)
      # come what may, keep the connection's schema search path intact
      with_clean_connection(conn) do
        super
      end

      # reset a few things, lest we pollute the way for those who follow
      self.class.clear_cache!
    end

    protected

    def with_clean_connection(conn)
      original_search_path = self.class.connection_search_path conn
      yield
      conn.schema_search_path = original_search_path
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
landable-1.14.0 lib/landable/migration.rb
landable-1.13.2 lib/landable/migration.rb