{"id":20904707,"url":"https://github.com/sage/rake_helper","last_synced_at":"2025-05-13T05:30:49.529Z","repository":{"id":137383822,"uuid":"64628867","full_name":"Sage/rake_helper","owner":"Sage","description":"A set of common helper methods to DRY up Rails rake tasks","archived":true,"fork":false,"pushed_at":"2019-09-19T07:34:59.000Z","size":26,"stargazers_count":4,"open_issues_count":1,"forks_count":0,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-03-12T21:28:13.887Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Sage.png","metadata":{"files":{"readme":"README.md","changelog":"change_log/next/.gitkeep","contributing":null,"funding":null,"license":"LICENSE","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":"2016-08-01T02:23:52.000Z","updated_at":"2025-01-20T21:40:09.000Z","dependencies_parsed_at":null,"dependency_job_id":"4483b47e-fa9e-43d9-aaa9-2ffdc6192585","html_url":"https://github.com/Sage/rake_helper","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sage%2Frake_helper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sage%2Frake_helper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sage%2Frake_helper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sage%2Frake_helper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Sage","download_url":"https://codeload.github.com/Sage/rake_helper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253882728,"owners_count":21978541,"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":[],"created_at":"2024-11-18T13:18:35.086Z","updated_at":"2025-05-13T05:30:49.518Z","avatar_url":"https://github.com/Sage.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# rake_helper [![Build Status](https://travis-ci.org/Sage/rake_helper.svg?branch=master)](https://travis-ci.org/Sage/rake_helper) [![Maintainability](https://api.codeclimate.com/v1/badges/a0c3d698f12736c976f5/maintainability)](https://codeclimate.com/github/Sage/rake_helper/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/a0c3d698f12736c976f5/test_coverage)](https://codeclimate.com/github/Sage/rake_helper/test_coverage) [![Gem Version](https://badge.fury.io/rb/rake_helper.svg)](https://badge.fury.io/rb/rake_helper)\n\nA set of common helper methods to DRY up Rails rake tasks\n\n## Installation\n\nAdd to your project's Gemfile:\n```ruby\ngem 'rake_helper'\n```\n\nRun:\n```\nbundle install\n```\n\nInclude in your project's Rakefile:\n```ruby\ninclude RakeHelper\n```\n\n## Usage\n\n### Logging Messages\n\nThese output a timestamped `puts` statement in the terminal for the benefit \nof the person running the rake task, but also log a message in the Rails \nlog in case it is missed in a long stream of output.\n\nThere are 3 predefined methods which prepend a standardized keyword:\n\n#### start\nLogs as type `:info`\n```ruby\nstart('Updating user records')\n# =\u003e 2016-08-07 13:02:51 -0400 START: Updating user records\n```\n\n#### finish\nLogs as type `:info`\n```ruby\nfinish('Updating user records')\n# =\u003e 2016-08-07 13:04:28 -0400 FINISH: Updating user records\n```\n\n#### failure\nLogs as type `:error`\n```ruby\nfailure(\"Updating user records: #{e}\")\n# =\u003e 2016-08-07 13:03:16 -0400 FAILURE: Updating user records: SomeError\n```\n\nThe standard logger severities can also be called directly as methods with a\nsingle `message` parameter:\n* `warn`\n* `unknown`\n* `info`\n* `fatal`\n* `debug`\n* `error`\n\nFor example: \n```ruby\ninfo('Useful information')\n# =\u003e 2016-08-07 13:03:16 -0400 Useful information\n```\n\n### Running SQL Statements\n\nThe `run_sql` method will execute one or more SQL statements and return the\nresults as an `Array`. The first param is required and should be a valid SQL\nstring. The string can include several statements separated by semicolons.\nYou can pass an `action` option which should be any valid\n[ActiveRecord::ConnectionAdapters::DatabaseStatements](http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html)\nmethod name as a symbol.\n\n#### :update example\n```ruby\nsql = \u003c\u003c-SQL\n  UPDATE users SET activated = 1 WHERE created_at \u003e 2016-01-01;\n  UPDATE businesses SET name = 'Widgets Inc' WHERE id = 22; \nSQL\n\nrun_sql(sql)\n\n# OR capturing the record count for each query:\n\nresults = run_sql(sql, action: :update)\ninfo(\"User count: #{results.first}, Business count: #{results.last}\")\n# =\u003e 2016-08-07 13:11:45 -0400 \"User count: 10, Business count: 1\"\n```\n\n#### :select_value example\n```ruby\nsql = \u003c\u003c-SQL\n  SELECT id FROM users WHERE email = 'bob@example.com'; \nSQL\n\nresults = run_sql(sql, action: :select_value)\ninfo(\"Bob's ID: #{results.first}\")\n# =\u003e 2016-08-07 13:11:45 -0400 \"Bob's ID: 15\"\n```\n\n#### :delete example\n```ruby\nsql = \u003c\u003c-SQL\n  DELETE FROM users WHERE created_at \u003c 2016-01-01 AND activated = 0; \nSQL\n\nrun_sql(sql)\n\n# OR capturing the record count for each query:\n\nresults = run_sql(sql, action: :delete)\ninfo(\"Num users deleted: #{results.first}\")\n# =\u003e 2016-08-07 13:11:45 -0400 \"Num users deleted: 52\" \n```\n\n### Full Example\n\n#### Rake File\n```ruby\n# lib/tasks/update_locales.rake\n\ndesc 'Update US locales to en-US'\ntask update_locales: :environment do\n  message = 'Updating US locales'\n  start(message)\n\n  sql = \u003c\u003c-SQL\n    UPDATE users SET locale = 'en-US' WHERE locale IS NULL OR locale = 'en';\n    UPDATE countries SET locale = 'en-US' where code = 'US';\n  SQL\n\n  begin\n    run_sql(sql)\n    finish(message)\n  rescue Exception =\u003e e\n    failure(e)\n  end\nend\n```\n\n#### Successful Run\n```sh\n$ bundle exec rake update_locales\n2016-08-07 13:57:56 -0400 START: Updating US locales\n2016-08-07 13:57:56 -0400 FINISH: Updating US locales\n```\n\n#### Failed Run\n```sh\n$ bundle exec rake update_locales\n2016-08-07 14:07:50 -0400 START: Updating US locales\n2016-08-07 14:07:50 -0400 FAILURE: ActiveRecord::ConnectionTimeoutError\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsage%2Frake_helper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsage%2Frake_helper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsage%2Frake_helper/lists"}