Sha256: f1c146cb42ecd66e2f703d041f94cfab8208ca79d4da9b29038247c041a6c578

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

module DatabaseCleaner
  class Safeguard
    class Error < Exception
      class RemoteDatabaseUrl < Error
        def initialize
          super("ENV['DATABASE_URL'] is set to a remote URL. Please refer to https://github.com/DatabaseCleaner/database_cleaner#safeguards")
        end
      end

      class ProductionEnv < Error
        def initialize(env)
          super("ENV['#{env}'] is set to production. Please refer to https://github.com/DatabaseCleaner/database_cleaner#safeguards")
        end
      end
    end

    class RemoteDatabaseUrl
      LOCAL = %w(localhost 127.0.0.1)

      def run
        raise Error::RemoteDatabaseUrl if !skip? && given?
      end

      private

        def given?
          remote?(ENV['DATABASE_URL'])
        end

        def remote?(url)
          url && !LOCAL.any? { |str| url.include?(str) }
        end

        def skip?
          ENV['DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL'] ||
            DatabaseCleaner.allow_remote_database_url
        end
    end

    class Production
      KEYS = %w(ENV RACK_ENV RAILS_ENV)

      def run
        raise Error::ProductionEnv.new(key) if !skip? && given?
      end

      private

        def given?
          !!key
        end

        def key
          @key ||= KEYS.detect { |key| ENV[key] == 'production' }
        end

        def skip?
          ENV['DATABASE_CLEANER_ALLOW_PRODUCTION'] ||
            DatabaseCleaner.allow_production
        end
    end

    CHECKS = [
      RemoteDatabaseUrl,
      Production
    ]

    def run
      CHECKS.each { |const| const.new.run }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
database_cleaner-1.7.0 lib/database_cleaner/safeguard.rb