lol_dba 2.1.5 → 2.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +179 -0
  3. data/.gitignore +1 -1
  4. data/.rspec +2 -0
  5. data/.rubocop.yml +7 -0
  6. data/.travis.yml +16 -9
  7. data/Appraisals +19 -12
  8. data/Gemfile +1 -8
  9. data/Gemfile.lock +60 -59
  10. data/README.md +5 -2
  11. data/Rakefile +4 -4
  12. data/bin/lol_dba +2 -2
  13. data/gemfiles/rails_3_2.gemfile +0 -3
  14. data/gemfiles/rails_3_2.gemfile.lock +33 -31
  15. data/gemfiles/rails_4_0.gemfile +0 -3
  16. data/gemfiles/rails_4_0.gemfile.lock +31 -27
  17. data/gemfiles/rails_4_1.gemfile +0 -3
  18. data/gemfiles/rails_4_1.gemfile.lock +29 -26
  19. data/gemfiles/rails_4_2.gemfile +0 -3
  20. data/gemfiles/rails_4_2.gemfile.lock +52 -48
  21. data/gemfiles/rails_5_0.gemfile +0 -3
  22. data/gemfiles/rails_5_0.gemfile.lock +55 -51
  23. data/gemfiles/rails_5_1.gemfile +0 -3
  24. data/gemfiles/rails_5_1.gemfile.lock +57 -53
  25. data/gemfiles/rails_5_2.gemfile +9 -0
  26. data/gemfiles/rails_5_2.gemfile.lock +115 -0
  27. data/lib/lol_dba/cli.rb +26 -15
  28. data/lib/lol_dba/index_finding/belongs_to.rb +32 -0
  29. data/lib/lol_dba/index_finding/error_logging.rb +15 -0
  30. data/lib/lol_dba/index_finding/has_and_belongs_to_many.rb +20 -0
  31. data/lib/lol_dba/index_finding/has_many.rb +47 -0
  32. data/lib/lol_dba/index_finding/index_finder.rb +81 -0
  33. data/lib/lol_dba/index_finding/migration_formatter.rb +50 -0
  34. data/lib/lol_dba/index_finding/relation_inspector.rb +30 -0
  35. data/lib/lol_dba/index_finding/relation_inspector_factory.rb +13 -0
  36. data/lib/lol_dba/rails_compatibility.rb +47 -0
  37. data/lib/lol_dba/railtie.rb +2 -2
  38. data/lib/lol_dba/sql_migrations/migration.rb +51 -0
  39. data/lib/lol_dba/sql_migrations/migration_mocker.rb +70 -0
  40. data/lib/lol_dba/sql_migrations/sql_generator.rb +49 -0
  41. data/lib/lol_dba/sql_migrations/writer.rb +29 -0
  42. data/lib/lol_dba/version.rb +1 -1
  43. data/lib/lol_dba.rb +14 -188
  44. data/lib/tasks/lol_dba.rake +7 -8
  45. data/lol_dba.gemspec +19 -16
  46. data/spec/associations_index_spec.rb +43 -56
  47. data/spec/fixtures/app/models/address.rb +0 -1
  48. data/spec/fixtures/app/models/billable_week.rb +1 -3
  49. data/spec/fixtures/app/models/company.rb +0 -1
  50. data/spec/fixtures/app/models/complex_billable_week.rb +0 -1
  51. data/spec/fixtures/app/models/complex_timesheet.rb +0 -1
  52. data/spec/fixtures/app/models/country.rb +1 -1
  53. data/spec/fixtures/app/models/favourite.rb +0 -2
  54. data/spec/fixtures/app/models/freelancer.rb +1 -3
  55. data/spec/fixtures/app/models/gift.rb +0 -2
  56. data/spec/fixtures/app/models/god.rb +1 -1
  57. data/spec/fixtures/app/models/project.rb +0 -2
  58. data/spec/fixtures/app/models/timesheet.rb +1 -3
  59. data/spec/fixtures/app/models/user.rb +0 -2
  60. data/spec/fixtures/app/models/worker.rb +0 -1
  61. data/spec/fixtures/app/models/worker_user.rb +2 -3
  62. data/spec/fixtures/schema.rb +53 -53
  63. data/spec/migration_formatter_spec.rb +66 -0
  64. data/spec/spec_helper.rb +17 -9
  65. data/spec/sql_generator_spec.rb +9 -0
  66. metadata +88 -19
  67. data/lib/lol_dba/migration.rb +0 -40
  68. data/lib/lol_dba/sql_generator.rb +0 -96
  69. data/lib/lol_dba/writer.rb +0 -25
  70. data/spec/common_function_spec.rb +0 -96
