Skip to content

Add modular safeguard support #659

@thefotios

Description

@thefotios

My team recently came across a really nasty issue where spring kept our ActiveRecord::Base.connection_config cached. In that case, the current ENV var based safeguards failed us (luckily nobody was seriously injured).

For example,

# First, let's run a console
> DATABASE_URL=$(heroku config:get DATABASE_URL -a my-prod-app) bin/rails console

# :tada: we're running tests against the old DATABASE_URL even though it's not set
> bin/rspec

There are lots of different ways to get into this scenario (like other code loaders or multiple ORMs that use other variables). While checking for the ENV vars works for some low hanging fruit, it doesn't let ORMs (or maybe even other app/test frameworks) make more intelligent decisions.

We actually just added this to our test suite and it works great (based on the idea in #521 (comment))

# Simplified version
DatabaseCleaner::Safeguard::CHECKS << Class.new do
	def run
    	# Prevent running tests against protected environments
        #   - This is the same mechanism that the `rake` tasks use so you can't
        #     run destructive operations like `db:reset` against production
        ::ActiveRecord::Tasks::DatabaseTasks.check_protected_environments!
    end
end

I think it could be very useful to modularize the concept of a Safeguard so that various ORM modules could provide their own (the above case could something like DatabaseCleaner::ActiveRecord::Safeguard::ProtectedEnvironment).

I would be more than willing to work on this, but wanted to gauge interest and possibly talk about some design decisions before going ahead with it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions