handcuffs 1.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ebb91c87f47ee27d48e0b3624f58710d6b183a0b
4
+ data.tar.gz: bc6b7fcb372a4677fb869bad00a12e270418e8b6
5
+ SHA512:
6
+ metadata.gz: 21dfc00d8c3b5a09dd96a6bae221ab8107c5fcfd582b8f89896b52d077247371e64753c0b86895b9eb0d4cd9b3dccb5ef5e1ef6861b9148993bd1eb9181006a7
7
+ data.tar.gz: ca359b041108377c6d36c1e5414d0d87140b43b846bd9f9773ffb3ec3fb914cebd3efc04023523ba256a6c9e14f71bf5a72eb0be723f93d4db5bb4a2c2f35155
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ handcuffs-*.gem
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at bradurani@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in handcuffs.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,148 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ handcuffs (0.1.11)
5
+ rails (>= 4.0, < 5.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionmailer (4.2.6)
11
+ actionpack (= 4.2.6)
12
+ actionview (= 4.2.6)
13
+ activejob (= 4.2.6)
14
+ mail (~> 2.5, >= 2.5.4)
15
+ rails-dom-testing (~> 1.0, >= 1.0.5)
16
+ actionpack (4.2.6)
17
+ actionview (= 4.2.6)
18
+ activesupport (= 4.2.6)
19
+ rack (~> 1.6)
20
+ rack-test (~> 0.6.2)
21
+ rails-dom-testing (~> 1.0, >= 1.0.5)
22
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
23
+ actionview (4.2.6)
24
+ activesupport (= 4.2.6)
25
+ builder (~> 3.1)
26
+ erubis (~> 2.7.0)
27
+ rails-dom-testing (~> 1.0, >= 1.0.5)
28
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
29
+ activejob (4.2.6)
30
+ activesupport (= 4.2.6)
31
+ globalid (>= 0.3.0)
32
+ activemodel (4.2.6)
33
+ activesupport (= 4.2.6)
34
+ builder (~> 3.1)
35
+ activerecord (4.2.6)
36
+ activemodel (= 4.2.6)
37
+ activesupport (= 4.2.6)
38
+ arel (~> 6.0)
39
+ activesupport (4.2.6)
40
+ i18n (~> 0.7)
41
+ json (~> 1.7, >= 1.7.7)
42
+ minitest (~> 5.1)
43
+ thread_safe (~> 0.3, >= 0.3.4)
44
+ tzinfo (~> 1.1)
45
+ arel (6.0.3)
46
+ builder (3.2.2)
47
+ byebug (5.0.0)
48
+ columnize (= 0.9.0)
49
+ coderay (1.1.0)
50
+ columnize (0.9.0)
51
+ concurrent-ruby (1.0.1)
52
+ diff-lcs (1.2.5)
53
+ erubis (2.7.0)
54
+ globalid (0.3.6)
55
+ activesupport (>= 4.1.0)
56
+ i18n (0.7.0)
57
+ json (1.8.3)
58
+ loofah (2.0.3)
59
+ nokogiri (>= 1.5.9)
60
+ mail (2.6.4)
61
+ mime-types (>= 1.16, < 4)
62
+ method_source (0.8.2)
63
+ mime-types (3.0)
64
+ mime-types-data (~> 3.2015)
65
+ mime-types-data (3.2016.0221)
66
+ mini_portile2 (2.0.0)
67
+ minitest (5.8.4)
68
+ nokogiri (1.6.7.2)
69
+ mini_portile2 (~> 2.0.0.rc2)
70
+ pg (0.18.2)
71
+ pry (0.10.3)
72
+ coderay (~> 1.1.0)
73
+ method_source (~> 0.8.1)
74
+ slop (~> 3.4)
75
+ pry-byebug (3.2.0)
76
+ byebug (~> 5.0)
77
+ pry (~> 0.10)
78
+ rack (1.6.4)
79
+ rack-test (0.6.3)
80
+ rack (>= 1.0)
81
+ rails (4.2.6)
82
+ actionmailer (= 4.2.6)
83
+ actionpack (= 4.2.6)
84
+ actionview (= 4.2.6)
85
+ activejob (= 4.2.6)
86
+ activemodel (= 4.2.6)
87
+ activerecord (= 4.2.6)
88
+ activesupport (= 4.2.6)
89
+ bundler (>= 1.3.0, < 2.0)
90
+ railties (= 4.2.6)
91
+ sprockets-rails
92
+ rails-deprecated_sanitizer (1.0.3)
93
+ activesupport (>= 4.2.0.alpha)
94
+ rails-dom-testing (1.0.7)
95
+ activesupport (>= 4.2.0.beta, < 5.0)
96
+ nokogiri (~> 1.6.0)
97
+ rails-deprecated_sanitizer (>= 1.0.1)
98
+ rails-html-sanitizer (1.0.3)
99
+ loofah (~> 2.0)
100
+ railties (4.2.6)
101
+ actionpack (= 4.2.6)
102
+ activesupport (= 4.2.6)
103
+ rake (>= 0.8.7)
104
+ thor (>= 0.18.1, < 2.0)
105
+ rake (11.1.2)
106
+ rspec-core (3.3.2)
107
+ rspec-support (~> 3.3.0)
108
+ rspec-expectations (3.3.1)
109
+ diff-lcs (>= 1.2.0, < 2.0)
110
+ rspec-support (~> 3.3.0)
111
+ rspec-mocks (3.3.2)
112
+ diff-lcs (>= 1.2.0, < 2.0)
113
+ rspec-support (~> 3.3.0)
114
+ rspec-rails (3.3.3)
115
+ actionpack (>= 3.0, < 4.3)
116
+ activesupport (>= 3.0, < 4.3)
117
+ railties (>= 3.0, < 4.3)
118
+ rspec-core (~> 3.3.0)
119
+ rspec-expectations (~> 3.3.0)
120
+ rspec-mocks (~> 3.3.0)
121
+ rspec-support (~> 3.3.0)
122
+ rspec-support (3.3.0)
123
+ slop (3.6.0)
124
+ sprockets (3.5.2)
125
+ concurrent-ruby (~> 1.0)
126
+ rack (> 1, < 3)
127
+ sprockets-rails (3.0.4)
128
+ actionpack (>= 4.0)
129
+ activesupport (>= 4.0)
130
+ sprockets (>= 3.0.0)
131
+ thor (0.19.1)
132
+ thread_safe (0.3.5)
133
+ tzinfo (1.2.2)
134
+ thread_safe (~> 0.1)
135
+
136
+ PLATFORMS
137
+ ruby
138
+
139
+ DEPENDENCIES
140
+ bundler (~> 1.11)
141
+ handcuffs!
142
+ pg
143
+ pry
144
+ pry-byebug
145
+ rspec-rails (~> 3.0)
146
+
147
+ BUNDLED WITH
148
+ 1.11.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Procore Technologies, 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.
data/README.md ADDED
@@ -0,0 +1,120 @@
1
+ # Handcuffs
2
+
3
+ Handcuffs provides an easy way to run migrations in phases in your [Ruby on
4
+ Rails](https://rubyonrails.org/) application.
5
+
6
+ To configure, first create a handcuff initializer and define a configuration
7
+
8
+ ```ruby
9
+ # config/initializers/handcuffs.rb
10
+
11
+ Handcuffs.configure do |config|
12
+ config.phases = [:pre_deploy, :post_deploy]
13
+ end
14
+ ```
15
+
16
+ Then call `phase` from inside your migrations
17
+
18
+ ```ruby
19
+ # db/migrate/20160318230933_add_on_sale_column.rb
20
+
21
+ class AddOnSaleColumn < ActiveRecord::Migration
22
+
23
+ phase :pre_deploy
24
+
25
+ def up
26
+ add_column :products, :on_sale, :boolean
27
+ end
28
+
29
+ def down
30
+ remove_column :products, :on_sale
31
+ end
32
+
33
+ end
34
+ ```
35
+
36
+ ```ruby
37
+ # db/migrate/20160318230988_add_on_sale_index
38
+
39
+ class AddOnSaleColumn < ActiveRecord::Migration
40
+
41
+ phase :post_deploy
42
+
43
+ def up
44
+ add_index :products, :on_sale, algorithm: :concurrently
45
+ end
46
+
47
+ def down
48
+ remove_column :products, :on_sale
49
+ end
50
+
51
+ end
52
+ ```
53
+
54
+ You can then run your migrations in phases using
55
+ ```bash
56
+ rake handcuffs:migrate[pre_deploy]
57
+ ```
58
+ or
59
+ ```bash
60
+ rake handcuffs:migrate[post_deploy]
61
+ ```
62
+
63
+ You can run all migrations using
64
+ ```bash
65
+ rake handcuffs:migrate[all]
66
+ ```
67
+
68
+ This differs from running `rake db:migrate` in that specs will be run in the
69
+ _order that the phases are defined in the handcuffs config_.
70
+
71
+ If you run a handcuffs rake task and any migration does not have a phase
72
+ defined, an error will be raised before any migrations are run. To prevent this
73
+ error, you can define a default phase for migrations that don't define one.
74
+ ```ruby
75
+ # config/initializers/handcuffs.rb
76
+
77
+ Handcuffs.configure do |config|
78
+ config.phases = [:pre_deploy, :post_deploy]
79
+ config.default_phase = :pre_deploy
80
+ end
81
+ ```
82
+
83
+ ## Installation
84
+
85
+ Add this line to your application's Gemfile:
86
+
87
+ ```ruby
88
+ gem 'handcuffs'
89
+ ```
90
+
91
+ And then execute:
92
+
93
+ $ bundle
94
+
95
+ Or install it yourself as:
96
+
97
+ $ gem install handcuffs
98
+
99
+ ##Running specs
100
+
101
+ The specs for handcuffs are in the dummy application at `/spec/dummy/spec`. The
102
+ spec suite requires PostgreSQL. To run it you will have to set the environment
103
+ variables `POSTGRES_DB_USERNAME` and `POSTGRES_DB_PASSWORD`. You can then run
104
+ the suite using `rake spec`
105
+
106
+ ## Development
107
+
108
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
109
+
110
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
111
+
112
+ ## Contributing
113
+
114
+ Bug reports and pull requests are welcome on GitHub at https://github.com/procore/handcuffs. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
115
+
116
+
117
+ ## License
118
+
119
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
120
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "handcuffs"
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,8 @@
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
data/handcuffs.gemspec ADDED
@@ -0,0 +1,37 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'handcuffs/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "handcuffs"
8
+ spec.version = Handcuffs::VERSION
9
+ spec.authors = ["Brad Urani"]
10
+ spec.email = ["bradurani@gmail.com"]
11
+
12
+ spec.summary = %q{A Ruby gem for running Active Record migrations in phase}
13
+ spec.description = %q{Allows you to define phase for migrations and run only the phase desired}
14
+ spec.homepage = "https://github.com/procore/handcuffs/"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.11"
31
+ spec.add_development_dependency "rspec-rails", "~> 3.0"
32
+ spec.add_development_dependency "pg"
33
+ spec.add_development_dependency "pry"
34
+ spec.add_development_dependency "pry-byebug"
35
+
36
+ spec.add_runtime_dependency "rails", ">= 4.0", "< 5.0"
37
+ end
@@ -0,0 +1,22 @@
1
+ module Handcuffs
2
+ mattr_accessor :config
3
+
4
+ def self.configure
5
+ raise 'must pass a block to Handcuffs.configure' unless block_given?
6
+ @@config = Configurator.new
7
+ yield @@config
8
+ end
9
+
10
+ class Configurator
11
+ attr_accessor :phases
12
+ attr_accessor :default_phase
13
+
14
+ def initialize
15
+ @phases = []
16
+ @default_phase = nil
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+
@@ -0,0 +1,51 @@
1
+
2
+ class HandcuffsError < StandardError; end
3
+
4
+ class RequiresPhaseArgumentError < HandcuffsError
5
+ def initialize(task)
6
+ msg = <<-MESSAGE
7
+ rake #{task} requires a phase argument.
8
+ For example: #{task}[pre_deploy]
9
+ MESSAGE
10
+ super(msg)
11
+ end
12
+ end
13
+
14
+ class HandcuffsNotConfiguredError < HandcuffsError
15
+ def initialize
16
+ msg = <<-MESSAGE
17
+ You must configure Handcuffs in your Rails initializer.
18
+ see README.md for details
19
+ MESSAGE
20
+ super(msg)
21
+ end
22
+ end
23
+
24
+ class HandcuffsUnknownPhaseError < HandcuffsError
25
+ def initialize(phase, phases)
26
+ msg = <<-MESSAGE
27
+ Unknown phase #{phase.to_s}
28
+ Handcuffs is configured to allow #{phases.to_sentence}
29
+ MESSAGE
30
+ super msg
31
+ end
32
+ end
33
+
34
+ class HandcuffsPhaseOutOfOrderError < HandcuffsError
35
+ def initialize(not_run_phase, attempted_phase)
36
+ msg = <<-MESSAGE
37
+ Your tried to run #{attempted_phase.to_s}, but #{not_run_phase.to_s} has not been run
38
+ MESSAGE
39
+ super msg
40
+ end
41
+ end
42
+
43
+ class HandcuffsPhaseUndefinedError < HandcuffsError
44
+ def initialize(undefined_phases)
45
+ msg = <<-MESSAGE
46
+ The following migrations do not have a phase defined
47
+ #{undefined_phases.to_sentence}
48
+ MESSAGE
49
+ super msg
50
+ end
51
+ end
@@ -0,0 +1,12 @@
1
+ module Handcuffs
2
+ module Extensions
3
+
4
+ attr_reader :handcuffs_phase
5
+
6
+ def phase(phase)
7
+ @handcuffs_phase = phase
8
+ end
9
+
10
+ end
11
+ end
12
+
@@ -0,0 +1,81 @@
1
+ class Handcuffs::PhaseFilter
2
+ attr_reader :attempted_phase
3
+ attr_reader :direction
4
+
5
+ def initialize(attempted_phase, direction)
6
+ @attempted_phase = attempted_phase
7
+ @direction = direction
8
+ end
9
+
10
+ def filter(migration_proxies)
11
+ migration_hashes = proxies_with_migrations(migration_proxies)
12
+ check_for_undefined_phases!(migration_hashes)
13
+ by_phase = migration_hashes.lazy.group_by { |mh| phase(mh[:migration]) }
14
+ defined_phases = Handcuffs.config.phases
15
+ if(attempted_phase == :all)
16
+ all_phases_by_configuration_order(by_phase, defined_phases)
17
+ else
18
+ runnable_for_phase(by_phase, defined_phases)
19
+ end
20
+ end
21
+
22
+ def proxies_with_migrations(migration_proxies)
23
+ migration_proxies.map do |proxy|
24
+ require(proxy.filename)
25
+ {
26
+ proxy: proxy,
27
+ migration: Kernel.const_get("::#{proxy.name}")
28
+ }
29
+ end
30
+ end
31
+
32
+ private
33
+ def runnable_for_phase(by_phase, defined_phases)
34
+ if(direction == :up)
35
+ check_order_up!(by_phase, defined_phases)
36
+ else
37
+ check_order_down!(by_phase, defined_phases)
38
+ end
39
+ Array(by_phase[attempted_phase]).map { |mh| mh[:proxy] }
40
+ end
41
+
42
+ def check_order_up!(by_phase, defined_phases)
43
+ defined_phases.take_while { |defined_phase| defined_phase != attempted_phase }
44
+ .detect { |defined_phase| by_phase.key?(defined_phase) }
45
+ .tap do |defined_phase|
46
+ raise HandcuffsPhaseOutOfOrderError.new(defined_phase, attempted_phase) if defined_phase
47
+ end
48
+ end
49
+
50
+ def check_order_down!(by_phase, defined_phases)
51
+ #There's no way to do this without some super hackery. If we run rake
52
+ #handcuffs::rollback[:post_deploy] and the top of the list (in desc order)
53
+ #in a pre_deploy, we don't know if that was run before or after the
54
+ #last post_deploy because we can't count on the versions to give us the
55
+ #execution order. Without storing the execution order in another table,
56
+ #there's no way to implement this
57
+ end
58
+
59
+ def all_phases_by_configuration_order(by_phase, defined_phases)
60
+ defined_phases.reduce([]) do |acc, phase|
61
+ acc | by_phase[phase]
62
+ end.map { |mh| mh[:proxy] }
63
+ end
64
+
65
+ def check_for_undefined_phases!(migration_hashes)
66
+ unless Handcuffs.config.default_phase
67
+ nil_migration_hashes = migration_hashes.select do |mh|
68
+ mh[:migration].handcuffs_phase.nil?
69
+ end
70
+ if nil_migration_hashes.any?
71
+ filenames = nil_migration_hashes.map { |mh| mh[:proxy].filename }
72
+ raise HandcuffsPhaseUndefinedError.new(filenames)
73
+ end
74
+ end
75
+ end
76
+
77
+ def phase(migration)
78
+ migration.handcuffs_phase || Handcuffs.config.default_phase
79
+ end
80
+
81
+ end
@@ -0,0 +1,7 @@
1
+ module Handcuffs
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks do
4
+ load "tasks/handcuffs.rake"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Handcuffs
2
+ VERSION = "1.0.0"
3
+ end
data/lib/handcuffs.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'rails'
2
+ require 'active_record'
3
+
4
+ Dir[File.join(File.dirname(__FILE__), 'handcuffs', '*.rb')].each {|file| require file }
5
+
6
+ ActiveRecord::Migration.extend Handcuffs::Extensions
7
+
@@ -0,0 +1,77 @@
1
+ namespace :handcuffs do
2
+ task :migrate, [:phase] => :environment do |t,args|
3
+ phase = setup(args, 'handcuffs:migrate')
4
+ patch_migrator!(phase)
5
+ run_task('db:migrate')
6
+ # load_config
7
+
8
+ #active_record/railties/databases.rake:44
9
+ # ActiveRecord::Tasks::DatabaseTasks.migrate
10
+ # dump_schema
11
+ end
12
+
13
+ task :rollback, [:phase] => :environment do |t,args|
14
+ phase = setup(args, 'handcuffs:rollback')
15
+ patch_migrator!(phase)
16
+ # load_config
17
+ run_task('db:rollback')
18
+
19
+ #active_record/railsties/databases.rake:142
20
+ # step = ENV['STEP'] ? ENV['STEP'].to_i : 1
21
+ # ActiveRecord::Migrator.rollback(ActiveRecord::Tasks::DatabaseTasks.migrations_paths, step)
22
+ # dump_schema
23
+ end
24
+
25
+ def setup(args, task)
26
+ phase = args.phase
27
+ raise RequiresPhaseArgumentError.new(task) unless phase.present?
28
+ raise HandcuffsNotConfiguredError.new unless Handcuffs.config
29
+ phase = phase.to_sym
30
+ unless Handcuffs.config.phases.include?(phase) || phase == :all
31
+ raise HandcuffsUnknownPhaseError.new(phase, Handcuffs.config.phases)
32
+ end
33
+ phase
34
+ end
35
+
36
+ def patch_migrator!(phase)
37
+ ActiveRecord::Migrator.prepend(PendingFilter)
38
+ ActiveRecord::Migrator.extend(PhaseAccessor)
39
+ ActiveRecord::Migrator.handcuffs_phase = phase
40
+ end
41
+
42
+ def run_task(name)
43
+ Rake::Task.clear # necessary to avoid tasks being loaded several times in dev mode
44
+ Rails.application.load_tasks
45
+ Rake::Task[name].reenable # in case you're going to invoke the same task second time.
46
+ Rake::Task[name].invoke
47
+ end
48
+
49
+ # def load_config
50
+ # #active_record/railties/databases.rake:5
51
+ # ActiveRecord::Base.configurations = ActiveRecord::Tasks::DatabaseTasks.database_configuration || {}
52
+ # ActiveRecord::Migrator.migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
53
+ # end
54
+ #
55
+ # def dump_schema
56
+ # rake['db:structure:dump'].invoke
57
+ # end
58
+
59
+ module PendingFilter
60
+ def runnable
61
+ attempted_phase = self.class.handcuffs_phase
62
+ if(@direction == :up)
63
+ Handcuffs::PhaseFilter.new(attempted_phase, @direction).filter(super)
64
+ else
65
+ phase_migrations = Handcuffs::PhaseFilter.new(attempted_phase, @direction).filter(migrations)
66
+ runnable = phase_migrations[start..finish]
67
+ runnable.pop if target
68
+ runnable.find_all { |m| ran?(m) }
69
+ end
70
+ end
71
+ end
72
+
73
+ module PhaseAccessor
74
+ attr_accessor :handcuffs_phase
75
+ end
76
+
77
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: handcuffs
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Brad Urani
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pg
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '4.0'
90
+ - - "<"
91
+ - !ruby/object:Gem::Version
92
+ version: '5.0'
93
+ type: :runtime
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '4.0'
100
+ - - "<"
101
+ - !ruby/object:Gem::Version
102
+ version: '5.0'
103
+ description: Allows you to define phase for migrations and run only the phase desired
104
+ email:
105
+ - bradurani@gmail.com
106
+ executables: []
107
+ extensions: []
108
+ extra_rdoc_files: []
109
+ files:
110
+ - ".gitignore"
111
+ - CODE_OF_CONDUCT.md
112
+ - Gemfile
113
+ - Gemfile.lock
114
+ - LICENSE.txt
115
+ - README.md
116
+ - Rakefile
117
+ - bin/console
118
+ - bin/setup
119
+ - handcuffs.gemspec
120
+ - lib/handcuffs.rb
121
+ - lib/handcuffs/configuration.rb
122
+ - lib/handcuffs/errors.rb
123
+ - lib/handcuffs/extensions.rb
124
+ - lib/handcuffs/phase_filter.rb
125
+ - lib/handcuffs/railtie.rb
126
+ - lib/handcuffs/version.rb
127
+ - lib/tasks/handcuffs.rake
128
+ homepage: https://github.com/procore/handcuffs/
129
+ licenses:
130
+ - MIT
131
+ metadata:
132
+ allowed_push_host: https://rubygems.org
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubyforge_project:
149
+ rubygems_version: 2.4.5.1
150
+ signing_key:
151
+ specification_version: 4
152
+ summary: A Ruby gem for running Active Record migrations in phase
153
+ test_files: []