{"id":13878045,"url":"https://github.com/fatkodima/columns_trace","last_synced_at":"2025-04-09T12:09:42.657Z","repository":{"id":198144392,"uuid":"700333747","full_name":"fatkodima/columns_trace","owner":"fatkodima","description":"Detect unnecessary selected database columns","archived":false,"fork":false,"pushed_at":"2024-09-05T17:55:47.000Z","size":44,"stargazers_count":113,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-02T03:58:49.390Z","etag":null,"topics":["gem","performance","rails","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fatkodima.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-10-04T11:58:48.000Z","updated_at":"2025-02-27T18:16:28.000Z","dependencies_parsed_at":null,"dependency_job_id":"a88954cf-1138-45cf-a1d5-31fa65806d5e","html_url":"https://github.com/fatkodima/columns_trace","commit_stats":{"total_commits":24,"total_committers":2,"mean_commits":12.0,"dds":0.04166666666666663,"last_synced_commit":"76cdd86a78d99b8001e1bfd23fe36946a2d578c6"},"previous_names":["fatkodima/columns_trace"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fatkodima%2Fcolumns_trace","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fatkodima%2Fcolumns_trace/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fatkodima%2Fcolumns_trace/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fatkodima%2Fcolumns_trace/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fatkodima","download_url":"https://codeload.github.com/fatkodima/columns_trace/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248036067,"owners_count":21037092,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gem","performance","rails","ruby"],"created_at":"2024-08-06T08:01:38.300Z","updated_at":"2025-04-09T12:09:42.637Z","avatar_url":"https://github.com/fatkodima.png","language":"Ruby","readme":"# ColumnsTrace\n\n[![Build Status](https://github.com/fatkodima/columns_trace/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/fatkodima/columns_trace/actions/workflows/test.yml)\n\nDetects unnecessary selected database columns in Rails controllers, `ActiveJob` and `Sidekiq` jobs.\n\n## Requirements\n\n- ruby 2.7+\n- rails 6.0+\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'columns_trace'\n```\n\nAnd then run:\n\n```sh\n$ bundle install\n```\n\n## Usage\n\nHit a controller or email action or run `ActiveJob` (or `Sidekiq`) job, open `log/columns_trace.log`,\nand see the output:\n\n```\nImportsController#create\n  1 User record: unused columns - \"bio\", \"settings\"; used columns - \"id\", \"email\", \"name\",\n  \"account_id\", \"created_at\", \"updated_at\"\n  ↳ app/controllers/application_controller.rb:32:in `block in \u003cclass:ApplicationController\u003e'\n\n  1 Account record: unused columns - \"settings\", \"logo\", \"updated_at\";\n  used columns - \"id\", \"plan_id\"\n  ↳ app/controllers/application_controller.rb:33:in `block in \u003cclass:ApplicationController\u003e'\n\n  10 Project records: unused columns - \"description\", \"avatar\", \"url\", \"created_at\", \"updated_at\";\n  used columns - \"id\", \"user_id\"\n  ↳ app/models/user.rb:46: in `projects'\n    app/services/imports_service.rb:129: in `import_projects'\n    app/controllers/imports_controller.rb:49:in `index'\n\nImportProjectJob\n  1 User record: unused columns - \"email\", \"name\", \"bio\", \"created_at\", \"updated_at\";\n  used columns - \"id\", \"settings\"\n  ↳ app/jobs/import_project_job.rb:23:in `perform'\n\n  1 Project record: unused columns - \"description\", \"avatar\", \"settings\", \"created_at\",\n  \"updated_at\"; used columns - \"id\", \"user_id\", \"url\"\n  ↳ app/jobs/import_project_job.rb:24:in `perform'\n```\n\n### Tracing custom code\n\nTo get columns usage in the custom code, you can manually wrap it by `ColumnsTrace.report`:\n\n```ruby\ntask my_rake_task: :environment do\n  ColumnsTrace.report(\"my_rake_task\") do\n    # do stuff\n  end\nend\n```\n\n## Configuration\n\nYou can override the following default options:\n\n```ruby\n# config/initializers/columns_trace.rb\n\nColumnsTrace.configure do |config|\n  # Configures models that will be ignored.\n  # Always adds Rails' internal `ActiveRecord::SchemaMigration`\n  # and `ActiveRecord::InternalMetadata` models by default.\n  config.ignored_models = []\n\n  # Configures columns that will be ignored.\n  #\n  # Global setting\n  #   config.ignored_columns = [:updated_at]\n  # Per-model setting\n  #   config.ignored_columns = [:updated_at, { User =\u003e :admin }]\n  config.ignored_columns = []\n\n  # The reporter that is used for reporting.\n  # Defaults to log reporter that outputs to `log/columns_trace.log` file\n  # when inside a Rails application.\n  config.reporter = nil\n\n  # Controls the contents of the printed backtrace.\n  # Is set to the default Rails.backtrace_cleaner when the gem is used in the Rails app.\n  config.backtrace_cleaner = -\u003e(backtrace) { backtrace }\nend\n```\n\n`Sidekiq` integration is disabled by default. You need to explicitly enable it:\n\n```ruby\n# config/initializers/columns_trace.rb\n\nColumnsTrace.enable_sidekiq_tracing!\n```\n\n### Custom reporters\n\nBy default offenses are reported to a log reporter that outputs to `log/columns_trace.log` file\nwhen inside a Rails application.\n\nYou can set your custom reporter by defining a class responding to `#report` method.\n\n```ruby\nclass MyReporter\n  def report(title, created_records)\n    title # =\u003e \"controller#action\"\n    created_records # =\u003e [#\u003cColumnsTrace::CreatedRecord\u003e]\n    created_records.each do |record|\n      record.model # class of ActiveRecord model\n      record.accessed_fields  # array of accessed fields\n      record.unused_fields # array of unused fields\n      record.backtrace # array of strings\n      record.record # ActiveRecord model instance\n    end\n  end\nend\n\nColumnsTrace.reporter = MyReporter.new\n```\n\n## Development\n\nAfter checking out the repo, run `bundle install` to install dependencies. Then, run `rake` to run the linter and tests.\n\nTo 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).\n\n## Additional resources\n\nAlternatives:\n\n- [snip_snip](https://github.com/kddnewton/snip_snip) - archived, supports only controllers\n\nInteresting reads:\n\n- [Reasons why SELECT * is bad for SQL performance](https://tanelpoder.com/posts/reasons-why-select-star-is-bad-for-sql-performance/)\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/fatkodima/columns_trace.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","funding_links":[],"categories":["Ruby","Gems"],"sub_categories":["Performance Optimization"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffatkodima%2Fcolumns_trace","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffatkodima%2Fcolumns_trace","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffatkodima%2Fcolumns_trace/lists"}