metadata CHANGED
@@ -1,26 +1,25 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lol_dba
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.5
4
+ version: 2.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Diego Plentz
8
- - Elad Meidar
9
- - Eric Davis
10
- - Muness Alrubaie
11
- - Vladimir Sharshov
12
8
  autorequire:
13
9
  bindir: bin
14
10
  cert_chain: []
15
- date: 2018-03-04 00:00:00.000000000 Z
11
+ date: 2019-04-03 00:00:00.000000000 Z
16
12
  dependencies:
17
13
  - !ruby/object:Gem::Dependency
18
- name: activerecord
14
+ name: actionpack
19
15
  requirement: !ruby/object:Gem::Requirement
20
16
  requirements:
21
17
  - - ">="
22
18
  - !ruby/object:Gem::Version
23
19
  version: '3.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6.0'
24
23
  type: :runtime
25
24
  prerelease: false
26
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -28,13 +27,19 @@ dependencies:
28
27
  - - ">="
29
28
  - !ruby/object:Gem::Version
30
29
  version: '3.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6.0'
31
33
  - !ruby/object:Gem::Dependency
32
- name: actionpack
34
+ name: activerecord
33
35
  requirement: !ruby/object:Gem::Requirement
34
36
  requirements:
35
37
  - - ">="
36
38
  - !ruby/object:Gem::Version
37
39
  version: '3.0'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '6.0'
38
43
  type: :runtime
39
44
  prerelease: false
40
45
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,6 +47,9 @@ dependencies:
42
47
  - - ">="
43
48
  - !ruby/object:Gem::Version
44
49
  version: '3.0'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '6.0'
45
53
  - !ruby/object:Gem::Dependency
46
54
  name: railties
47
55
  requirement: !ruby/object:Gem::Requirement
@@ -49,6 +57,9 @@ dependencies:
49
57
  - - ">="
50
58
  - !ruby/object:Gem::Version
51
59
  version: '3.0'
60
+ - - "<"
61
+ - !ruby/object:Gem::Version
62
+ version: '6.0'
52
63
  type: :runtime
53
64
  prerelease: false
54
65
  version_requirements: !ruby/object:Gem::Requirement
@@ -56,8 +67,53 @@ dependencies:
56
67
  - - ">="
57
68
  - !ruby/object:Gem::Version
58
69
  version: '3.0'
70
+ - - "<"
71
+ - !ruby/object:Gem::Version
72
+ version: '6.0'
59
73
  - !ruby/object:Gem::Dependency
60
74
  name: appraisal
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '2.2'
80
+ type: :development
81
+ prerelease: false
82
+ version_requirements: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - "~>"
85
+ - !ruby/object:Gem::Version
86
+ version: '2.2'
87
+ - !ruby/object:Gem::Dependency
88
+ name: simplecov
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - "~>"
92
+ - !ruby/object:Gem::Version
93
+ version: '0.1'
94
+ type: :development
95
+ prerelease: false
96
+ version_requirements: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - "~>"
99
+ - !ruby/object:Gem::Version
100
+ version: '0.1'
101
+ - !ruby/object:Gem::Dependency
102
+ name: sqlite3
103
+ requirement: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - "~>"
106
+ - !ruby/object:Gem::Version
107
+ version: 1.3.5
108
+ type: :development
109
+ prerelease: false
110
+ version_requirements: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - "~>"
113
+ - !ruby/object:Gem::Version
114
+ version: 1.3.5
115
+ - !ruby/object:Gem::Dependency
116
+ name: rspec-rails
61
117
  requirement: !ruby/object:Gem::Requirement
62
118
  requirements:
63
119
  - - ">="
@@ -74,16 +130,16 @@ description: lol_dba is a small package of rake tasks that scan your application
74
130
  and displays a list of columns that probably should be indexed. Also, it can generate
75
131
  .sql migration scripts.
76
132
  email:
77
- - diego@plentz.org
78
- - elad@eizesus.com
79
- - ''
80
- - vsharshov@gmail.com
133
+ - diego@plentz.io
81
134
  executables:
82
135
  - lol_dba
