8000 GitHub - takatoshiono/activerecord-readonly_statement: Check database statements readonly
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

takatoshiono/activerecord-readonly_statement

Repository files navigation

ActiveRecord::ReadonlyStatement

This gem provides a rack middleware ActiveRecord::ReadonlyStatement::Middleware to check database statements and raise ActiveRecord::ReadonlyStatementError if any statements are non read.

The middleware decides whether to enable checking by executing an enable_if block in each request.

Installation

Add this line to your application's Gemfile:

gem 'activerecord-readonly_statement'

And then execute:

$ bundle

Or install it yourself as:

$ gem install activerecord-readonly_statement

Usage

In Rails app,

config/initializers/activerecord-readonly_statement.rb:

Rails.application.config.middleware.use ActiveRecord::ReadonlyStatement::Middleware

In Rack app,

config.ru:

require 'activerecord/readonly_statement'
use ActiveRecord::ReadonlyStatement::Middleware

Configuration

ActiveRecord::ReadonlyStatement.configuration do |config|
  # Set the adapter class
  # TODO: config.adapter = :mysql2
  config.adapter = ActiveRecord::ConnectionAdapters::Mysql2Adapter

  # Determine which request is readonly
  config.enable_if do |env|
    env.fetch('REQUEST_METHOD') == 'GET'
  end
end

Use in request testing

For example, set to be enable if X-Check-Readonly request header exists.

config.enable_if do |env|
  env.fetch('HTTP_X_CHECK_READONLY', 'off') == 'on'
end

And in your test

get '/v1/your/api', headers: { 'X-Check-Readonly' => 'on' }

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/takatoshiono/activerecord-readonly_statement.

License

The gem is available as open source under the terms of the MIT License.

About

Check database statements readonly

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0