├── .github └── workflows │ ├── licensing.yml │ └── main.yml ├── .gitignore ├── .rubocop.yml ├── CODE_OF_CONDUCT.md ├── Gemfile ├── Gemfile.lock ├── LICENSE.txt ├── README.md ├── Rakefile ├── activerecord-sql_commenter.gemspec ├── bin ├── console └── setup ├── doc └── dependency_decisions.yml ├── lib └── active_record │ ├── sql_commenter.rb │ └── sql_commenter │ ├── query_logs_formatter.rb │ ├── query_logs_tags_format.rb │ └── version.rb └── test ├── sql_commenter_test.rb └── test_helper.rb /.github/workflows/licensing.yml: -------------------------------------------------------------------------------- 1 | name: Verify dependency licenses 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | types: 9 | - opened 10 | - reopened 11 | - synchronize 12 | 13 | jobs: 14 | licensing: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - uses: actions/checkout@v2 18 | with: 19 | fetch-depth: 0 20 | - run: sudo gem install license_finder 21 | - run: license_finder 22 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: Ruby 2 | 3 | on: [push,pull_request] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v2 10 | - name: Set up Ruby 11 | uses: ruby/setup-ruby@v1 12 | with: 13 | ruby-version: 3.1.2 14 | bundler-cache: true 15 | - name: Run tests 16 | run: bundle exec rake test 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /_yardoc/ 4 | /coverage/ 5 | /pkg/ 6 | /spec/reports/ 7 | /tmp/ 8 | 9 | # rspec failure tracking 10 | .rspec_status 11 | -------------------------------------------------------------------------------- /.rubocop.yml: -------------------------------------------------------------------------------- 1 | AllCops: 2 | TargetRubyVersion: 2.4 3 | 4 | Style/StringLiterals: 5 | Enabled: true 6 | EnforcedStyle: double_quotes 7 | 8 | Style/StringLiteralsInInterpolation: 9 | Enabled: true 10 | EnforcedStyle: double_quotes 11 | 12 | Layout/LineLength: 13 | Max: 120 14 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community. 8 | 9 | ## Our Standards 10 | 11 | Examples of behavior that contributes to a positive environment for our community include: 12 | 13 | * Demonstrating empathy and kindness toward other people 14 | * Being respectful of differing opinions, viewpoints, and experiences 15 | * Giving and gracefully accepting constructive feedback 16 | * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience 17 | * Focusing on what is best not just for us as individuals, but for the overall community 18 | 19 | Examples of unacceptable behavior include: 20 | 21 | * The use of sexualized language or imagery, and sexual attention or 22 | advances of any kind 23 | * Trolling, insulting or derogatory comments, and personal or political attacks 24 | * Public or private harassment 25 | * Publishing others' private information, such as a physical or email 26 | address, without their explicit permission 27 | * Other conduct which could reasonably be considered inappropriate in a 28 | professional setting 29 | 30 | ## Enforcement Responsibilities 31 | 32 | Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful. 33 | 34 | Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate. 35 | 36 | ## Scope 37 | 38 | This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. 39 | 40 | ## Enforcement 41 | 42 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at coutermarsh.mike@gmail.com. All complaints will be reviewed and investigated promptly and fairly. 43 | 44 | All community leaders are obligated to respect the privacy and security of the reporter of any incident. 45 | 46 | ## Enforcement Guidelines 47 | 48 | Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct: 49 | 50 | ### 1. Correction 51 | 52 | **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community. 53 | 54 | **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested. 55 | 56 | ### 2. Warning 57 | 58 | **Community Impact**: A violation through a single incident or series of actions. 59 | 60 | **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban. 61 | 62 | ### 3. Temporary Ban 63 | 64 | **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior. 65 | 66 | **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban. 67 | 68 | ### 4. Permanent Ban 69 | 70 | **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. 71 | 72 | **Consequence**: A permanent ban from any sort of public interaction within the community. 73 | 74 | ## Attribution 75 | 76 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0, 77 | available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. 78 | 79 | Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity). 80 | 81 | [homepage]: https://www.contributor-covenant.org 82 | 83 | For answers to common questions about this code of conduct, see the FAQ at 84 | https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations. 85 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | source "https://rubygems.org" 4 | 5 | # Specify your gem's dependencies in activerecord-sql_commenter.gemspec 6 | gemspec 7 | 8 | gem "rake", "~> 13.0" 9 | 10 | gem "minitest", "~> 5.8" 11 | 12 | gem "rubocop", "~> 1.7" 13 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | activerecord-sql_commenter (0.1.0) 5 | activerecord (~> 7.0.0) 6 | activesupport (~> 7.0.0) 7 | 8 | GEM 9 | remote: https://rubygems.org/ 10 | specs: 11 | activemodel (7.0.3) 12 | activesupport (= 7.0.3) 13 | activerecord (7.0.3) 14 | activemodel (= 7.0.3) 15 | activesupport (= 7.0.3) 16 | activesupport (7.0.3) 17 | concurrent-ruby (~> 1.0, >= 1.0.2) 18 | i18n (>= 1.6, < 2) 19 | minitest (>= 5.1) 20 | tzinfo (~> 2.0) 21 | ast (2.4.2) 22 | concurrent-ruby (1.1.10) 23 | i18n (1.10.0) 24 | concurrent-ruby (~> 1.0) 25 | minitest (5.15.0) 26 | parallel (1.22.1) 27 | parser (3.1.2.0) 28 | ast (~> 2.4.1) 29 | rainbow (3.1.1) 30 | rake (13.0.6) 31 | regexp_parser (2.5.0) 32 | rexml (3.2.5) 33 | rubocop (1.30.1) 34 | parallel (~> 1.10) 35 | parser (>= 3.1.0.0) 36 | rainbow (>= 2.2.2, < 4.0) 37 | regexp_parser (>= 1.8, < 3.0) 38 | rexml (>= 3.2.5, < 4.0) 39 | rubocop-ast (>= 1.18.0, < 2.0) 40 | ruby-progressbar (~> 1.7) 41 | unicode-display_width (>= 1.4.0, < 3.0) 42 | rubocop-ast (1.18.0) 43 | parser (>= 3.1.1.0) 44 | ruby-progressbar (1.11.0) 45 | sqlite3 (1.4.2) 46 | tzinfo (2.0.4) 47 | concurrent-ruby (~> 1.0) 48 | unicode-display_width (2.1.0) 49 | 50 | PLATFORMS 51 | x86_64-darwin-20 52 | x86_64-linux 53 | 54 | DEPENDENCIES 55 | activerecord-sql_commenter! 56 | minitest (~> 5.8) 57 | rake (~> 13.0) 58 | rubocop (~> 1.7) 59 | sqlite3 60 | 61 | BUNDLED WITH 62 | 2.2.21 63 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2022 PlanetScale, Inc. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ActiveRecord SqlCommenter 2 | [![Gem Version](https://badge.fury.io/rb/activerecord-sql_commenter.svg)](https://badge.fury.io/rb/activerecord-sql_commenter) 3 | 4 | Transform your Rail's query comments into [SqlCommenter](https://google.github.io/sqlcommenter/) format. 5 | 6 | SqlCommenter is compatible with [PlanetScale's Query Insights](https://planetscale.com/docs/concepts/query-insights). 7 | 8 | **Before:** 9 | ```sql 10 | select * from users /*application:apibb,controller:database_info,action:show*/ 11 | ``` 12 | 13 | **After:** 14 | ```sql 15 | select * from users /*application='apibb',controller='database_info',action='show'*/ 16 | ``` 17 | 18 | ## Installation 19 | 20 | Add this line to your application's Gemfile: 21 | 22 | ```ruby 23 | gem "activerecord-sql_commenter", require: "active_record/sql_commenter" 24 | ``` 25 | 26 | And then execute: 27 | 28 | $ bundle install 29 | 30 | ## Enabling Query Log Tags 31 | In your Rails application, add the following to `config/application.rb`. 32 | 33 | ```ruby 34 | config.active_record.query_log_tags_enabled = true 35 | ``` 36 | 37 | You can define which tags are added to queries. 38 | 39 | ```ruby 40 | config.active_record.query_log_tags = [ :application, :controller, :action, :job ] 41 | ``` 42 | 43 | Tags can also be cached per request or job execution. 44 | 45 | ```ruby 46 | config.active_record.cache_query_log_tags = true 47 | ``` 48 | 49 | To learn more, see the [full documentation here](https://api.rubyonrails.org/classes/ActiveRecord/QueryLogs.html). 50 | 51 | ## Development 52 | 53 | After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. 54 | 55 | 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org). 56 | 57 | ## Contributing 58 | 59 | Bug reports and pull requests are welcome on GitHub at https://github.com/planetscale/activerecord-sql_commenter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/mscoutermarsh/activerecord-sql_commenter/blob/main/CODE_OF_CONDUCT.md). 60 | 61 | ## License 62 | 63 | The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). 64 | 65 | ## Code of Conduct 66 | 67 | Everyone interacting in the ActiveRecord::SqlCommenter project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/mscoutermarsh/activerecord-sql_commenter/blob/main/CODE_OF_CONDUCT.md). 68 | 69 | ## Thanks ❤️ 70 | Thank you to @modulitos for the [original implementation contributed to Marginalia](https://github.com/basecamp/marginalia/pull/130). 71 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'rake/testtask' 4 | require 'bundler/gem_tasks' 5 | 6 | Rake::TestTask.new do |t| 7 | t.pattern = "test/*_test.rb" 8 | end 9 | 10 | task default: %i[test] 11 | -------------------------------------------------------------------------------- /activerecord-sql_commenter.gemspec: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require_relative "lib/active_record/sql_commenter/version" 4 | 5 | Gem::Specification.new do |spec| 6 | spec.name = "activerecord-sql_commenter" 7 | spec.version = ActiveRecord::SqlCommenter::VERSION 8 | spec.authors = ["Mike Coutermarsh", "Iheanyi Ekechukwu"] 9 | spec.email = ["coutermarsh.mike@gmail.com"] 10 | 11 | spec.summary = "Make ActiveRecord query logging compatible with sqlcommenter." 12 | spec.description = "This gem changes ActiveRecord's query logs to be compatible with the sqlcommenter format." 13 | spec.homepage = "https://github.com/planetscale/activerecord-sql_commenter" 14 | spec.license = "MIT" 15 | spec.required_ruby_version = ">= 2.4.0" 16 | 17 | spec.metadata["allowed_push_host"] = "https://rubygems.org" 18 | 19 | spec.metadata["homepage_uri"] = spec.homepage 20 | spec.metadata["source_code_uri"] = "https://github.com/planetscale/activerecord-sql_commenter" 21 | 22 | # Specify which files should be added to the gem when it is released. 23 | # The `git ls-files -z` loads the files in the RubyGem that have been added into git. 24 | spec.files = Dir.chdir(File.expand_path(__dir__)) do 25 | `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) } 26 | end 27 | spec.bindir = "exe" 28 | spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } 29 | spec.require_paths = ["lib"] 30 | 31 | spec.add_dependency "activerecord", "~> 7.0.0" 32 | spec.add_dependency "activesupport", "~> 7.0.0" 33 | spec.add_development_dependency "sqlite3" 34 | end 35 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # frozen_string_literal: true 3 | 4 | require "bundler/setup" 5 | require "activerecord/sql_commenter" 6 | 7 | # You can add fixtures and/or initialization code here to make experimenting 8 | # with your gem easier. You can also use a different console, if you like. 9 | 10 | # (If you use this, don't forget to add pry to your Gemfile!) 11 | # require "pry" 12 | # Pry.start 13 | 14 | require "irb" 15 | IRB.start(__FILE__) 16 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | set -vx 5 | 6 | bundle install 7 | 8 | # Do any other automated setup that you need to do here 9 | -------------------------------------------------------------------------------- /doc/dependency_decisions.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - - :permit 3 | - MIT 4 | - :who: 5 | :why: 6 | :versions: [] 7 | :when: 2022-06-13 16:22:04.433565000 Z 8 | - - :approve 9 | - sqlite3 10 | - &1 11 | :who: 12 | :why: 13 | :versions: [] 14 | :when: 2022-06-13 16:26:54.044527000 Z 15 | - - :approve 16 | - New BSD 17 | - *1 18 | - - :approve 19 | - rexml 20 | - &2 21 | :who: 22 | :why: 23 | :versions: [] 24 | :when: 2022-06-13 16:27:09.213502000 Z 25 | - - :approve 26 | - Simplified BSD 27 | - *2 28 | -------------------------------------------------------------------------------- /lib/active_record/sql_commenter.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "active_support/lazy_load_hooks" 4 | require_relative "sql_commenter/version" 5 | require_relative "sql_commenter/query_logs_tags_format" 6 | 7 | ActiveSupport.on_load :active_record do 8 | ActiveRecord::QueryLogs.include(ActiveRecord::SqlCommenter::QueryLogsTagsFormat) 9 | end 10 | -------------------------------------------------------------------------------- /lib/active_record/sql_commenter/query_logs_formatter.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module ActiveRecord 4 | module SqlCommenter 5 | module QueryLogs 6 | class Formatter 7 | attr_reader :key_value_separator 8 | 9 | def initialize 10 | @key_value_separator = "=" 11 | end 12 | 13 | # @param [string] value 14 | # @return [String] The formatted value that will be used in our key-value 15 | # pairs. 16 | def quote_value(value) 17 | "'#{value.gsub("'", "\\\\'")}'" 18 | end 19 | end 20 | end 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /lib/active_record/sql_commenter/query_logs_tags_format.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "active_support/core_ext/module/attribute_accessors_per_thread" 4 | require_relative "query_logs_formatter" 5 | 6 | module ActiveRecord 7 | module SqlCommenter 8 | module QueryLogsTagsFormat 9 | def self.included(klass) 10 | klass.class_eval do 11 | thread_mattr_accessor(:tags_formatter, instance_accessor: false) 12 | 13 | class << self 14 | private 15 | 16 | def formatter 17 | self.tags_formatter ||= QueryLogs::Formatter.new 18 | end 19 | 20 | undef tag_content 21 | def tag_content 22 | context = ActiveSupport::ExecutionContext.to_h 23 | 24 | tags.flat_map { |i| [*i] }.filter_map do |tag| 25 | key, handler = tag 26 | handler ||= taggings[key] 27 | 28 | val = if handler.nil? 29 | context[key] 30 | elsif handler.respond_to?(:call) 31 | if handler.arity.zero? 32 | handler.call 33 | else 34 | handler.call(context) 35 | end 36 | else 37 | handler 38 | end 39 | "#{key}#{formatter.key_value_separator}#{formatter.quote_value(val)}" unless val.nil? 40 | end.join(",") 41 | end 42 | end 43 | end 44 | end 45 | end 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /lib/active_record/sql_commenter/version.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module ActiveRecord 4 | module SqlCommenter 5 | VERSION = "0.1.0" 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /test/sql_commenter_test.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require_relative "../lib/active_record/sql_commenter" 4 | 5 | require "minitest/autorun" 6 | require "active_record" 7 | 8 | ActiveRecord::Base.establish_connection( 9 | adapter: "sqlite3", 10 | database: ":memory:" 11 | ) 12 | 13 | class SqlCommenterTest < Minitest::Test 14 | def test_comment 15 | ActiveRecord::QueryLogs.tags = [test_value: -> { "test" }] 16 | assert_equal "/*test_value='test'*/", ActiveRecord::QueryLogs.call("") 17 | end 18 | 19 | def test_escapes_values 20 | ActiveRecord::QueryLogs.tags = [test_value: -> { "'test'" }] 21 | assert_equal "/*test_value='\\'test\\''*/", ActiveRecord::QueryLogs.call("") 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "activerecord/sql_commenter" 4 | --------------------------------------------------------------------------------