83
136
  extensions: []
84
137
  extra_rdoc_files: []
85
138
  files:
139
+ - ".codeclimate.yml"
86
140
  - ".gitignore"
141
+ - ".rspec"
142
+ - ".rubocop.yml"
87
143
  - ".travis.yml"
88
144
  - Appraisals
89
145
  - Gemfile
@@ -103,17 +159,28 @@ files:
103
159
  - gemfiles/rails_5_0.gemfile.lock
104
160
  - gemfiles/rails_5_1.gemfile
105
161
  - gemfiles/rails_5_1.gemfile.lock
162
+ - gemfiles/rails_5_2.gemfile
163
+ - gemfiles/rails_5_2.gemfile.lock
106
164
  - lib/lol_dba.rb
107
165
  - lib/lol_dba/cli.rb
108
- - lib/lol_dba/migration.rb
166
+ - lib/lol_dba/index_finding/belongs_to.rb
167
+ - lib/lol_dba/index_finding/error_logging.rb
168
+ - lib/lol_dba/index_finding/has_and_belongs_to_many.rb
169
+ - lib/lol_dba/index_finding/has_many.rb
170
+ - lib/lol_dba/index_finding/index_finder.rb
171
+ - lib/lol_dba/index_finding/migration_formatter.rb
172
+ - lib/lol_dba/index_finding/relation_inspector.rb
173
+ - lib/lol_dba/index_finding/relation_inspector_factory.rb
174
+ - lib/lol_dba/rails_compatibility.rb
109
175
  - lib/lol_dba/railtie.rb
110
- - lib/lol_dba/sql_generator.rb
176
+ - lib/lol_dba/sql_migrations/migration.rb
177
+ - lib/lol_dba/sql_migrations/migration_mocker.rb
178
+ - lib/lol_dba/sql_migrations/sql_generator.rb
179
+ - lib/lol_dba/sql_migrations/writer.rb
111
180
  - lib/lol_dba/version.rb
112
- - lib/lol_dba/writer.rb
113
181
  - lib/tasks/lol_dba.rake
114
182
  - lol_dba.gemspec
115
183
  - spec/associations_index_spec.rb
116
- - spec/common_function_spec.rb
117
184
  - spec/fixtures/app/models/address.rb
118
185
  - spec/fixtures/app/models/billable_week.rb
119
186
  - spec/fixtures/app/models/company.rb
@@ -134,7 +201,9 @@ files:
134
201
  - spec/fixtures/app/models/worker_user.rb
135
202
  - spec/fixtures/app/models/wrong.rb
136
203
  - spec/fixtures/schema.rb
204
+ - spec/migration_formatter_spec.rb
137
205
  - spec/spec_helper.rb
206
+ - spec/sql_generator_spec.rb
138
207
  homepage: https://github.com/plentz/lol_dba
139
208
  licenses:
140
209
  - MIT
@@ -155,15 +224,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
155
224
  - !ruby/object:Gem::Version
156
225
  version: '0'
157
226
  requirements: []
158
- rubyforge_project:
159
- rubygems_version: 2.7.6
227
+ rubygems_version: 3.0.2
160
228
  signing_key:
161
229
  specification_version: 4
162
230
  summary: A small package of rake tasks to track down missing database indexes and
163
231
  generate sql migration scripts
164
232
  test_files:
165
233
  - spec/associations_index_spec.rb
166
- - spec/common_function_spec.rb
167
234
  - spec/fixtures/app/models/address.rb
168
235
  - spec/fixtures/app/models/billable_week.rb
169
236
  - spec/fixtures/app/models/company.rb
@@ -184,4 +251,6 @@ test_files:
184
251
  - spec/fixtures/app/models/worker_user.rb
185
252
  - spec/fixtures/app/models/wrong.rb
186
253
  - spec/fixtures/schema.rb
254
+ - spec/migration_formatter_spec.rb
187
255
  - spec/spec_helper.rb
