database_leak_finder 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 654d5ed283dd00dbca47501ebfd87180d3b07a46
4
+ data.tar.gz: 1c9f861f039c88890d7e9dcdcabdce0eeb2902fb
5
+ SHA512:
6
+ metadata.gz: 3b14b5be68f8aed7af6ea3a86b86148d17d77ccd6372771f194ec50c3743222f6b40fc5b3a373b9149e057814a6e231f1b9d7791ab10e087eed0160ff5e73820
7
+ data.tar.gz: 39315f530d1388618689de20d68940ff233a1afec76bf005e48b550397e69d2a9d55bf7fae95c860c3133e4b876cac8302c402e903b2bec60a748dff1aed2160
@@ -0,0 +1,32 @@
1
+ # DatabaseLeakFinder [![Circle CI](https://circleci.com/gh/vitalinfo/database_leak_finder.svg?style=svg)](https://circleci.com/gh/vitalinfo/database_leak_finder)
2
+
3
+ Easy way to find problem in specs with uncleared DB data
4
+
5
+ ## Installation
6
+
7
+ Add the following to your Gemfile:
8
+ ```ruby
9
+ gem 'database_leak_finder'
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ Add this line to your `spec_helper.rb`
15
+ ```ruby
16
+ config.include DatabaseLeakFinder
17
+ ```
18
+
19
+ ### Ignoring Tables
20
+ You may have some tables that you don't want to report.
21
+
22
+ Add this line to your `spec_helper.rb` before `config.include DatabaseLeakFinder`
23
+ ```ruby
24
+ DatabaseLeakFinder.config(ignored_tables: [list of tables])
25
+ ```
26
+
27
+ ## Contributing
28
+ 1. Fork it
29
+ 2. Create your feature branch (git checkout -b my-new-feature)
30
+ 3. Commit your changes (git commit -am 'Add some feature')
31
+ 4. Push to the branch (git push origin my-new-feature)
32
+ 5. Create new Pull Request
@@ -0,0 +1,24 @@
1
+ require 'active_record'
2
+ require 'database_leak_finder/finder'
3
+ require 'database_leak_finder/handler'
4
+ require 'database_leak_finder/logger'
5
+
6
+ module DatabaseLeakFinder
7
+ class << self
8
+ def included(base)
9
+ base.after(:each) do |example|
10
+ DatabaseLeakFinder::Handler.log_example(example)
11
+ end
12
+
13
+ base.after(:all) do
14
+ DatabaseLeakFinder::Handler.find_leaks
15
+ end
16
+
17
+ DatabaseLeakFinder.config({}) unless DatabaseLeakFinder::Handler.options
18
+ end
19
+
20
+ def config(options)
21
+ DatabaseLeakFinder::Handler.config(options)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,33 @@
1
+ module DatabaseLeakFinder
2
+ class Finder
3
+ IGNORED_SYSTEM_TABLES = ['ar_internal_metadata', 'schema_migrations']
4
+
5
+ def initialize(options)
6
+ @ignored_tables = (options[:ignored_tables] || []) + IGNORED_SYSTEM_TABLES
7
+ end
8
+
9
+ def process
10
+ return [] unless ActiveRecord::Base.connected?
11
+ filtered_tables.each_with_object({}) do |table, result|
12
+ amount = count_for(table)
13
+ result[table] = amount unless amount.zero?
14
+ result
15
+ end
16
+ end
17
+
18
+ private
19
+ def count_for(table)
20
+ ActiveRecord::Base.connection.select_value("SELECT COUNT(*) FROM #{table}").to_i
21
+ rescue
22
+ raise StandardError, "An error occurred while try to count '#{table}'"
23
+ end
24
+
25
+ def filtered_tables
26
+ tables - @ignored_tables
27
+ end
28
+
29
+ def tables
30
+ ActiveRecord::Base.connection.data_sources
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,29 @@
1
+ module DatabaseLeakFinder
2
+ module Handler
3
+ class << self
4
+ attr_reader :options
5
+
6
+ def log_example(example)
7
+ @example = example
8
+ end
9
+
10
+ def find_leaks
11
+ leaks = finder.process
12
+ logger.log(@example, leaks) unless leaks.empty?
13
+ end
14
+
15
+ def config(options)
16
+ @options = options
17
+ end
18
+
19
+ private
20
+ def finder
21
+ @finder ||= DatabaseLeakFinder::Finder.new(@options)
22
+ end
23
+
24
+ def logger
25
+ @logger ||= DatabaseLeakFinder::Logger.new
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,23 @@
1
+ require 'highline'
2
+
3
+ module DatabaseLeakFinder
4
+ class Logger
5
+ def log(example, leaks)
6
+ puts <<-LOG.strip_heredoc
7
+ \n
8
+ #{HighLine.color('#### Leaking Spec', :red)}
9
+ The spec '#{HighLine.color(spec_path_for(example), :red, :underline)}' leaves the following rows in the database:
10
+ LOG
11
+ leaks.each(&method(:log_leak))
12
+ end
13
+
14
+ private
15
+ def spec_path_for(example)
16
+ example.metadata[:example_group][:file_path]
17
+ end
18
+
19
+ def log_leak(table, count)
20
+ puts " - #{HighLine.color(count.to_s, :red)} #{'row'.pluralize(count)} for the #{HighLine.color(table, :white, :underline)} table"
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module DatabaseLeakFinder
2
+ VERSION = '0.0.2'
3
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: database_leak_finder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Vital Ryabchinskiy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-16 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 3.0.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 3.0.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: highline
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '1.0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '1.0'
97
+ description: "'Easy way to find spec which doesn't clear database after execution"
98
+ email: vital.ryabchinskiy@gmail.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - README.md
104
+ - lib/database_leak_finder.rb
105
+ - lib/database_leak_finder/finder.rb
106
+ - lib/database_leak_finder/handler.rb
107
+ - lib/database_leak_finder/logger.rb
108
+ - lib/database_leak_finder/version.rb
109
+ homepage: https://github.com/vitalinfo/database_leak_finder
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.5.1
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Find database leaks
133
+ test_files: []