pronto-rails_migrations_annotated 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2f2b2a7302b5a18526b94a83ecd5b43783c31ea3dd1bc4d65987d7e8e9f5c637
4
- data.tar.gz: 954ec3c7b49dad59c247f76d74c787ce1db6761230710fe4fa7d8169b193bb03
3
+ metadata.gz: d2b901020822988af989df4a5c7380a1d0888436b99a1b2ccd835e9b08a88875
4
+ data.tar.gz: 51392445670ec4052e0ad6790a6499cf71d62b4d5354a48e27df8ed036ab5f34
5
5
  SHA512:
6
- metadata.gz: 194af503a81830b608c5068d9f26b18b3692833dd55e065bb913acbe4262f633027831133873f5f8b3263f18a748515edeedbf1cea9a7551bf8507b912ba6f45
7
- data.tar.gz: 8f5ebbd09ebb84f7b0dc9b7940f0aed70cc56efa027393b64b2c4806f195c86fb4778286f98371443b63db8f9dfa8d49a8b035f37c6bbe32ad29ca177a2876bd
6
+ metadata.gz: 9b50a79d424571f695fe1faeb208ab10d0b474ab161d26c9128cfe355cbaf4de58000cb373daa64b234ac0bd0e2a7b8744ee2dec000f80785c673bdd067db353
7
+ data.tar.gz: 4b2b7867aced9e74a06c855337437b802407479d67bb805da19ec502079bdebe748e8f185f383c99338c74d9e9d8194461100c55374b0f1c93bd35a840610143
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pronto-rails_migrations_annotated (0.0.1)
4
+ pronto-rails_migrations_annotated (0.0.2)
5
5
  pronto (~> 0.11)
6
6
 
7
7
  GEM
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Pronto
4
4
  module RailsMigrationsAnnotatedVersion
5
- VERSION = "0.0.1"
5
+ VERSION = "0.0.2"
6
6
  end
7
7
  end
@@ -12,6 +12,7 @@ module Pronto
12
12
 
13
13
  check_migrations_mixed_with_code
14
14
  check_migration_version_numbers
15
+ check_large_schema_diff
15
16
 
16
17
  @messages
17
18
  end
@@ -40,6 +41,23 @@ module Pronto
40
41
  end
41
42
  end
42
43
 
44
+ def diff_threshold
45
+ # TODO: some config for this?
46
+ 200
47
+ end
48
+
49
+ def check_large_schema_diff
50
+ return unless diff_threshold
51
+
52
+ if schema_patches.sum(&:additions) >= diff_threshold || schema_patches.sum(&:deletions) >= diff_threshold
53
+ add_message_at_patch(schema_patches.first, "Large schema diff, pay attention")
54
+ end
55
+
56
+ if structure_patches.sum(&:additions) >= diff_threshold || structure_patches.sum(&:deletions) >= diff_threshold
57
+ add_message_at_patch(structure_patches.first, "Large structure diff, pay attention")
58
+ end
59
+ end
60
+
43
61
  def check_migrations_mixed_with_code
44
62
  return unless migration_patches.any? && non_migration_related_patches.any?
45
63
 
@@ -52,45 +70,46 @@ module Pronto
52
70
  version_numbers = migration_patches.map { |patch| patch.delta.new_file[:path].sub(/^[^0-9]*([0-9]+).+/, '\1') }
53
71
 
54
72
  schema_file_name = 'db/schema.rb'
55
- if File.exist?(schema_file_name)
73
+ if File.exist?(schema_file_name) && !gitignored?(schema_file_name)
56
74
  if schema_patches.none?
57
75
  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
76
  else
71
- add_message_at_patch(migration_patches.first, "Cannot detect schema migration version", :warning)
77
+ match = File.read(schema_file_name).match(%r{ActiveRecord::Schema.define\(version:\s*(?<version>[0-9_]+)\s*\)})
78
+ if match
79
+ schema_migration_version = match[:version]
80
+ schema_migration_version_clean = schema_migration_version.gsub(/[^0-9]/, '')
81
+ version_numbers.select { |version| version > schema_migration_version_clean }.each do |wrong_version|
82
+ add_message_at_patch(
83
+ migration_patches.first { |patch| patch.delta.new_file[:path].include?(wrong_version) },
84
+ "Migration version #{wrong_version} is above schema.rb version #{schema_migration_version}"
85
+ )
86
+ end
87
+ else
88
+ add_message_at_patch(migration_patches.first, "Cannot detect schema migration version", :warning)
89
+ end
72
90
  end
73
91
  end
74
92
 
75
93
  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
-
94
+ if File.exist?(structure_file_name) && !gitignored?(structure_file_name)
81
95
  migration_line_regex = /\A\s*\('(?<version>[0-9]{14})'\)/
82
96
  structure_file_lines = File.readlines(structure_file_name)
83
97
  versions_from_schema = structure_file_lines.select{|line| line =~ migration_line_regex }
84
98
 
85
-
99
+ missing_from_structure = false
86
100
  version_numbers.select { |version| versions_from_schema.none? { |strct_ver| strct_ver.include?(version) } }
87
101
  .each do |wrong_version|
102
+ missing_from_structure = true
88
103
  add_message_at_patch(
89
104
  migration_patches.first { |patch| patch.delta.new_file[:path].include?(wrong_version) },
90
- "Migration #{wrong_version} is missing from structure.sql"
105
+ "Migration #{wrong_version} is missing from structure.sql", :error
91
106
  )
92
107
  end
93
108
 
109
+ if structure_patches.none? && !missing_from_structure
110
+ add_message_at_patch(migration_patches, "Migration file detected, but no changes in structure.sql")
111
+ end
112
+
94
113
  structure_patches.each do |patch|
95
114
  patch.added_lines.each do |line|
96
115
  next unless line.content.match?(migration_line_regex)
@@ -113,8 +132,9 @@ module Pronto
113
132
  )
114
133
  end
115
134
 
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/)
135
+ if structure_patches.any? &&
136
+ !(structure_file_lines.last(2) == ["\n", "\n"] && structure_file_lines[-3] != "\n") &&
137
+ !structure_file_lines.last.match?(/\A\s*[^\s]+\s*\n/)
118
138
  add_message_at_patch(structure_patches.last,
119
139
  "structure.sql must end with a newline or 2 empty lines", line: :last)
120
140
  end
@@ -122,6 +142,13 @@ module Pronto
122
142
 
123
143
  end
124
144
 
145
+ def gitignored?(path)
146
+ # TODO: get this from rugged (but it's not exposed to plugins) or make more compatible
147
+ `git check-ignore #{path}` != ""
148
+ rescue Errno::ENOENT # when git not present
149
+ nil
150
+ end
151
+
125
152
  def migration_patches
126
153
  # nb: there may be engines added to migrations_paths in config or database.yml
127
154
  # but cannot check for this without more knowledge into the particular app
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pronto-rails_migrations_annotated
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vasily Fedoseyev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-11 00:00:00.000000000 Z
11
+ date: 2021-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pronto