patches 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6b3d2cbeb7527f40447f40af14ba0e1f77f7e044
4
+ data.tar.gz: 9bef2a16a778e769ef4aa95118e817ad0246283c
5
+ SHA512:
6
+ metadata.gz: 3c7b3accf85699654b44802122b61a115357be010788810578e90693eae5acc8f623fbd997e1c003b33ccf1442d6a000e2286668703398dacce95884e432b002
7
+ data.tar.gz: 613051dd3fb716f12b1f1d4957e91ccdbb8704ed2a40f6a3a5919e1ce6ed7f564de3245bb8730c2aea6b53330ed2cf24ed4e3a817891b276f9d86c4b70c1c813
data/.gitignore ADDED
@@ -0,0 +1,12 @@
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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.3
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem "rails", "~> 4.1"
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Patches
2
+ [![Build status](https://badge.buildkite.com/4f3df3f3458bcc933dc44cab6c136af5c3bbdd9f761f1a99ff.svg)](https://buildkite.com/jobready/patches)
3
+ [![Code Climate](https://codeclimate.com/repos/557f93b76956807f81000001/badges/39d142050017ffeb2564/gpa.svg)](https://codeclimate.com/repos/557f93b76956807f81000001/feed)
4
+ [![Test Coverage](https://codeclimate.com/repos/557f93b76956807f81000001/badges/39d142050017ffeb2564/coverage.svg)](https://codeclimate.com/repos/557f93b76956807f81000001/coverage)
5
+
6
+ A simple gem for one off tasks
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'patches'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install patches
23
+
24
+ ## Usage
25
+
26
+ see `docs/usage.md`
27
+
28
+ ## Development
29
+
30
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
31
+
32
+ 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).
33
+
34
+ ## Contributing
35
+
36
+ 1. Fork it ( https://github.com/[my-github-username]/patches/fork )
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -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
data/bin/setup ADDED
@@ -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
data/docs/usage.md ADDED
@@ -0,0 +1,38 @@
1
+ # Usage
2
+
3
+ Install the migration
4
+
5
+ ```
6
+ bundle exec rake patches:install:migrations
7
+ ```
8
+
9
+ Generate a patch
10
+
11
+ ```
12
+ bundle exec rails g patches:patch PreferenceUpdate
13
+ ```
14
+
15
+ Which will result in a file like below
16
+
17
+ ```ruby
18
+ class PreferenceUpdate < Patches::Base
19
+ def run
20
+ # Code goes here
21
+ end
22
+ end
23
+ ```
24
+
25
+ update the run method and then execute
26
+
27
+
28
+ ```bash
29
+ bundle exec rake patches:run
30
+ ```
31
+
32
+ Patches will only ever run once, patches will run in order.
33
+
34
+ To run patches after db:migrate on deployment edit Capfile and add
35
+
36
+ ```ruby
37
+ require 'patches/capistrano'
38
+ ```
@@ -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,23 @@
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.rb.erb", "db/patches/#{file_name}.rb"
9
+ end
10
+
11
+ private
12
+
13
+ def class_name
14
+ name.camelize
15
+ end
16
+
17
+ def file_name
18
+ "#{Time.now.strftime('%Y%m%d%H%M')}_#{name.underscore}"
19
+ end
20
+ end
21
+ end
22
+ end
23
+
@@ -0,0 +1,5 @@
1
+ class <%= class_name %> < Patches::Base
2
+ def run
3
+ # Code goes here
4
+ end
5
+ end
data/lib/patches.rb ADDED
@@ -0,0 +1,26 @@
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/engine" if defined?(Rails)
24
+ require "patches/patch"
25
+ require "patches/pending"
26
+ require "patches/runner"
@@ -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,13 @@
1
+ namespace :deploy do
2
+ desc 'Runs rake patches:run'
3
+ task :patches 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
+ after 'deploy:migrate', 'deploy:patches'
13
+ 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,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,34 @@
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
+ Patches.logger.info("Patches found: #{files.join(',')}")
13
+
14
+ files.each do |file|
15
+ unless already_run?(file)
16
+ yield file
17
+ end
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def files
24
+ @files ||= Dir[File.join(path, "*.rb")].to_a.sort
25
+ end
26
+
27
+ def already_run?(path)
28
+ patches[File.basename(path)]
29
+ end
30
+
31
+ def patches
32
+ @patches ||= Patches::Patch.path_lookup
33
+ end
34
+ end
@@ -0,0 +1,42 @@
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
+ 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
+ instance.run
16
+ complete!(patch_path(file_path))
17
+ end
18
+ end
19
+
20
+ def complete!(patch_path)
21
+ Patches::Patch.create!(path: patch_path)
22
+ end
23
+
24
+ def patch_path(patch_path)
25
+ File.basename(patch_path)
26
+ end
27
+
28
+ def pending
29
+ @pending ||= Patches::Pending.new(path)
30
+ end
31
+
32
+ private
33
+ def load_class(path)
34
+ begin
35
+ name = Patches.class_name(path)
36
+ load path
37
+ name.constantize
38
+ rescue StandardError, LoadError
39
+ raise UnknownPatch, "#{path} should define #{name}"
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,26 @@
1
+ class Patches::TenantRunner
2
+ attr_accessor :path
3
+
4
+ def initialize(path: nil, tenants: nil)
5
+ @path = path
6
+ @tenants = tenants
7
+ end
8
+
9
+ def perform
10
+ Patches.logger.info("Patches tenant runner for: #{tenants.join(',')}")
11
+
12
+ tenants.each do |tenant|
13
+ Apartment::Tenant.switch(tenant)
14
+ runner = build
15
+ runner.perform
16
+ end
17
+ end
18
+
19
+ def build
20
+ Patches::Runner.new(path)
21
+ end
22
+
23
+ def tenants
24
+ @tenants ||= (Apartment.tenant_names || [])
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Patches
2
+ VERSION = "0.1.8"
3
+ end
@@ -0,0 +1,22 @@
1
+ require 'patches/tenant_runner'
2
+
3
+ namespace :patches do
4
+ desc "Run Patches"
5
+ task :run => [:environment] do
6
+ if defined?(Apartment) && tenants.present?
7
+ Patches::TenantRunner.new(tenants: tenants).perform
8
+ else
9
+ Patches::Runner.new.perform
10
+ end
11
+ end
12
+
13
+ def tenants
14
+ ENV['DB'] ? ENV['DB'].split(',').map { |s| s.strip } : Apartment.tenant_names || []
15
+ end
16
+
17
+ task :pending => [:environment] do
18
+ Patches::Pending.each do |patch|
19
+ puts patch
20
+ end
21
+ end
22
+ end
data/patches.gemspec ADDED
@@ -0,0 +1,33 @@
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 = ["John D'Agostino"]
10
+ spec.email = ["johnd@jobready.com.au"]
11
+
12
+ spec.summary = %q{A simple gem for one off tasks}
13
+ spec.description = %q{A simple gem for one of tasks for example database patches}
14
+ spec.homepage = "http://github.com/jobready/patches"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "railties", ">= 3.2"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.8"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "sqlite3", "~> 1.3.5"
26
+ spec.add_development_dependency "rspec-rails", "~> 3.2.0"
27
+ spec.add_development_dependency "capybara", "~> 2.3.0"
28
+ spec.add_development_dependency "generator_spec", "~> 0.9.0"
29
+ spec.add_development_dependency "simplecov", "~> 0.10"
30
+ spec.add_development_dependency "factory_girl", "~> 4.5.0"
31
+ spec.add_development_dependency "timecop", "~> 0.7.0"
32
+ spec.add_development_dependency "database_cleaner", "~> 1.3.0"
33
+ end
@@ -0,0 +1,19 @@
1
+ #!/bin/bash
2
+ set -e
3
+
4
+ echo '--- setting ruby version'
5
+ rbenv local 2.1.5
6
+
7
+ echo '--- setting up env'
8
+ REVISION=https://github.com/$BUILDBOX_PROJECT_SLUG/commit/$BUILDBOX_COMMIT
9
+
10
+ echo '--- bundling'
11
+ bundle install -j $(nproc) --without production --quiet
12
+
13
+ echo '--- running specs'
14
+ if bundle exec rspec; then
15
+ echo "[Successful] $BUILDBOX_PROJECT_SLUG - Build - $BUILDBOX_BUILD_URL - Commit - $REVISION" | hipchat_room_message -t $HIPCHAT_TOKEN -r $HIPCHAT_ROOM -f "Buildbox" -c "green"
16
+ else
17
+ echo "[Failed] Build $BUILDBOX_PROJECT_SLUG - Build - $BUILDBOX_BUILD_URL - Commit - $REVISION" | hipchat_room_message -t $HIPCHAT_TOKEN -r $HIPCHAT_ROOM -f "Buildbox" -c "red"
18
+ exit 1;
19
+ fi
metadata ADDED
@@ -0,0 +1,224 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: patches
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.8
5
+ platform: ruby
6
+ authors:
7
+ - John D'Agostino
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-23 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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.5
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.3.5
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 3.2.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 3.2.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: capybara
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 2.3.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 2.3.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: generator_spec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.9.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.9.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.10'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.10'
125
+ - !ruby/object:Gem::Dependency
126
+ name: factory_girl
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 4.5.0
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 4.5.0
139
+ - !ruby/object:Gem::Dependency
140
+ name: timecop
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 0.7.0
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 0.7.0
153
+ - !ruby/object:Gem::Dependency
154
+ name: database_cleaner
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: 1.3.0
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: 1.3.0
167
+ description: A simple gem for one of tasks for example database patches
168
+ email:
169
+ - johnd@jobready.com.au
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - ".gitignore"
175
+ - ".rspec"
176
+ - ".travis.yml"
177
+ - Gemfile
178
+ - README.md
179
+ - Rakefile
180
+ - bin/console
181
+ - bin/setup
182
+ - db/migrate/201506011700_create_patch.rb
183
+ - docs/usage.md
184
+ - lib/generators/patches.rb
185
+ - lib/generators/patches/patch_generator.rb
186
+ - lib/generators/patches/templates/patch.rb.erb
187
+ - lib/patches.rb
188
+ - lib/patches/base.rb
189
+ - lib/patches/capistrano.rb
190
+ - lib/patches/capistrano/tasks.rake
191
+ - lib/patches/engine.rb
192
+ - lib/patches/patch.rb
193
+ - lib/patches/pending.rb
194
+ - lib/patches/runner.rb
195
+ - lib/patches/tenant_runner.rb
196
+ - lib/patches/version.rb
197
+ - lib/tasks/patches.rake
198
+ - patches.gemspec
199
+ - script/buildkite.sh
200
+ homepage: http://github.com/jobready/patches
201
+ licenses: []
202
+ metadata: {}
203
+ post_install_message:
204
+ rdoc_options: []
205
+ require_paths:
206
+ - lib
207
+ required_ruby_version: !ruby/object:Gem::Requirement
208
+ requirements:
209
+ - - ">="
210
+ - !ruby/object:Gem::Version
211
+ version: '0'
212
+ required_rubygems_version: !ruby/object:Gem::Requirement
213
+ requirements:
214
+ - - ">="
215
+ - !ruby/object:Gem::Version
216
+ version: '0'
217
+ requirements: []
218
+ rubyforge_project:
219
+ rubygems_version: 2.2.2
220
+ signing_key:
221
+ specification_version: 4
222
+ summary: A simple gem for one off tasks
223
+ test_files: []
224
+ has_rdoc: