pronto-rails_migrations_annotated 0.0.1

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
+ SHA256:
3
+ metadata.gz: 2f2b2a7302b5a18526b94a83ecd5b43783c31ea3dd1bc4d65987d7e8e9f5c637
4
+ data.tar.gz: 954ec3c7b49dad59c247f76d74c787ce1db6761230710fe4fa7d8169b193bb03
5
+ SHA512:
6
+ metadata.gz: 194af503a81830b608c5068d9f26b18b3692833dd55e065bb913acbe4262f633027831133873f5f8b3263f18a748515edeedbf1cea9a7551bf8507b912ba6f45
7
+ data.tar.gz: 8f5ebbd09ebb84f7b0dc9b7940f0aed70cc56efa027393b64b2c4806f195c86fb4778286f98371443b63db8f9dfa8d49a8b035f37c6bbe32ad29ca177a2876bd
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in pronto-rails_migrations_annotated.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,108 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ pronto-rails_migrations_annotated (0.0.1)
5
+ pronto (~> 0.11)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.8.0)
11
+ public_suffix (>= 2.0.2, < 5.0)
12
+ ast (2.4.2)
13
+ diff-lcs (1.4.4)
14
+ faraday (1.8.0)
15
+ faraday-em_http (~> 1.0)
16
+ faraday-em_synchrony (~> 1.0)
17
+ faraday-excon (~> 1.1)
18
+ faraday-httpclient (~> 1.0.1)
19
+ faraday-net_http (~> 1.0)
20
+ faraday-net_http_persistent (~> 1.1)
21
+ faraday-patron (~> 1.0)
22
+ faraday-rack (~> 1.0)
23
+ multipart-post (>= 1.2, < 3)
24
+ ruby2_keywords (>= 0.0.4)
25
+ faraday-em_http (1.0.0)
26
+ faraday-em_synchrony (1.0.0)
27
+ faraday-excon (1.1.0)
28
+ faraday-httpclient (1.0.1)
29
+ faraday-net_http (1.0.1)
30
+ faraday-net_http_persistent (1.2.0)
31
+ faraday-patron (1.0.0)
32
+ faraday-rack (1.0.0)
33
+ gitlab (4.17.0)
34
+ httparty (~> 0.18)
35
+ terminal-table (~> 1.5, >= 1.5.1)
36
+ httparty (0.20.0)
37
+ mime-types (~> 3.0)
38
+ multi_xml (>= 0.5.2)
39
+ mime-types (3.4.1)
40
+ mime-types-data (~> 3.2015)
41
+ mime-types-data (3.2021.1115)
42
+ multi_xml (0.6.0)
43
+ multipart-post (2.1.1)
44
+ octokit (4.21.0)
45
+ faraday (>= 0.9)
46
+ sawyer (~> 0.8.0, >= 0.5.3)
47
+ parallel (1.21.0)
48
+ parser (3.0.2.0)
49
+ ast (~> 2.4.1)
50
+ pronto (0.11.0)
51
+ gitlab (~> 4.4, >= 4.4.0)
52
+ httparty (>= 0.13.7)
53
+ octokit (~> 4.7, >= 4.7.0)
54
+ rainbow (>= 2.2, < 4.0)
55
+ rexml (~> 3.2)
56
+ rugged (>= 0.23.0, < 1.1.0)
57
+ thor (>= 0.20.3, < 2.0)
58
+ public_suffix (4.0.6)
59
+ rainbow (3.0.0)
60
+ rake (13.0.6)
61
+ regexp_parser (2.1.1)
62
+ rexml (3.2.5)
63
+ rspec (3.10.0)
64
+ rspec-core (~> 3.10.0)
65
+ rspec-expectations (~> 3.10.0)
66
+ rspec-mocks (~> 3.10.0)
67
+ rspec-core (3.10.1)
68
+ rspec-support (~> 3.10.0)
69
+ rspec-expectations (3.10.1)
70
+ diff-lcs (>= 1.2.0, < 2.0)
71
+ rspec-support (~> 3.10.0)
72
+ rspec-mocks (3.10.2)
73
+ diff-lcs (>= 1.2.0, < 2.0)
74
+ rspec-support (~> 3.10.0)
75
+ rspec-support (3.10.3)
76
+ rubocop (1.23.0)
77
+ parallel (~> 1.10)
78
+ parser (>= 3.0.0.0)
79
+ rainbow (>= 2.2.2, < 4.0)
80
+ regexp_parser (>= 1.8, < 3.0)
81
+ rexml
82
+ rubocop-ast (>= 1.12.0, < 2.0)
83
+ ruby-progressbar (~> 1.7)
84
+ unicode-display_width (>= 1.4.0, < 3.0)
85
+ rubocop-ast (1.13.0)
86
+ parser (>= 3.0.1.1)
87
+ ruby-progressbar (1.11.0)
88
+ ruby2_keywords (0.0.5)
89
+ rugged (1.0.1)
90
+ sawyer (0.8.2)
91
+ addressable (>= 2.3.5)
92
+ faraday (> 0.8, < 2.0)
93
+ terminal-table (1.8.0)
94
+ unicode-display_width (~> 1.1, >= 1.1.1)
95
+ thor (1.1.0)
96
+ unicode-display_width (1.8.0)
97
+
98
+ PLATFORMS
99
+ x86_64-darwin-21
100
+
101
+ DEPENDENCIES
102
+ pronto-rails_migrations_annotated!
103
+ rake (~> 13.0)
104
+ rspec (~> 3.0)
105
+ rubocop (~> 1.21)
106
+
107
+ BUNDLED WITH
108
+ 2.2.31
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Vasily Fedoseyev
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,38 @@
1
+ # Pronto::RailsMigrationsAnnotated
2
+
3
+ Pronto runner to enforce migrations to be in separate PRs, but allow schema annotations.
4
+ Also check for obvious things like adding migration number that is not present in PR to structure.sql.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'pronto-rails_migrations_annotated'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install pronto-rails_migrations_annotated
21
+
22
+ ## Usage
23
+
24
+ When in gemfile - pronto should pick up this runner automatically.
25
+
26
+ ## Development
27
+
28
+ 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.
29
+
30
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Vasfed/pronto-rails_migrations_annotated.
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pronto
4
+ module RailsMigrationsAnnotatedVersion
5
+ VERSION = "0.0.1"
6
+ end
7
+ end
@@ -0,0 +1,155 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pronto'
4
+ require_relative "rails_migrations_annotated/version"
5
+
6
+ module Pronto
7
+ class RailsMigrationsAnnotated < Runner
8
+ VERSION = Pronto::RailsMigrationsAnnotatedVersion::VERSION
9
+
10
+ def run
11
+ @messages = []
12
+
13
+ check_migrations_mixed_with_code
14
+ check_migration_version_numbers
15
+
16
+ @messages
17
+ end
18
+
19
+ # todo: override def self.title ?
20
+
21
+ private
22
+
23
+ def add_message_at_patch(patches, message, level = :warning, line: :first)
24
+ patches = [patches] unless patches.is_a?(Array)
25
+ patches.each do |patch|
26
+ target_line = case line
27
+ when :first then patch.added_lines.first || patch.lines.first
28
+ when :last then patch.added_lines.last || patch.lines.last
29
+ else line
30
+ end
31
+
32
+ @messages << Message.new(
33
+ patch.delta.new_file[:path],
34
+ target_line,
35
+ level,
36
+ message,
37
+ nil,
38
+ self.class
39
+ )
40
+ end
41
+ end
42
+
43
+ def check_migrations_mixed_with_code
44
+ return unless migration_patches.any? && non_migration_related_patches.any?
45
+
46
+ add_message_at_patch(migration_patches, "Do not mix migrations with other stuff", :fatal)
47
+ end
48
+
49
+ def check_migration_version_numbers
50
+ return unless migration_patches.any?
51
+
52
+ version_numbers = migration_patches.map { |patch| patch.delta.new_file[:path].sub(/^[^0-9]*([0-9]+).+/, '\1') }
53
+
54
+ schema_file_name = 'db/schema.rb'
55
+ if File.exist?(schema_file_name)
56
+ if schema_patches.none?
57
+ add_message_at_patch(migration_patches, "Migration file detected, but no changes in schema.rb", :error)
58
+ end
59
+
60
+ match = File.read(schema_file_name).match(%r{ActiveRecord::Schema.define\(version:\s*(?<version>[0-9_]+)\s*\)})
61
+ if match
62
+ schema_migration_version = match[:version]
63
+ schema_migration_version_clean = schema_migration_version.gsub(/[^0-9]/, '')
64
+ version_numbers.select { |version| version > schema_migration_version_clean }.each do |wrong_version|
65
+ add_message_at_patch(
66
+ migration_patches.first { |patch| patch.delta.new_file[:path].include?(wrong_version) },
67
+ "Migration version #{wrong_version} is above schema.rb version #{schema_migration_version}"
68
+ )
69
+ end
70
+ else
71
+ add_message_at_patch(migration_patches.first, "Cannot detect schema migration version", :warning)
72
+ end
73
+ end
74
+
75
+ structure_file_name = 'db/structure.sql'
76
+ if File.exist?(structure_file_name)
77
+ if structure_patches.none?
78
+ add_message_at_patch(migration_patches, "Migration file detected, but no changes in structure.sql", :error)
79
+ end
80
+
81
+ migration_line_regex = /\A\s*\('(?<version>[0-9]{14})'\)/
82
+ structure_file_lines = File.readlines(structure_file_name)
83
+ versions_from_schema = structure_file_lines.select{|line| line =~ migration_line_regex }
84
+
85
+
86
+ version_numbers.select { |version| versions_from_schema.none? { |strct_ver| strct_ver.include?(version) } }
87
+ .each do |wrong_version|
88
+ add_message_at_patch(
89
+ migration_patches.first { |patch| patch.delta.new_file[:path].include?(wrong_version) },
90
+ "Migration #{wrong_version} is missing from structure.sql"
91
+ )
92
+ end
93
+
94
+ structure_patches.each do |patch|
95
+ patch.added_lines.each do |line|
96
+ next unless line.content.match?(migration_line_regex)
97
+ match = line.content.match(migration_line_regex)
98
+ version = match[:version]
99
+ next if version_numbers.include?(version) ||
100
+ line.content.end_with?(',') && patch.deleted_lines.any? { |deleted| deleted.content.include?(version) }
101
+
102
+ add_message_at_patch(patch, "Migration #{version} is not present in this changeset", :error, line: line)
103
+ end
104
+ end
105
+
106
+ bad_semicolon = !versions_from_schema.last.end_with?("');\n")
107
+ unsorted_migrations = versions_from_schema != versions_from_schema.sort
108
+
109
+ if bad_semicolon || unsorted_migrations
110
+ add_message_at_patch(
111
+ structure_patches.first{ |patch| patch.lines.any?{ |line| line.content.match?(migration_line_regex) } },
112
+ "Migration versions must be sorted and have correct syntax"
113
+ )
114
+ end
115
+
116
+ unless structure_file_lines.last(2) == ["\n", "\n"] && structure_file_lines[-3] != "\n" ||
117
+ structure_file_lines.last.match?(/\A\s*[^\s]\s*\n/)
118
+ add_message_at_patch(structure_patches.last,
119
+ "structure.sql must end with a newline or 2 empty lines", line: :last)
120
+ end
121
+ end
122
+
123
+ end
124
+
125
+ def migration_patches
126
+ # nb: there may be engines added to migrations_paths in config or database.yml
127
+ # but cannot check for this without more knowledge into the particular app
128
+ # rails uses Dir[*paths.flat_map { |path| "#{path}/**/[0-9]*_*.rb" }]
129
+ @migration_patches ||= @patches.select do |patch|
130
+ patch.delta.added? && patch.delta.new_file[:path] =~ %r{db/migrate/.*[0-9]+_\w+.rb}
131
+ end
132
+ end
133
+
134
+ def non_migration_related_patches
135
+ @patches.reject do |patch|
136
+ path = patch.delta.new_file[:path]
137
+ # lines = added_lines + deleted_lines, allow edits in comments
138
+ path =~ %r{db/migrate/.*[0-9]+_\w+.rb} || path =~ %r{db/schema.rb} || path =~ %r{db/structure.sql} ||
139
+ path.end_with?('.rb') && patch.lines.all? do |line|
140
+ !(line.addition? || line.deletion?) ||
141
+ (line.content =~ /\A\s*#/ || line.content =~ /\A\s*\z/)
142
+ end
143
+ end
144
+ end
145
+
146
+ def schema_patches
147
+ @schema_patches = @patches.select { |patch| patch.delta.new_file[:path] =~ %r{db/schema.rb} }
148
+ end
149
+
150
+ def structure_patches
151
+ @structure_patches = @patches.select { |patch| patch.delta.new_file[:path] =~ %r{db/structure.sql} }
152
+ end
153
+
154
+ end
155
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/pronto/rails_migrations_annotated/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "pronto-rails_migrations_annotated"
7
+ spec.version = Pronto::RailsMigrationsAnnotatedVersion::VERSION
8
+ spec.authors = ["Vasily Fedoseyev"]
9
+ spec.email = ["vasilyfedoseyev@gmail.com"]
10
+
11
+ spec.summary = "Pronto runner to check rails migrations to be separate from other code, but allow annotations"
12
+ spec.description = <<~TEXT
13
+ For project stability it's a common practive to release db migrations separated from other code changes.
14
+ This pronto runner helps to enforce this. Also checks other migration PR basic sanity things.
15
+ TEXT
16
+ spec.homepage = "https://github.com/Vasfed/pronto-rails_migrations_annotated"
17
+ spec.license = "MIT"
18
+ spec.required_ruby_version = ">= 2.3.0"
19
+
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = "https://github.com/Vasfed/pronto-rails_migrations_annotated"
22
+ # spec.metadata["changelog_uri"] = "TODO: Put CHANGELOG.md URL here once there's a changelog"
23
+
24
+ # Specify which files should be added to the gem when it is released.
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
27
+ `git ls-files -z`.split("\x0").reject do |f|
28
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
29
+ end
30
+ end
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency "pronto", '~>0.11'
34
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pronto-rails_migrations_annotated
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Vasily Fedoseyev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-12-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pronto
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.11'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.11'
27
+ description: |
28
+ For project stability it's a common practive to release db migrations separated from other code changes.
29
+ This pronto runner helps to enforce this. Also checks other migration PR basic sanity things.
30
+ email:
31
+ - vasilyfedoseyev@gmail.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - ".rspec"
37
+ - ".rubocop.yml"
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - lib/pronto/rails_migrations_annotated.rb
44
+ - lib/pronto/rails_migrations_annotated/version.rb
45
+ - pronto-rails_migrations_annotated.gemspec
46
+ homepage: https://github.com/Vasfed/pronto-rails_migrations_annotated
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ homepage_uri: https://github.com/Vasfed/pronto-rails_migrations_annotated
51
+ source_code_uri: https://github.com/Vasfed/pronto-rails_migrations_annotated
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 2.3.0
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.0.9
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: Pronto runner to check rails migrations to be separate from other code, but
71
+ allow annotations
72
+ test_files: []