birdspotting 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5eee91abd57f9e2b8fd2b1ea4074048637ef84a6be504ab85106c5b6685cc851
4
+ data.tar.gz: 8ea9402c56cceac3fc6a62b58872b2dddbd90eb04f7c33e9bf6eee3bb4c30cc8
5
+ SHA512:
6
+ metadata.gz: aa88d709c649cd4447c4bd01ebb2a4c21a8f2f445c7bc961fb432a6a1e8aab1292bd1e5602c96c11fb088373d9edbaf727c9d53639be54c16cbe01222bbf6657
7
+ data.tar.gz: 450ecba8b6d2f64677b03e552c82ebe8aee70e130f82374cafe8e2277807449b38fa382fcb2f0b397ae0003b5fd9917ea11fe850b0e4e87eceac899181a66b09
@@ -0,0 +1,43 @@
1
+ aliasses:
2
+ - &step_restore_vendor_cache
3
+ restore_cache:
4
+ keys:
5
+ - birdspotting-vendor-{{ .Branch }}-{{ .Revision }}
6
+ - birdspotting-vendor-{{ .Branch }}
7
+ - birdspotting-vendor-
8
+ - &step_save_vendor_cache
9
+ save_cache:
10
+ key: birdspotting-vendor-{{ .Branch }}-{{ .Revision }}
11
+ paths: [ vendor/bundle ]
12
+
13
+
14
+ version: 2
15
+
16
+ jobs:
17
+ test:
18
+ docker:
19
+ - image: circleci/ruby:2.4-jessie-node
20
+ steps:
21
+ - checkout
22
+ - *step_restore_vendor_cache
23
+ - run:
24
+ name: Install Ruby dependencies
25
+ command: bundle install --path vendor/bundle --jobs=2
26
+ - *step_save_vendor_cache
27
+ - run:
28
+ name: Run RSpec suite
29
+ command: |
30
+ bundle exec rspec \
31
+ --format progress \
32
+ --format RspecJunitFormatter --out /tmp/test-results/rspec.xml
33
+ environment:
34
+ RAILS_ENV: test
35
+ - store_test_results:
36
+ path: /tmp/test-results
37
+
38
+ workflows:
39
+ version: 2
40
+
41
+ test:
42
+ jobs:
43
+ - test
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ *.swp
13
+ *.gem
14
+ *.swo
15
+
16
+ .ruby-gemset
17
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,82 @@
1
+ require: rubocop-rspec
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 2.3
5
+ Exclude:
6
+ - "examples/*.rb"
7
+
8
+ #
9
+ # Opinionated cops
10
+ #
11
+
12
+ Layout/IndentHash:
13
+ EnforcedStyle: consistent
14
+
15
+ Metrics/AbcSize:
16
+ Max: 20
17
+
18
+ Metrics/LineLength:
19
+ Max: 100
20
+
21
+ Metrics/MethodLength:
22
+ Max: 20
23
+
24
+ Style/ClassAndModuleChildren:
25
+ EnforcedStyle: compact
26
+
27
+ Style/StringLiterals:
28
+ EnforcedStyle: double_quotes
29
+
30
+ Style/TrailingCommaInArrayLiteral:
31
+ EnforcedStyleForMultiline: comma
32
+
33
+ Style/TrailingCommaInHashLiteral:
34
+ EnforcedStyleForMultiline: comma
35
+
36
+ #
37
+ # Disabled cops
38
+ #
39
+
40
+ RSpec/VerifiedDoubles:
41
+ Enabled: false
42
+
43
+ Style/FrozenStringLiteralComment:
44
+ Enabled: false
45
+
46
+ Style/BracesAroundHashParameters:
47
+ Enabled: false
48
+
49
+ Style/FormatString:
50
+ Enabled: false
51
+
52
+ Style/Documentation:
53
+ Enabled: false
54
+
55
+ Style/BlockComments:
56
+ Exclude:
57
+ - "spec/spec_helper.rb"
58
+
59
+ Metrics/BlockLength:
60
+ Exclude:
61
+ - "spec/**/*_spec.rb"
62
+
63
+ Style/Proc:
64
+ Enabled: false
65
+
66
+ Layout/EmptyLinesAroundModuleBody:
67
+ Enabled: false
68
+
69
+ RSpec/MultipleExpectations:
70
+ Enabled: false
71
+
72
+ RSpec/AnyInstance:
73
+ Enabled: false
74
+
75
+ RSpec/ContextWording:
76
+ Enabled: false
77
+
78
+ RSpec/MessageSpies:
79
+ Enabled: false
80
+
81
+ RSpec/NestedGroups:
82
+ Enabled: false
@@ -0,0 +1 @@
1
+ ruby-2.4.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at david.bourguignon@drivy.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in birdspotting.gemspec
6
+ gemspec
@@ -0,0 +1,128 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ birdspotting (0.1.0)
5
+ activerecord (>= 5.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.2.1)
11
+ activesupport (= 5.2.1)
12
+ activerecord (5.2.1)
13
+ activemodel (= 5.2.1)
14
+ activesupport (= 5.2.1)
15
+ arel (>= 9.0)
16
+ activesupport (5.2.1)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ arel (9.0.0)
22
+ ast (2.4.0)
23
+ coderay (1.1.2)
24
+ concurrent-ruby (1.0.5)
25
+ diff-lcs (1.3)
26
+ docile (1.3.1)
27
+ ffi (1.9.25)
28
+ formatador (0.2.5)
29
+ guard (2.14.2)
30
+ formatador (>= 0.2.4)
31
+ listen (>= 2.7, < 4.0)
32
+ lumberjack (>= 1.0.12, < 2.0)
33
+ nenv (~> 0.1)
34
+ notiffany (~> 0.0)
35
+ pry (>= 0.9.12)
36
+ shellany (~> 0.0)
37
+ thor (>= 0.18.1)
38
+ guard-compat (1.2.1)
39
+ guard-rspec (4.7.3)
40
+ guard (~> 2.1)
41
+ guard-compat (~> 1.1)
42
+ rspec (>= 2.99.0, < 4.0)
43
+ guard-rubocop (1.3.0)
44
+ guard (~> 2.0)
45
+ rubocop (~> 0.20)
46
+ i18n (1.1.0)
47
+ concurrent-ruby (~> 1.0)
48
+ jaro_winkler (1.5.1)
49
+ json (2.1.0)
50
+ listen (3.1.5)
51
+ rb-fsevent (~> 0.9, >= 0.9.4)
52
+ rb-inotify (~> 0.9, >= 0.9.7)
53
+ ruby_dep (~> 1.2)
54
+ lumberjack (1.0.13)
55
+ method_source (0.9.0)
56
+ minitest (5.11.3)
57
+ nenv (0.3.0)
58
+ notiffany (0.1.1)
59
+ nenv (~> 0.1)
60
+ shellany (~> 0.0)
61
+ parallel (1.12.1)
62
+ parser (2.5.1.2)
63
+ ast (~> 2.4.0)
64
+ powerpack (0.1.2)
65
+ pry (0.11.3)
66
+ coderay (~> 1.1.0)
67
+ method_source (~> 0.9.0)
68
+ rainbow (3.0.0)
69
+ rake (10.5.0)
70
+ rb-fsevent (0.10.3)
71
+ rb-inotify (0.9.10)
72
+ ffi (>= 0.5.0, < 2)
73
+ rspec (3.8.0)
74
+ rspec-core (~> 3.8.0)
75
+ rspec-expectations (~> 3.8.0)
76
+ rspec-mocks (~> 3.8.0)
77
+ rspec-core (3.8.0)
78
+ rspec-support (~> 3.8.0)
79
+ rspec-expectations (3.8.1)
80
+ diff-lcs (>= 1.2.0, < 2.0)
81
+ rspec-support (~> 3.8.0)
82
+ rspec-mocks (3.8.0)
83
+ diff-lcs (>= 1.2.0, < 2.0)
84
+ rspec-support (~> 3.8.0)
85
+ rspec-support (3.8.0)
86
+ rspec_junit_formatter (0.4.1)
87
+ rspec-core (>= 2, < 4, != 2.12.0)
88
+ rubocop (0.59.0)
89
+ jaro_winkler (~> 1.5.1)
90
+ parallel (~> 1.10)
91
+ parser (>= 2.5, != 2.5.1.1)
92
+ powerpack (~> 0.1)
93
+ rainbow (>= 2.2.2, < 4.0)
94
+ ruby-progressbar (~> 1.7)
95
+ unicode-display_width (~> 1.0, >= 1.0.1)
96
+ rubocop-rspec (1.29.1)
97
+ rubocop (>= 0.58.0)
98
+ ruby-progressbar (1.10.0)
99
+ ruby_dep (1.5.0)
100
+ shellany (0.0.1)
101
+ simplecov (0.16.1)
102
+ docile (~> 1.1)
103
+ json (>= 1.8, < 3)
104
+ simplecov-html (~> 0.10.0)
105
+ simplecov-html (0.10.2)
106
+ thor (0.20.0)
107
+ thread_safe (0.3.6)
108
+ tzinfo (1.2.5)
109
+ thread_safe (~> 0.1)
110
+ unicode-display_width (1.4.0)
111
+
112
+ PLATFORMS
113
+ ruby
114
+
115
+ DEPENDENCIES
116
+ birdspotting!
117
+ bundler (~> 1.16)
118
+ guard-rspec
119
+ guard-rubocop
120
+ rake (~> 10.0)
121
+ rspec (~> 3.0)
122
+ rspec_junit_formatter
123
+ rubocop
124
+ rubocop-rspec
125
+ simplecov
126
+
127
+ BUNDLED WITH
128
+ 1.16.1
@@ -0,0 +1,47 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ # Note: The cmd option is now required due to the increasing number of ways
19
+ # rspec may be run, below are examples of the most common uses.
20
+ # * bundler: 'bundle exec rspec'
21
+ # * bundler binstubs: 'bin/rspec'
22
+ # * spring: 'bin/rspec' (This will use spring if running and you have
23
+ # installed the spring binstubs per the docs)
24
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
25
+ # * 'just' rspec: 'rspec'
26
+
27
+ guard :rspec, cmd: "bundle exec rspec", failed_mode: :focus do
28
+ require "guard/rspec/dsl"
29
+ dsl = Guard::RSpec::Dsl.new(self)
30
+
31
+ # Feel free to open issues for suggestions and improvements
32
+
33
+ # RSpec files
34
+ rspec = dsl.rspec
35
+ watch(rspec.spec_helper) { rspec.spec_dir }
36
+ watch(rspec.spec_support) { rspec.spec_dir }
37
+ watch(rspec.spec_files)
38
+
39
+ # Ruby files
40
+ ruby = dsl.ruby
41
+ dsl.watch_spec_files_for(ruby.lib_files)
42
+ end
43
+
44
+ guard :rubocop do
45
+ watch(/.+\.rb$/)
46
+ watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) }
47
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 David Bourguignon
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.
@@ -0,0 +1,140 @@
1
+
2
+ # Birdspotting
3
+
4
+ Some add-ons on `ActiveRecord::Migration` to make migration safer in the context of zero downtime deployment.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'birdspotting'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install birdspotting
21
+
22
+ ## Usage
23
+
24
+ ### Configuration
25
+
26
+ You can configure the gem (for instance by creating a `config/initializers/birdspotting.rb`) with the following options (here with the default values):
27
+
28
+ ```ruby
29
+ Birdspotting.configure do |config|
30
+ config.start_check_at = nil
31
+ config.check_bypass_env_var = "BYPASS_SCHEMA_STATEMENTS_CHECK"
32
+
33
+ config.add_column_position_check = true
34
+
35
+ config.encoding_check = true
36
+ config.encoding_check_message = "\n/!\\ You are dealing with a %<type>s field" \
37
+ "(%<column_name>s): did you think about emojis and used the appropriate encoding? /!\\ \n\n"
38
+
39
+ config.rename_column_check = true
40
+ config.rename_column_message = "Don't use rename_column! https://stackoverflow.com/a/18542147"
41
+
42
+ config.remove_column_check = true
43
+ end
44
+ ```
45
+
46
+ #### Common configuration
47
+
48
+ `start_check_at` allows to start the checks after some migration version only. Set it to a migration
49
+ timestamp like 20151209000000 for instance. When nil, all migrations will be checked.
50
+
51
+ `check_bypass_env_var` specify the ENV var allowing to bypass the checks.
52
+
53
+ ### add_column request position
54
+
55
+ We like to keep or columns organised for the case where we don't use the ORM but some other client.
56
+
57
+ This will raise a `Birdspotting::ColumnPositionMissingError` error if neither `:first` or `:after`
58
+ is in the add_columns option.
59
+
60
+ You can skip this validation by setting `add_column_position_check` to `false`.
61
+
62
+ ### add_column encoding warning
63
+
64
+ This will add a warning when adding a string (or text) column to warn us to think about encoding
65
+ issues. Like do we want to support emojis, or unusual characters?
66
+
67
+ You can skip this validation by setting `encoding_check` to `false`.
68
+ You can customise the warning message by using the `encoding_check_message` setting.
69
+
70
+ ### rename_column
71
+
72
+ By default, we don't want to use the rename column possibility as it will break any live application.
73
+ And we want to be able to release and run migration without downtime.
74
+ Though when a rename_column is used, it will raise a `Birdspotting::RenameColumnForbiddenError`.
75
+
76
+ You can skip this validation by setting `rename_column_check` to `false`.
77
+ You can customise the warning message by using the `rename_column_message` setting.
78
+
79
+ ### remove_column
80
+
81
+ By default, we don't want to be able to remove a columns which is still in use by a the application.
82
+
83
+ Thus we check if the column is still present in the columns list.
84
+
85
+ - If we are not able to find the model, we issue a `Birdspotting::ModelNotFoundError`.
86
+ - If the column is still present in the model, we issue a `Birdspotting::RemoveColumnForbiddenError`.
87
+
88
+ We advise to set the column in the `ignored_columns` of the model.
89
+
90
+ You can skip this validation by setting `remove_column_check` to `false`.
91
+
92
+ ### reorder columns [mySql only]
93
+
94
+ As said above, we like to keep or columns organised for the case where
95
+ we don't use the ORM but some other client.
96
+
97
+ This helper allow to reorder all the columns of a table.
98
+
99
+ Usage:
100
+
101
+ ````ruby
102
+ class ReorderPostsColumns < ActiveRecord::Migration[5.2]
103
+ include Birdspotting::ReorderColumns
104
+
105
+ def change
106
+ reorder_columns_for :posts, %i{
107
+ id
108
+ author
109
+ body
110
+ subject
111
+ posted_at
112
+ created_at
113
+ updated_at
114
+ }
115
+ end
116
+ end
117
+ ````
118
+
119
+ **CAVEAT:**
120
+
121
+ * All columns must be passed in parameters (or it will raise a `Birdspotting::MismatchedColumnsError`).
122
+ * For now, it only works on mysql (or it will raise a `Birdspotting::UnsupportedAdapterError`).
123
+
124
+ ## Development
125
+
126
+ 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.
127
+
128
+ 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).
129
+
130
+ ## Contributing
131
+
132
+ Bug reports and pull requests are welcome on GitHub at https://github.com/drivy/birdspotting. 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.
133
+
134
+ ## License
135
+
136
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
137
+
138
+ ## Code of Conduct
139
+
140
+ Everyone interacting in the Birdspotting project’s codebases and issue trackers is expected to follow the [code of conduct](https://github.com/drivy/birdspotting/blob/master/CODE_OF_CONDUCT.md).
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "birdspotting"
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(__FILE__)
@@ -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
@@ -0,0 +1,33 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "birdspotting/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "birdspotting"
7
+ spec.version = Birdspotting::VERSION
8
+ spec.authors = ["Drivy", "Howard Wilson", "David Bourguignon"]
9
+ spec.email = ["oss@drivy.com"]
10
+
11
+ spec.summary = "Rails migration helpers."
12
+ spec.homepage = "https://github.com/drivy/birdspotting"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
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_development_dependency "bundler", "~> 1.16"
23
+ spec.add_development_dependency "guard-rspec"
24
+ spec.add_development_dependency "guard-rubocop"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "rspec_junit_formatter"
28
+ spec.add_development_dependency "rubocop"
29
+ spec.add_development_dependency "rubocop-rspec"
30
+ spec.add_development_dependency "simplecov"
31
+
32
+ spec.add_runtime_dependency "activerecord", ">= 5.0"
33
+ end
@@ -0,0 +1,21 @@
1
+ require "active_record"
2
+
3
+ require "birdspotting/version"
4
+ require "birdspotting/errors"
5
+ require "birdspotting/configuration"
6
+ require "birdspotting/schema_statements"
7
+ require "birdspotting/reorder_columns"
8
+
9
+ module Birdspotting
10
+ def self.configuration
11
+ @configuration || configure
12
+ end
13
+
14
+ def self.configure(&block)
15
+ @configuration = Configuration.default.tap do |config|
16
+ block&.call(config)
17
+ end
18
+ end
19
+ end
20
+
21
+ ActiveRecord::Migration.prepend Birdspotting::SchemaStatements
@@ -0,0 +1,35 @@
1
+ class Birdspotting::Configuration
2
+ attr_accessor :start_check_at,
3
+ :check_bypass_env_var,
4
+ :add_column_position_check,
5
+ :encoding_check,
6
+ :encoding_check_message,
7
+ :rename_column_check,
8
+ :rename_column_message,
9
+ :remove_column_check
10
+
11
+ def self.default
12
+ new.tap do |config|
13
+ config.start_check_at = nil
14
+ config.check_bypass_env_var = "BYPASS_SCHEMA_STATEMENTS_CHECK"
15
+ config.add_column_position_check = true
16
+ config.encoding_check = true
17
+ config.encoding_check_message = "\n/!\\ You are dealing with a %<type>s field" \
18
+ "(%<column_name>s): did you think about emojis and used the appropriate encoding? /!\\ \n\n"
19
+ config.rename_column_check = true
20
+ config.rename_column_message = "Don't use rename_column! https://stackoverflow.com/a/18542147"
21
+ config.remove_column_check = true
22
+ end
23
+ end
24
+
25
+ %w[
26
+ add_column_position_check
27
+ encoding_check
28
+ rename_column_check
29
+ remove_column_check
30
+ ].each do |name|
31
+ define_method "#{name}?" do
32
+ public_send(name)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,24 @@
1
+ module Birdspotting
2
+
3
+ class Error < StandardError
4
+ end
5
+
6
+ class ColumnPositionMissingError < Error
7
+ end
8
+
9
+ class RemoveColumnForbiddenError < Error
10
+ end
11
+
12
+ class ModelNotFoundError < Error
13
+ end
14
+
15
+ class RenameColumnForbiddenError < Error
16
+ end
17
+
18
+ class UnsupportedAdapterError < Error
19
+ end
20
+
21
+ class MismatchedColumnsError < Error
22
+ end
23
+
24
+ end
@@ -0,0 +1,50 @@
1
+ module Birdspotting::ReorderColumns
2
+
3
+ def reorder_columns_for(table_name, ordered_columns)
4
+ check_if_supported
5
+ check_all_columns_present(ordered_columns, table_name)
6
+
7
+ column_types = column_types(table_name)
8
+
9
+ count = 0
10
+ ordered_columns.zip(ordered_columns.rotate)[0...-1].each do |column, column_after|
11
+ sql_type = column_types[column_after.to_s]
12
+ sql = <<~SQL
13
+ ALTER TABLE #{table_name} MODIFY COLUMN #{column_after} #{sql_type} AFTER #{column}, ALGORITHM=INPLACE, LOCK=NONE
14
+ SQL
15
+
16
+ message = "Moving #{column_after} after #{column} (#{count += 1} of #{ordered_columns.count})"
17
+ say_with_time message do
18
+ ActiveRecord::Base.connection.execute sql
19
+ end
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def check_all_columns_present(ordered_columns, table_name)
26
+ columns = ActiveRecord::Base.connection.columns(table_name)
27
+ return if Set.new(ordered_columns.map(&:to_s)) == Set.new(columns.map(&:name))
28
+
29
+ raise Birdspotting::MismatchedColumnsError,
30
+ "All columns must be present on the #{table_name} table"
31
+ end
32
+
33
+ def column_types(table_name)
34
+ table_definition = ActiveRecord::Base
35
+ .connection
36
+ .execute("SHOW CREATE TABLE `#{table_name}`")
37
+ .to_h[table_name.to_s]
38
+ column_definitions = table_definition.lines.map(&:strip).select { |l| l.starts_with?("`") }
39
+
40
+ Hash[column_definitions.map { |d| d.match(/`(.*)`\s*(.+?)(,|)\z/)[1, 2] }]
41
+ end
42
+
43
+ def check_if_supported
44
+ return if ActiveRecord::Base.connection_config[:adapter] == "mysql2"
45
+
46
+ raise Birdspotting::UnsupportedAdapterError,
47
+ "reorder_columns_for only supported with mysql2 adapter"
48
+ end
49
+
50
+ end
@@ -0,0 +1,96 @@
1
+ module Birdspotting::SchemaStatements
2
+
3
+ def add_column(table_name, column_name, type, options = {})
4
+ add_column_position_check(options)
5
+ encoding_check(column_name, type, options)
6
+
7
+ super
8
+ end
9
+
10
+ def rename_column(*args, **kwargs)
11
+ rename_column_check(kwargs)
12
+
13
+ super(*args)
14
+ end
15
+
16
+ def remove_column(table_name, column_name, type = nil, options = {})
17
+ remove_column_check(column_name, table_name, options)
18
+
19
+ super(table_name, column_name, type, options)
20
+ end
21
+
22
+ private
23
+
24
+ def rename_column_check(options)
25
+ return if bypass_check?(options)
26
+ return unless Birdspotting.configuration.rename_column_check?
27
+
28
+ raise Birdspotting::RenameColumnForbiddenError, Birdspotting.configuration.rename_column_message
29
+ end
30
+
31
+ def add_column_position_check(options)
32
+ return if bypass_check?(options)
33
+ return unless Birdspotting.configuration.add_column_position_check?
34
+ return unless options[:after].nil? && options[:first].nil?
35
+
36
+ raise Birdspotting::ColumnPositionMissingError,
37
+ "The :after or :first option is required when adding columns"
38
+ end
39
+
40
+ def encoding_check(column_name, type, options)
41
+ return if bypass_check?(options)
42
+ return unless Birdspotting.configuration.encoding_check?
43
+ return unless %i[text string].include?(type)
44
+
45
+ STDERR.puts sprintf(
46
+ Birdspotting.configuration.encoding_check_message,
47
+ type: type,
48
+ column_name: column_name
49
+ )
50
+ end
51
+
52
+ def remove_column_check(column_name, table_name, options)
53
+ return if bypass_check?(options)
54
+ return unless Birdspotting.configuration.remove_column_check?
55
+
56
+ model = model_for(table_name)
57
+
58
+ unless model
59
+ raise Birdspotting::ModelNotFoundError,
60
+ "No model for `#{table_name}` table could be found. " \
61
+ "Is the associated model preloaded?" \
62
+ "Call the model at the beginning of the migration to ensure it is loaded." \
63
+ "Or use the :bypass_schema_statements_check option " \
64
+ "if you're sure of what you are doing."
65
+ end
66
+
67
+ if model.columns.map(&:name).include?(column_name.to_s) # rubocop:disable Style/GuardClause
68
+ raise Birdspotting::RemoveColumnForbiddenError,
69
+ "`#{column_name}` column should be added to ignored_columns in `#{model.name}` model" \
70
+ " before being removed. Use #{Birdspotting.configuration.check_bypass_env_var}" \
71
+ " env variable if you're sure of what you are doing."
72
+ end
73
+ end
74
+
75
+ def model_for(table_name)
76
+ model = ActiveRecord::Base.descendants.find { |t| t.table_name == table_name.to_s }
77
+ model || begin
78
+ table_name.to_s.classify.constantize
79
+ rescue StandardError
80
+ nil
81
+ end
82
+ end
83
+
84
+ def bypass_check?(options = {})
85
+ checkable_version? ||
86
+ options.delete(:bypass_schema_statements_check) ||
87
+ ENV.key?(Birdspotting.configuration.check_bypass_env_var)
88
+ end
89
+
90
+ def checkable_version?
91
+ version &&
92
+ Birdspotting.configuration.start_check_at &&
93
+ version <= Birdspotting.configuration.start_check_at
94
+ end
95
+
96
+ end
@@ -0,0 +1,3 @@
1
+ module Birdspotting
2
+ VERSION = "0.1.0".freeze
3
+ end
metadata ADDED
@@ -0,0 +1,207 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: birdspotting
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Drivy
8
+ - Howard Wilson
9
+ - David Bourguignon
10
+ autorequire:
11
+ bindir: exe
12
+ cert_chain: []
13
+ date: 2018-09-14 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '1.16'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '1.16'
29
+ - !ruby/object:Gem::Dependency
30
+ name: guard-rspec
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ - !ruby/object:Gem::Dependency
44
+ name: guard-rubocop
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: rake
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '10.0'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: '10.0'
71
+ - !ruby/object:Gem::Dependency
72
+ name: rspec
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '3.0'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - "~>"
83
+ - !ruby/object:Gem::Version
84
+ version: '3.0'
85
+ - !ruby/object:Gem::Dependency
86
+ name: rspec_junit_formatter
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ - !ruby/object:Gem::Dependency
100
+ name: rubocop
101
+ requirement: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ type: :development
107
+ prerelease: false
108
+ version_requirements: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ - !ruby/object:Gem::Dependency
114
+ name: rubocop-rspec
115
+ requirement: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ type: :development
121
+ prerelease: false
122
+ version_requirements: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ - !ruby/object:Gem::Dependency
128
+ name: simplecov
129
+ requirement: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ - !ruby/object:Gem::Dependency
142
+ name: activerecord
143
+ requirement: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '5.0'
148
+ type: :runtime
149
+ prerelease: false
150
+ version_requirements: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '5.0'
155
+ description:
156
+ email:
157
+ - oss@drivy.com
158
+ executables: []
159
+ extensions: []
160
+ extra_rdoc_files: []
161
+ files:
162
+ - ".circleci/config.yml"
163
+ - ".gitignore"
164
+ - ".rspec"
165
+ - ".rubocop.yml"
166
+ - ".ruby-version"
167
+ - CODE_OF_CONDUCT.md
168
+ - Gemfile
169
+ - Gemfile.lock
170
+ - Guardfile
171
+ - LICENSE.txt
172
+ - README.md
173
+ - Rakefile
174
+ - bin/console
175
+ - bin/setup
176
+ - birdspotting.gemspec
177
+ - lib/birdspotting.rb
178
+ - lib/birdspotting/configuration.rb
179
+ - lib/birdspotting/errors.rb
180
+ - lib/birdspotting/reorder_columns.rb
181
+ - lib/birdspotting/schema_statements.rb
182
+ - lib/birdspotting/version.rb
183
+ homepage: https://github.com/drivy/birdspotting
184
+ licenses:
185
+ - MIT
186
+ metadata: {}
187
+ post_install_message:
188
+ rdoc_options: []
189
+ require_paths:
190
+ - lib
191
+ required_ruby_version: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - ">="
194
+ - !ruby/object:Gem::Version
195
+ version: '0'
196
+ required_rubygems_version: !ruby/object:Gem::Requirement
197
+ requirements:
198
+ - - ">="
199
+ - !ruby/object:Gem::Version
200
+ version: '0'
201
+ requirements: []
202
+ rubyforge_project:
203
+ rubygems_version: 2.7.6
204
+ signing_key:
205
+ specification_version: 4
206
+ summary: Rails migration helpers.
207
+ test_files: []