256
+ - spec/sql_generator_spec.rb
@@ -1,40 +0,0 @@
1
- module LolDba
2
- class Migration
3
- attr_accessor :full_name
4
-
5
- def initialize(migration_file)
6
- self.full_name = File.basename(migration_file, ".rb")
7
- require Rails.root.join(migration_file)
8
- end
9
-
10
- def number
11
- /^(\d+)_(.*)$/.match(full_name)[1]
12
- end
13
-
14
- def name
15
- /^(\d+)_(.*)$/.match(full_name)[2]
16
- end
17
-
18
- def to_s
19
- full_name
20
- end
21
-
22
- def migration_class
23
- name.camelize.split(".")[0].constantize
24
- end
25
-
26
- def up
27
- migration_class.migrate(:up)
28
- connection.execute("INSERT INTO schema_migrations (version) VALUES (#{number})")
29
- end
30
-
31
- def down
32
- migration_class.migrate(:down)
33
- connection.execute("DELETE FROM schema_migrations WHERE version = #{number}")
34
- end
35
-
36
- def connection
37
- ActiveRecord::Base.connection
38
- end
39
- end
40
- end
@@ -1,96 +0,0 @@
1
- module LolDba
2
- class SqlGenerator
3
- class << self
4
-
5
- def connection
6
- ActiveRecord::Base.connection
7
- end
8
-
9
- def methods_to_modify
10
- [:execute, :do_execute, :rename_column, :change_column, :column_for, :tables, :indexes, :select_all] & connection.methods
11
- end
12
-
13
- def redefine_execute_methods
14
- save_original_methods
15
- connection.class.send(:define_method, :execute) { |*args|
16
- if args.first =~ /SELECT "schema_migrations"."version"/ || args.first =~ /^SHOW/
17
- self.orig_execute(*args)
18
- else
19
- Writer.write(to_sql(args.first, args.last))
20
- end
21
- }
22
- connection.class.send(:define_method, :do_execute) { |*args|
23
- if args.first =~ /SELECT "schema_migrations"."version"/ || args.first =~ /^SHOW/
24
- self.orig_do_execute(*args)
25
- else
26
- Writer.write(to_sql(args.first, args.last))
27
- end
28
- }
29
- connection.class.send(:define_method, :column_for) { |*args| args.last }
30
- connection.class.send(:define_method, :change_column) { |*args| [] }
31
- connection.class.send(:define_method, :rename_column) { |*args| [] }
32
- connection.class.send(:define_method, :tables) { |*args| [] }
33
- connection.class.send(:define_method, :select_all) { |*args| [] }
34
- connection.class.send(:define_method, :indexes) { |*args| [] }
35
- connection.class.send(:define_method, :index_name_exists?) { |*args| args[2] } #returns always the default(args[2])
36
- end
37
-
38
- def save_original_methods
39
- methods_to_modify.each do |method_name|
40
- connection.class.send(:alias_method, "orig_#{method_name}".to_sym, method_name)
41
- end
42
- end
43
-
44
- def reset_methods
45
- methods_to_modify.each do |method_name|
46
- connection.class.send(:alias_method, method_name, "orig_#{method_name}".to_sym) rescue nil
47
- end
48
- end
49
-
50
- def generate_instead_of_executing(&block)
51
- LolDba::Writer.reset
52
- redefine_execute_methods
53
- yield
54
- reset_methods
55
- end
56
-
57
- def migrations(which)
58
- migrator = nil
59
- if ::ActiveRecord::VERSION::MAJOR == 4
60
- migrator = ActiveRecord::Migrator.new(:up, ActiveRecord::Migrator.migrations(ActiveRecord::Migrator.migrations_path))
61
- else
62
- migrator = ActiveRecord::Migrator.new(:up, ActiveRecord::Migrator.migrations_path)
63
- end
64
- if which == "all"
65
- migrator.migrations.collect { |m| m.filename }
66
- elsif which == "pending"
67
- pending = migrator.pending_migrations
68
- if pending.empty?
69
- puts "No pending migrations."
70
- exit
71
- end
72
- migrator.pending_migrations.collect { |m| m.filename }
73
- else
74
- if migration = migrator.migrations.find {|m| m.version == which.to_i}
75
- [migration.filename]
76
- else
77
- puts "There are no migrations for version #{which}."
78
- exit
79
- end
80
- end
81
- end
82
-
83
- def generate(which)
84
- generate_instead_of_executing { migrations(which).each { |file| up_and_down(file) } }
85
- end
86
-
87
- def up_and_down(file)
88
- migration = LolDba::Migration.new(file)
89
- LolDba::Writer.file_name = "#{migration}.sql"
90
- migration.up
91
- #MigrationSqlGenerator::Writer.file_name = "#{migration}_down.sql"
92
- #migration.down
93
- end
94
- end
95
- end
96
- end
@@ -1,25 +0,0 @@
1
- module LolDba
2
- class Writer
3
- class << self
4
- attr_accessor :file_name
5
-
6
- def reset
7
- FileUtils.rm_rf output_dir
8
- Dir.mkdir output_dir
9
- end
10
-
11
- def output_dir
12
- File.join(Rails.root, "db", "migrate_sql")
13
- end
14
-
15
- def path
16
- File.join(output_dir, file_name)
17
- end
18
-
19
- def write(string)
20
- return if file_name.nil?
21
- File.open(path, 'a') { |file| file << string; file << ";\n" }
22
- end
23
- end
24
- end
25
- end
@@ -1,96 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe "Function form_migration_content:" do
4
-
5
- before do
6
- @add = ["add_index :report, :_id_test_plan"]
7
- end
8
-
9
- it "print migration skeleton with set name" do
10
- migration = LolDba.form_migration_content("TestMigration", @add)
11
- expect(migration).to match(/class TestMigration/i)
12
- end
13
-
14
- it "print migration with add_keys params" do
15
- migration = LolDba.form_migration_content("TestMigration", @add)
16
- expect(migration).to match(/add_index :report, :_id_test_plan/i)
17
- end
18
- end
19
-
20
- RSpec.describe "Function form_data_for_migration:" do
21
-
22
- it "return data for migrations for non-indexed single key in table" do
23
- relationship_indexes = {:users => [:user_id]}
24
- allow(LolDba).to receive(:key_exists?).with(:users, :user_id).and_return(false)
25
-
26
- add_indexes = LolDba.form_data_for_migration(relationship_indexes)
27
-
28
- expect(add_indexes.first).to eq("add_index :users, :user_id")
29
- end
30
-
31
- it "return data for migrations for non-indexed composite key in table" do
32
- relationship_indexes = {:friends => [[:user_id, :friend_id]]}
33
- allow(LolDba).to receive(:key_exists?).with(:friends, [:user_id, :friend_id]).and_return(false)
34
-
35
- add_indexes = LolDba.form_data_for_migration(relationship_indexes)
36
-
37
- expect(add_indexes.first).to eq("add_index :friends, [:user_id, :friend_id]")
38
- end
39
-
40
- it "ignore empty or nil keys for table" do
41
- relationship_indexes = {:table => [""], :table2 => [nil]}
42
- add_indexes = LolDba.form_data_for_migration(relationship_indexes)
43
-
44
- expect(add_indexes).to be_empty
45
- end
46
-
47
- end
48
-
49
- RSpec.describe "Function key_exists?:" do
50
-
51
- it "return true if key is already indexed" do
52
- expect(LolDba.key_exists?("companies", "country_id")).to be_truthy
53
- end
54
-
55
- it "return false if key is not indexed yet" do
56
- expect(LolDba.key_exists?("addresses", "country_id")).to be_falsey
57
- end
58
-
59
- it "return true if key is primary key(default)" do
60
- expect(LolDba.key_exists?("addresses", "id")).to be_truthy
61
- end
62
-
63
- it "return true if key is custom primary key" do
64
- expect(LolDba.key_exists?("gifts", "custom_primary_key")).to be_truthy
65
- end
66
-
67
- end
68
-
69
- RSpec.describe "Function puts_migration_content:" do
70
-
71
- before do
72
- @relationship_indexes, warning_messages = LolDba.check_for_indexes
73
- end
74
-
75
- it "print migration code" do
76
- expect($stdout).to receive(:puts).with("")
77
- expect($stdout).to receive(:puts).with(/TIP/)
78
- expect($stdout).to receive(:puts).with(/TestMigration/i)
79
- LolDba.puts_migration_content("TestMigration", @relationship_indexes, "")
80
- end
81
-
82
- it "print warning messages if they exist" do
83
- warning = "warning text here"
84
- expect($stdout).to receive(:puts).at_least(:once).with(warning)
85
- expect($stdout).to receive(:puts)
86
-
87
- LolDba.puts_migration_content("TestMigration", {}, warning)
88
- end
89
-
90
- it "print nothing if no indexes and warning messages exist" do
91
- expect($stdout).to receive(:puts).with("")
92
- expect($stdout).to receive(:puts).with("Yey, no missing indexes found!")
93
- LolDba.puts_migration_content("TestMigration",{}, "")
94
- end
95
-
96
- end