patches 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2efd0dc04504ab68ece9ffb46f6e1c2a6adcae93b39dac5824d4cfed34093a99
4
+ data.tar.gz: 167f4f394fea327cbdf85f9c3e1ac067639abdfd606b2077ec70e5b1bb0a4bbb
5
+ SHA512:
6
+ metadata.gz: 90edf98dc27bdeb176f8075711a2703cad55853ef010ce99b908a63001739d58dfab1ccc002e5ce61e4bbfe0724b4ca08321037b673c20fe529e76bc5a58f535
7
+ data.tar.gz: 666b762d233f27259f8c33afe2cf2ddb4cff060d313de6795840fac767e14e46401e1c5e8ff321379b4d9f9dbeea4bd00704656e03942ac312bdb3fea13f64a4
@@ -0,0 +1,24 @@
1
+ steps:
2
+ - label: ':hammer: Tests'
3
+ plugins:
4
+ docker-compose:
5
+ run: app
6
+ command: 'bundle exec rspec'
7
+ agents:
8
+ queue: docker-heavy
9
+ artifact_paths: 'coverage/.resultset.json'
10
+
11
+ - wait
12
+
13
+ - command: "Report CodeClimate Coverage"
14
+ label: ":codeclimate: Report coverage"
15
+ plugins:
16
+ jobready/codeclimate-test-reporter#v2.0:
17
+ prefix: /app
18
+ artifact: "coverage/.resultset*.json"
19
+ input_type: simplecov
20
+ parts: 1
21
+ env:
22
+ CC_TEST_REPORTER_ID:
23
+ agents:
24
+ queue: docker-heavy
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /coverage/
11
+ /test.db
12
+ /*.gem
13
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,42 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
+ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [3.4.0] - 2020-07-21
10
+ ### Added
11
+ - `Patches::TenantWorker` application version constraint forward compatibility
12
+
13
+ ## [3.3.0] - 2020-07-20
14
+ ### Added
15
+ - Application version constraints
16
+
17
+ ## [3.2.0] - 2020-07-16
18
+ ### Added
19
+ - Added `Patches::Worker` extra parameters to support forward compatibility with the upcoming releases
20
+
21
+ ## [3.1.0] - 2019-11-25
22
+ ### Fixed
23
+ - Gem compatibility with Apartment 2
24
+
25
+ ## [3.0.1] - 2018-11-19
26
+ ### Added
27
+ - Set icon_emoji of posted slack message to :dog:
28
+
29
+ ## [3.0.0] - 2018-11-19
30
+ ### Removed
31
+ - Hipchat is no longer supported
32
+
33
+ ## [2.4.1] - 2018-09-19
34
+ ### Changed
35
+ - Corrected gem ownership and authors.
36
+ ### Added
37
+ - Changelog
38
+ - Dockerfile and BuildKite pipeline config
39
+
40
+ ## [2.4.0] - 2018-09-17
41
+ ### Added
42
+ - Added slack notification configurability
@@ -0,0 +1,9 @@
1
+ FROM ruby:2.3
2
+
3
+ RUN touch ~/.gemrc && echo "gem: --no-ri --no-rdoc" >> ~/.gemrc
4
+
5
+ WORKDIR /app/
6
+ ADD . /app/
7
+ RUN bundle install
8
+
9
+ CMD bundle exec rake -T
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem "rails", "~> 4.1"
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2015 JobReady
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
20
+
21
+ https://opensource.org/licenses/MIT
@@ -0,0 +1,52 @@
1
+ # Patches
2
+ [![Build status](https://badge.buildkite.com/4f3df3f3458bcc933dc44cab6c136af5c3bbdd9f761f1a99ff.svg)](https://buildkite.com/jobready/patches)
3
+ [![Maintainability](https://api.codeclimate.com/v1/badges/39d142050017ffeb2564/maintainability)](https://codeclimate.com/repos/557f93b76956807f81000001/maintainability)
4
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/39d142050017ffeb2564/test_coverage)](https://codeclimate.com/repos/557f93b76956807f81000001/test_coverage)
5
+ [![Gem Version](https://badge.fury.io/rb/patches.svg)](https://badge.fury.io/rb/patches)
6
+
7
+ ![patches](docs/patches.jpg)
8
+
9
+
10
+ A simple gem for one off tasks
11
+
12
+ ## Version 2.0
13
+
14
+ Please note the breaking change release around deployment. See docs/usage.md for the full change.
15
+
16
+ TL;DR You need to manually declare the patches task to run in your deploy.rb
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ ```ruby
23
+ gem 'patches'
24
+ ```
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install patches
32
+
33
+ ## Usage
34
+
35
+ see `docs/usage.md`
36
+
37
+ ## Development
38
+
39
+ ```
40
+ docker-compose build
41
+ docker-compose run app bundle exec rspec
42
+ ```
43
+
44
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org/gems/patches).
45
+
46
+ ## Contributing
47
+
48
+ 1. Fork it ( https://github.com/jobready/patches/fork )
49
+ 2. Create your feature branch (`git checkout -b feature/my-feature-name`)
50
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
51
+ 4. Push to the branch (`git push origin feature/my-new-feature`)
52
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "patches"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,10 @@
1
+ class CreatePatch < ActiveRecord::Migration
2
+ def change
3
+ create_table :patches_patches do |t|
4
+ t.string :path, null: false
5
+ t.timestamps
6
+ end
7
+
8
+ add_index :patches_patches, :path, unique: true
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ version: '3.2'
2
+ services:
3
+ app:
4
+ build: .
5
+ volumes:
6
+ - .:/app
7
+ - app-gems:/usr/local/bundle
8
+
9
+ volumes:
10
+ app-gems:
11
+ driver: local
Binary file
@@ -0,0 +1,136 @@
1
+ # Usage
2
+
3
+ ## Initial Setup
4
+
5
+ Add patches to the project Gemfile
6
+
7
+ ```ruby
8
+ gem 'patches', '~> 2.4.0'
9
+ ```
10
+
11
+ Install the database migration
12
+
13
+ ```bash
14
+ bundle exec rake patches:install:migrations
15
+ ```
16
+
17
+ Migrate database
18
+
19
+ ```bash
20
+ bundle exec rake db:migrate
21
+ ```
22
+
23
+ ## Configuration
24
+
25
+ If you would like to run the patches asynchronously, or would like them to notify
26
+ your Slack channel when they fail or succeed, you need to set up
27
+ an initializer to set those options.
28
+
29
+ ```ruby
30
+ Patches::Config.configure do |config|
31
+ config.use_sidekiq = true
32
+
33
+ config.use_slack = true
34
+ config.slack_options = {
35
+ webhook_url: ENV['SLACK_WEBHOOK_URL'],
36
+ channel: ENV['SLACK_CHANNEL'],
37
+ username: ENV['SLACK_USER']
38
+ }
39
+ end
40
+ ```
41
+
42
+ ### Running patches in parallel for tenants
43
+
44
+ If you are using the Apartment gem, you can run the patches for each tenant in parallel.
45
+ Just set the config ```sidekiq_parallel``` to ```true``` and you're good to go.
46
+
47
+ ```ruby
48
+ Patches::Config.configure do |config|
49
+ config.use_sidekiq = true
50
+ config.sidekiq_parallel = true
51
+ end
52
+ ```
53
+
54
+ *Note:* Make sure your sidekiq queue is able to process concurrent jobs.
55
+ You can use ```config.sidekiq_options``` to customise it.
56
+
57
+ ### Application version verification
58
+
59
+ In environments where a rolling update of sidekiq workers is performed during the deployment, multiple versions of the application run at the same time. If a Patches job is scheduled by the new application version during the rolling update, there is a possibility that it can be executed by the old application version, which will not have all the required patch files.
60
+
61
+ To prevent this case, set the application version in the config:
62
+
63
+ ```ruby
64
+ Patches::Config.configure do |config|
65
+ revision_file_path = Rails.root.join('REVISION')
66
+
67
+ if File.exist?(revision_file_path)
68
+ config.application_version = File.read(revision_file_path)
69
+ config.retry_after_version_mismatch_in = 1.minute
70
+ end
71
+ end
72
+ ```
73
+
74
+ ## Creating Patches
75
+
76
+ Generate a patch
77
+
78
+ ```
79
+ bundle exec rails g patches:patch PreferenceUpdate
80
+ ```
81
+
82
+ Which will result in a file like below
83
+
84
+ ```ruby
85
+ class PreferenceUpdate < Patches::Base
86
+ def run
87
+ # Code goes here
88
+ end
89
+ end
90
+ ```
91
+
92
+ update the run method and then execute
93
+
94
+ Generate patch with specs
95
+
96
+ ```bash
97
+ bundle exec rails g patches:patch PreferenceUpdate --specs=true
98
+ ```
99
+
100
+
101
+ ```bash
102
+ bundle exec rake patches:run
103
+ ```
104
+
105
+ Patches will only ever run once, patches will run in order of creation date.
106
+
107
+ To run patches on deployment using Capistrano, edit your Capfile and add
108
+
109
+ ```ruby
110
+ require 'patches/capistrano'
111
+ ```
112
+
113
+ And then in your deploy.rb
114
+
115
+ ```ruby
116
+ after 'last_task_you_want_to_run' 'patches:run'
117
+ ```
118
+
119
+ If you are using sidekiq and restarting the sidekiq process on the box
120
+ as a part of the deploy process, please make sure that the patches run task runs
121
+ after sidekiq restarts, otherwise there is no guarentee the tasks will run.
122
+
123
+ ## File Download
124
+
125
+ If a patch requires data assets, you could use S3 to store the file.
126
+ If credentials are defined in env vars, as per https://docs.aws.amazon.com/cli/latest/topic/config-vars.html#id1
127
+
128
+ ```ruby
129
+ require 'aws-sdk-s3'
130
+ Aws::S3::Client.new.get_object(bucket: @bucket_name, key: filename, response_target: destination)
131
+ ```
132
+
133
+ ## Multitenant
134
+
135
+ Patches will detect if `Apartment` gem is installed and if there are any tenants
136
+ and run the patches for each tenant
@@ -0,0 +1,22 @@
1
+ module Patches
2
+ module Generators
3
+ class PatchGenerator < Rails::Generators::NamedBase
4
+ desc 'Adds an empty patch'
5
+ source_root File.expand_path('../templates', __FILE__)
6
+
7
+ def generate_patch
8
+ template "patch.erb", "app/db/#{file_name}.rb"
9
+ end
10
+
11
+ private
12
+
13
+ def class_name
14
+ name.camelize
15
+ end
16
+
17
+ def file_name
18
+ name.underscore
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,26 @@
1
+ module Patches
2
+ module Generators
3
+ class PatchGenerator < Rails::Generators::NamedBase
4
+ desc 'Adds an empty patch'
5
+ source_root File.expand_path('../templates', __FILE__)
6
+
7
+ class_option :specs, type: :boolean, default: false, description: 'Generates a rspec file for the patch'
8
+
9
+ def generate_patch
10
+ template "patch.rb.erb", "db/patches/#{file_name}.rb"
11
+ template "patch_spec.rb.erb", "spec/patches/#{file_name}_spec.rb" if options['specs']
12
+ end
13
+
14
+ private
15
+
16
+ def class_name
17
+ name.camelize
18
+ end
19
+
20
+ def file_name
21
+ "#{Time.now.strftime('%Y%m%d%H%M')}_#{name.underscore}"
22
+ end
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,5 @@
1
+ class <%= class_name %> < Patches::Base
2
+ def run
3
+ # Code goes here
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ require 'rails_helper'
2
+ require_relative '../../db/patches/<%= file_name %>'
3
+
4
+ describe <%= class_name %> do
5
+ describe '#run' do
6
+ # Specs go here
7
+ end
8
+ end
@@ -0,0 +1,33 @@
1
+ require "patches/version"
2
+
3
+ module Patches
4
+ def self.default_path
5
+ Rails.root.join('db/patches/') if defined?(:Rails)
6
+ end
7
+
8
+ def self.class_name(path)
9
+ match = path.match(/\d+_(.+?)\.rb/)
10
+ match[1].camelcase if match
11
+ end
12
+
13
+ def self.logger
14
+ @logger ||= Logger.new(STDOUT)
15
+ end
16
+
17
+ def self.logger=(log)
18
+ @logger = log
19
+ end
20
+ end
21
+
22
+ require "patches/base"
23
+ require "patches/config"
24
+ require "patches/tenant_run_concern"
25
+ require "patches/application_version_validation"
26
+ require "patches/tenant_worker" if defined?(Sidekiq)
27
+ require "patches/engine" if defined?(Rails)
28
+ require "patches/patch"
29
+ require "patches/pending"
30
+ require "patches/runner"
31
+ require "patches/tenant_runner"
32
+ require "patches/notifier"
33
+ require "patches/worker" if defined?(Sidekiq)
@@ -0,0 +1,9 @@
1
+ module Patches
2
+ module ApplicationVersionValidation
3
+ def valid_application_version?(application_version)
4
+ return true unless application_version
5
+ return true unless Patches::Config.configuration.application_version
6
+ Patches::Config.configuration.application_version == application_version
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ class Patches::Base
2
+
3
+ # TODO: Use this :/
4
+ def self.tenant(name)
5
+ @tenant = name
6
+ end
7
+
8
+ def run
9
+ logger.warn("Run method not implemented for #{self.class.name}")
10
+ end
11
+
12
+ private
13
+ def execute(sql)
14
+ ActiveRecord::Base.connection.execute(sql)
15
+ end
16
+
17
+ def logger
18
+ Patches.logger
19
+ end
20
+ end
@@ -0,0 +1 @@
1
+ load File.expand_path("capistrano/tasks.rake", File.dirname(__FILE__))
@@ -0,0 +1,12 @@
1
+ namespace :patches do
2
+ desc 'Runs rake patches:run'
3
+ task :run do
4
+ on primary fetch(:migration_role) do
5
+ within release_path do
6
+ with rails_env: fetch(:rails_env) do
7
+ execute :rake, "patches:run"
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,53 @@
1
+ module Patches
2
+ class Config
3
+ class << self
4
+ def configuration
5
+ @configuration ||= Configuration.new
6
+ end
7
+
8
+ def configuration=(config)
9
+ @configuration = config
10
+ end
11
+
12
+ def configure
13
+ yield configuration
14
+ end
15
+
16
+ class Configuration
17
+ attr_accessor \
18
+ :application_version,
19
+ :retry_after_version_mismatch_in,
20
+ :sidekiq_options,
21
+ :sidekiq_parallel,
22
+ :sidekiq_queue,
23
+ :slack_options,
24
+ :use_sidekiq,
25
+ :use_slack
26
+
27
+ def initialize
28
+ @sidekiq_queue = 'default'
29
+ end
30
+
31
+ def sidekiq_options
32
+ @sidekiq_options ||= { retry: false, queue: sidekiq_queue }
33
+ end
34
+
35
+ def retry_after_version_mismatch_in
36
+ @retry_after_version_mismatch_in ||= 1.minute
37
+ end
38
+
39
+ def slack_channel
40
+ slack_options[:channel]
41
+ end
42
+
43
+ def slack_username
44
+ slack_options[:username]
45
+ end
46
+
47
+ def slack_webhook_url
48
+ slack_options[:webhook_url]
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,7 @@
1
+ require 'rails/engine'
2
+
3
+ module Patches
4
+ class Engine < Rails::Engine
5
+ isolate_namespace Patches
6
+ end
7
+ end
@@ -0,0 +1,52 @@
1
+ require 'slack-notifier'
2
+
3
+ class Patches::Notifier
4
+ class << self
5
+ def notify_success(patches)
6
+ send_slack_message(success_message(patches), 'good')
7
+ end
8
+
9
+ def notify_failure(patch_path, error)
10
+ send_slack_message(failure_message(patch_path, error), 'danger')
11
+ end
12
+
13
+ def success_message(patches)
14
+ message = "#{environment_prefix}#{patches.count} patches succeeded"
15
+ append_tenant_message(message)
16
+ end
17
+
18
+ def failure_message(patch_path, error)
19
+ details = "#{Pathname.new(patch_path).basename} failed with error: #{error}"
20
+ message = "#{environment_prefix}Error applying patch: #{details}"
21
+ append_tenant_message(message)
22
+ end
23
+
24
+ def environment_prefix
25
+ "[#{Rails.env.upcase}] " if defined?(Rails)
26
+ end
27
+
28
+ def append_tenant_message(message)
29
+ message = message + " for tenant: #{Apartment::Tenant.current}" if defined?(Apartment)
30
+ message
31
+ end
32
+
33
+ def send_slack_message(message, color)
34
+ return unless defined?(Slack) && config.use_slack
35
+
36
+ notifier = Slack::Notifier.new(
37
+ config.slack_webhook_url,
38
+ channel: config.slack_channel,
39
+ username: config.slack_username)
40
+
41
+ payload = { icon_emoji: ":dog:", attachments: [{ color: color, text: message }] }
42
+
43
+ notifier.post payload
44
+ end
45
+
46
+ private
47
+
48
+ def config
49
+ Patches::Config.configuration
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,9 @@
1
+ require 'active_record'
2
+
3
+ class Patches::Patch < ActiveRecord::Base
4
+ validates :path, presence: true
5
+
6
+ def self.path_lookup
7
+ Hash[pluck(:path, :created_at)]
8
+ end
9
+ end
@@ -0,0 +1,35 @@
1
+ class Patches::Pending
2
+ include Enumerable
3
+
4
+ attr_accessor :path
5
+
6
+ def initialize(path=nil)
7
+ @path = path || Patches.default_path
8
+ end
9
+
10
+ def each
11
+ return nil unless files
12
+ new_files = files.reject { |file| already_run?(file) }
13
+ Patches.logger.info("Patches found: #{new_files.join(',')}")
14
+
15
+ new_files.each do |file|
16
+ unless already_run?(file)
17
+ yield file
18
+ end
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def files
25
+ @files ||= Dir[File.join(path, "*.rb")].to_a.sort
26
+ end
27
+
28
+ def already_run?(path)
29
+ patches[File.basename(path)]
30
+ end
31
+
32
+ def patches
33
+ @patches ||= Patches::Patch.path_lookup
34
+ end
35
+ end
@@ -0,0 +1,53 @@
1
+ class Patches::Runner
2
+ attr_accessor :path
3
+
4
+ class UnknownPatch < StandardError; end
5
+
6
+ def initialize(path = nil)
7
+ @path = path || Patches.default_path
8
+ end
9
+
10
+ def perform
11
+ completed_patches = pending.each do |file_path|
12
+ klass = load_class(file_path)
13
+ instance = klass.new
14
+ Patches.logger.info("Running #{klass} from #{file_path}")
15
+ begin
16
+ instance.run
17
+ rescue => e
18
+ Patches::Notifier.notify_failure(file_path, format_exception(e))
19
+ raise e
20
+ end
21
+ complete!(patch_path(file_path))
22
+ end
23
+ Patches::Notifier.notify_success(completed_patches)
24
+ completed_patches
25
+ end
26
+
27
+ def complete!(patch_path)
28
+ Patches::Patch.create!(path: patch_path)
29
+ end
30
+
31
+ def patch_path(patch_path)
32
+ File.basename(patch_path)
33
+ end
34
+
35
+ def pending
36
+ @pending ||= Patches::Pending.new(path)
37
+ end
38
+
39
+ def format_exception(e)
40
+ "#{e.class.name}: #{e.message} (#{e.backtrace.first})"
41
+ end
42
+
43
+ private
44
+ def load_class(path)
45
+ begin
46
+ name = Patches.class_name(path)
47
+ load path
48
+ name.constantize
49
+ rescue StandardError, LoadError
50
+ raise UnknownPatch, "#{path} should define #{name}"
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,9 @@
1
+ module Patches
2
+ module TenantRunConcern
3
+ def run(tenant_name, path = nil)
4
+ Apartment::Tenant.switch(tenant_name) do
5
+ Patches::Runner.new(path).perform
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,30 @@
1
+ class Patches::TenantRunner
2
+ include Patches::TenantRunConcern
3
+ attr_accessor :path
4
+
5
+ def initialize(path: nil, tenants: nil)
6
+ @path = path
7
+ @tenants = tenants
8
+ end
9
+
10
+ def perform
11
+ Patches.logger.info("Patches tenant runner for: #{tenants.join(',')}")
12
+ tenants.each do |tenant|
13
+ if parallel?
14
+ Patches::TenantWorker.perform_async(tenant, path)
15
+ else
16
+ run(tenant, path)
17
+ end
18
+ end
19
+ end
20
+
21
+ def tenants
22
+ @tenants ||= (Apartment.tenant_names || [])
23
+ end
24
+
25
+ private
26
+
27
+ def parallel?
28
+ Patches::Config.configuration.sidekiq_parallel
29
+ end
30
+ end
@@ -0,0 +1,17 @@
1
+ require 'sidekiq'
2
+
3
+ class Patches::TenantWorker
4
+ include Sidekiq::Worker
5
+ include Patches::TenantRunConcern
6
+ include Patches::ApplicationVersionValidation
7
+
8
+ sidekiq_options Patches::Config.configuration.sidekiq_options
9
+
10
+ def perform(tenant_name, path, params = {})
11
+ if valid_application_version?(params['application_version'])
12
+ run(tenant_name, path)
13
+ else
14
+ self.class.perform_in(Patches::Config.configuration.retry_after_version_mismatch_in, tenant_name, path, params)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,6 @@
1
+ module Patches
2
+ MAJOR = 3
3
+ MINOR = 4
4
+ PATCH = 0
5
+ VERSION = [MAJOR, MINOR, PATCH].compact.join(".").freeze
6
+ end
@@ -0,0 +1,16 @@
1
+ require 'sidekiq'
2
+
3
+ class Patches::Worker
4
+ include Sidekiq::Worker
5
+ include Patches::ApplicationVersionValidation
6
+
7
+ sidekiq_options Patches::Config.configuration.sidekiq_options
8
+
9
+ def perform(runner, params = {})
10
+ if valid_application_version?(params['application_version'])
11
+ runner.constantize.new.perform
12
+ else
13
+ self.class.perform_in(Patches::Config.configuration.retry_after_version_mismatch_in, runner, params)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,29 @@
1
+ namespace :patches do
2
+ desc "Run Patches"
3
+ task :run => [:environment] do
4
+ if defined?(Apartment) && tenants.present?
5
+ runner = Patches::TenantRunner
6
+ else
7
+ runner = Patches::Runner
8
+ end
9
+
10
+ if defined?(Sidekiq) && Patches::Config.configuration.use_sidekiq
11
+ Patches::Worker.perform_async(
12
+ runner,
13
+ application_version: Patches::Config.configuration.application_version
14
+ )
15
+ else
16
+ runner.new.perform
17
+ end
18
+ end
19
+
20
+ def tenants
21
+ ENV['DB'] ? ENV['DB'].split(',').map { |s| s.strip } : Apartment.tenant_names || []
22
+ end
23
+
24
+ task :pending => [:environment] do
25
+ Patches::Pending.each do |patch|
26
+ puts patch
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'patches/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "patches"
8
+ spec.version = Patches::VERSION
9
+ spec.authors = ["JobReady"]
10
+ spec.email = ["ruby_gems@jobready.com.au"]
11
+
12
+ spec.licenses = ['MIT']
13
+ spec.summary = %q{A simple gem for one off tasks}
14
+ spec.description = %q{A simple gem for one off tasks for example database patches}
15
+ spec.homepage = "http://github.com/jobready/patches"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "railties", ">= 3.2"
23
+ spec.add_dependency "slack-notifier"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.8"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "sqlite3", "~> 1.3.5"
28
+ spec.add_development_dependency "rspec-rails", "~> 4.0.0"
29
+ spec.add_development_dependency "capybara", "~> 2.3.0"
30
+ spec.add_development_dependency "generator_spec", "~> 0.9.0"
31
+ spec.add_development_dependency "simplecov", "~> 0.10"
32
+ spec.add_development_dependency "factory_girl", "~> 4.5.0"
33
+ spec.add_development_dependency "timecop", "~> 0.7.0"
34
+ spec.add_development_dependency "database_cleaner", "~> 1.3.0"
35
+ spec.add_development_dependency "pry"
36
+ spec.add_development_dependency "sidekiq", "~> 3.4.1"
37
+ spec.add_development_dependency "webmock"
38
+ spec.add_development_dependency "byebug"
39
+ end
metadata ADDED
@@ -0,0 +1,304 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: patches
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.4.0
5
+ platform: ruby
6
+ authors:
7
+ - JobReady
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-07-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: slack-notifier
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.3.5
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.3.5
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 4.0.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 4.0.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: capybara
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 2.3.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 2.3.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: generator_spec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.9.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.9.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.10'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.10'
139
+ - !ruby/object:Gem::Dependency
140
+ name: factory_girl
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 4.5.0
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 4.5.0
153
+ - !ruby/object:Gem::Dependency
154
+ name: timecop
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: 0.7.0
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: 0.7.0
167
+ - !ruby/object:Gem::Dependency
168
+ name: database_cleaner
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: 1.3.0
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: 1.3.0
181
+ - !ruby/object:Gem::Dependency
182
+ name: pry
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: sidekiq
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - "~>"
200
+ - !ruby/object:Gem::Version
201
+ version: 3.4.1
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - "~>"
207
+ - !ruby/object:Gem::Version
208
+ version: 3.4.1
209
+ - !ruby/object:Gem::Dependency
210
+ name: webmock
211
+ requirement: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - ">="
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ type: :development
217
+ prerelease: false
218
+ version_requirements: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - ">="
221
+ - !ruby/object:Gem::Version
222
+ version: '0'
223
+ - !ruby/object:Gem::Dependency
224
+ name: byebug
225
+ requirement: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - ">="
228
+ - !ruby/object:Gem::Version
229
+ version: '0'
230
+ type: :development
231
+ prerelease: false
232
+ version_requirements: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - ">="
235
+ - !ruby/object:Gem::Version
236
+ version: '0'
237
+ description: A simple gem for one off tasks for example database patches
238
+ email:
239
+ - ruby_gems@jobready.com.au
240
+ executables: []
241
+ extensions: []
242
+ extra_rdoc_files: []
243
+ files:
244
+ - ".buildkite/pipeline.yml"
245
+ - ".gitignore"
246
+ - ".rspec"
247
+ - CHANGELOG.md
248
+ - Dockerfile
249
+ - Gemfile
250
+ - LICENSE.md
251
+ - README.md
252
+ - Rakefile
253
+ - bin/console
254
+ - bin/setup
255
+ - db/migrate/201506011700_create_patch.rb
256
+ - docker-compose.yml
257
+ - docs/patches.jpg
258
+ - docs/usage.md
259
+ - lib/generators/patches.rb
260
+ - lib/generators/patches/patch_generator.rb
261
+ - lib/generators/patches/templates/patch.rb.erb
262
+ - lib/generators/patches/templates/patch_spec.rb.erb
263
+ - lib/patches.rb
264
+ - lib/patches/application_version_validation.rb
265
+ - lib/patches/base.rb
266
+ - lib/patches/capistrano.rb
267
+ - lib/patches/capistrano/tasks.rake
268
+ - lib/patches/config.rb
269
+ - lib/patches/engine.rb
270
+ - lib/patches/notifier.rb
271
+ - lib/patches/patch.rb
272
+ - lib/patches/pending.rb
273
+ - lib/patches/runner.rb
274
+ - lib/patches/tenant_run_concern.rb
275
+ - lib/patches/tenant_runner.rb
276
+ - lib/patches/tenant_worker.rb
277
+ - lib/patches/version.rb
278
+ - lib/patches/worker.rb
279
+ - lib/tasks/patches.rake
280
+ - patches.gemspec
281
+ homepage: http://github.com/jobready/patches
282
+ licenses:
283
+ - MIT
284
+ metadata: {}
285
+ post_install_message:
286
+ rdoc_options: []
287
+ require_paths:
288
+ - lib
289
+ required_ruby_version: !ruby/object:Gem::Requirement
290
+ requirements:
291
+ - - ">="
292
+ - !ruby/object:Gem::Version
293
+ version: '0'
294
+ required_rubygems_version: !ruby/object:Gem::Requirement
295
+ requirements:
296
+ - - ">="
297
+ - !ruby/object:Gem::Version
298
+ version: '0'
299
+ requirements: []
300
+ rubygems_version: 3.0.3
301
+ signing_key:
302
+ specification_version: 4
303
+ summary: A simple gem for one off tasks
304
+ test_files: []