1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
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
|