Skip to content

Allow Safeguards to be added and/or overridden outside of core #720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 42 additions & 7 deletions lib/database_cleaner/safeguard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,27 @@ def initialize
end
end

class AllowedUrl
# Base class for core-consumers to implement Safeguards
#
class Base
def run
raise NotImplementedError
end

def self.inherited(subclass)
DatabaseCleaner::Safeguard.registry << subclass
DatabaseCleaner::Safeguard.deprecated_registry.reject! do |const|
subclass.name.split("::").last == const.name.split("::").last
end
end
end

# Just a marker class for Safeguards implemented by core that are kept for backwards compatibility.
# Adapters should implement their own Safeguards using Safeguard::Base.
#
class Deprecated; end

class AllowedUrl < Deprecated
def run
return if skip?
raise Error::UrlNotAllowed if database_url_not_allowed?
Expand All @@ -39,8 +59,7 @@ def skip?
end
end


class RemoteDatabaseUrl
class RemoteDatabaseUrl < Deprecated
LOCAL = %w(localhost 127.0.0.1)

def run
Expand Down Expand Up @@ -73,7 +92,7 @@ def skip?
end
end

class Production
class Production < Deprecated
KEYS = %w(ENV APP_ENV RACK_ENV RAILS_ENV)

def run
Expand All @@ -96,14 +115,30 @@ def skip?
end
end

CHECKS = [
def run
self.class.registry.each { |const| const.new.run }
self.class.deprecated_registry.each { |const| const.new.run }
end

@registry = []
@deprecated_registry = [
RemoteDatabaseUrl,
Production,
AllowedUrl
]

def run
CHECKS.each { |const| const.new.run }
class << self
attr_reader :registry
attr_reader :deprecated_registry

def reset_registry!
@registry = []
@deprecated_registry = [
RemoteDatabaseUrl,
Production,
AllowedUrl
]
end
end
end
end
Loading
Loading