-
-
Save Epictetus/2566049 to your computer and use it in GitHub Desktop.
readonly rails console using Arproxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
APP_PATH = File.expand_path('../../config/application', __FILE__) | |
require File.expand_path('../../config/boot', __FILE__) | |
require 'rails/commands/console' | |
require APP_PATH | |
Rails.application.require_environment! | |
require "arproxy" | |
module Arproxy | |
class Readonly < Base | |
def execute(sql, name=nil) | |
if sql =~ /^(SELECT|SET|SHOW|DESCRIBE)\b/ | |
super sql, name | |
else | |
Rails.logger.warn "\e[31m #{name} (BLOCKED) #{sql}\e[0m" | |
end | |
end | |
end | |
end | |
Arproxy.configure do |config| | |
config.adapter = "mysql2" | |
config.use Arproxy::Readonly | |
end | |
Arproxy.enable! | |
Rails::Console.start(Rails.application) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment