migration_queries 0.4.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ff997e1ebe254c3fd23a538365fa0cafbcfc010c3e5199054202ed6bd1b2851
4
- data.tar.gz: 70bef9fc0517130bc9158cc1f48c32b38c098ab9f93248de3957da1ea52ae87a
3
+ metadata.gz: 62f8b4f72f64212491d3cb2e3062ca7fc2e32a79c951e3c21fc5b23e37286f01
4
+ data.tar.gz: b4c174e6969d8969c169207cb5f1786e8aa66d71b3c3692710b187102efc7986
5
5
  SHA512:
6
- metadata.gz: 55526a659e9ea130374069cc062a9895ffe8734845f91490514a64ad24ae11b3b574c7ccdc2011364585a2d63ec4037596a3d31c33e5e6724036714695b59f6a
7
- data.tar.gz: f80f52f70194dd955c697d931ac42cc6c08b85cdd67e2ffb717ec5c1dc1b307e1d5b40f383117774c769a2630aec96777ac6cb75076fdf0db16dec7a8ee244ab
6
+ metadata.gz: 8205a98bbd53287dd1d984a8a683e6419a3c73bcd938c10772239d11fd36ed8710c778b627ccde5d488335794a2249d4922eceb272c8b624cfabef99229bb85d
7
+ data.tar.gz: 38e06a529ce0ca44b7a362f4f500b8d4d1752f99f97e0f3a48b3dbe7d35be489a30f5b843ea5a8b0a33395ae6a38eb735bae97a4eea465bc7d8658641a018380
data/README.md CHANGED
@@ -14,12 +14,6 @@ end
14
14
 
15
15
  Then, run `bundle install` to install the gem.
16
16
 
17
- And then execute:
18
-
19
- ```bash
20
- bundle exec rake migration_queries:install
21
- ```
22
-
23
17
  ## Usage
24
18
 
25
19
  The gem is going to automatically track migration queries executed by ActiveRecord. You can view the logged queries in your Rails console or in the log files.
@@ -50,7 +44,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
50
44
 
51
45
  ## Contributing
52
46
 
53
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/migration_queries. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/migration_queries/blob/master/CODE_OF_CONDUCT.md).
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/szymonlipka/migration_queries. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/migration_queries/blob/master/CODE_OF_CONDUCT.md).
54
48
 
55
49
  ## License
56
50
 
data/Rakefile CHANGED
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- load("lib/tasks/setup_migration_queries.rake")
4
-
5
3
  require "bundler/gem_tasks"
6
4
  require "rspec/core/rake_task"
7
5
 
@@ -7,19 +7,38 @@ module MigrationQueries
7
7
  # with logic to capture SQL queries executed during migrations.
8
8
  module Executer
9
9
  def execute(sql, *_args)
10
- stack_trace = caller.detect { |path| path.include?(Rails.root.to_s) && path.include?("db/migrate") }
11
- file_path = stack_trace.match(/(.+):\d+:in/)[1] if stack_trace
12
- if defined?(file_path) && file_path
13
- data_object = MigrationQueries.gatherer.queries_data_objects.detect do |data_object|
14
- data_object.file_path == file_path
15
- end || MigrationQueries::Data.new(file_path: file_path)
16
- data_object.sql_queries << sql
17
-
18
- unless MigrationQueries.gatherer.queries_data_objects.include?(data_object)
19
- MigrationQueries.gatherer.queries_data_objects << data_object
20
- end
21
- end
10
+ gather_queries(sql, caller)
22
11
  super
23
12
  end
13
+
14
+ private
15
+
16
+ def gather_queries(sql, caller)
17
+ stack_trace = detect_stack_trace(caller)
18
+ file_path = extract_file_path(stack_trace) if stack_trace
19
+ gatherer = load_gatherer
20
+ return unless defined?(file_path) && file_path
21
+
22
+ data_object = gatherer.queries_data_objects.detect do |data_object|
23
+ data_object.file_path == file_path
24
+ end || MigrationQueries::Data.new(file_path: file_path)
25
+ data_object.sql_queries << sql
26
+
27
+ return if gatherer.queries_data_objects.include?(data_object)
28
+
29
+ MigrationQueries.gatherer.queries_data_objects << data_object
30
+ end
31
+
32
+ def load_gatherer
33
+ MigrationQueries.gatherer
34
+ end
35
+
36
+ def detect_stack_trace(caller)
37
+ caller.detect { |path| path.include?(Rails.root.to_s) && path.include?("db/migrate") }
38
+ end
39
+
40
+ def extract_file_path(stack_trace)
41
+ stack_trace.match(/(.+):\d+:in/)[1]
42
+ end
24
43
  end
25
44
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MigrationQueries
4
- VERSION = "0.4.0"
4
+ VERSION = "1.1.0"
5
5
  end
@@ -6,6 +6,9 @@ require_relative "migration_queries/executer"
6
6
  require_relative "migration_queries/migrater"
7
7
  require "active_record"
8
8
 
9
+ ActiveRecord::ConnectionAdapters::AbstractAdapter.include(MigrationQueries::Executer)
10
+ ActiveRecord::Migration::Current.include(MigrationQueries::Migrater)
11
+
9
12
  # MigrationQueries is a module that provides functionality to gather, execute, and migrate SQL queries
10
13
  # during ActiveRecord migrations. It includes the Gatherer, Executer, and Migrater modules to handle
11
14
  # different aspects of SQL query management.
@@ -15,9 +18,4 @@ module MigrationQueries
15
18
  def self.gatherer
16
19
  @gatherer ||= MigrationQueries::Gatherer.new
17
20
  end
18
-
19
- def self.init!
20
- ActiveRecord::ConnectionAdapters::AbstractAdapter.include(MigrationQueries::Executer)
21
- ActiveRecord::Migration::Current.include(MigrationQueries::Migrater)
22
- end
23
21
  end
@@ -1,5 +1,15 @@
1
1
  module MigrationQueries
2
2
  module Executer
3
3
  def execute: -> untyped
4
+
5
+ private
6
+
7
+ def detect_stack_trace: -> String
8
+
9
+ def extract_file_path: -> String
10
+
11
+ def gather_queries: -> untyped
12
+
13
+ def load_gatherer: -> MigrationQueries::Gatherer
4
14
  end
5
15
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: migration_queries
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Szymon Lipka
@@ -46,7 +46,6 @@ files:
46
46
  - lib/migration_queries/gatherer.rb
47
47
  - lib/migration_queries/migrater.rb
48
48
  - lib/migration_queries/version.rb
49
- - lib/tasks/setup_migration_queries.rake
50
49
  - sig/migration_queries.rbs
51
50
  - sig/migration_queries/data.rbs
52
51
  - sig/migration_queries/executer.rbs
@@ -1,24 +0,0 @@
1
- # lib/tasks/setup_migration_queries.rake
2
-
3
- namespace :migration_queries do
4
- desc "Create migration_queries.rb initializer"
5
- task :setup do
6
- initializer_path = Rails.root.join("config", "initializers", "migration_queries.rb")
7
- content = <<~RUBY
8
- # frozen_string_literal: true
9
-
10
- if Rails.env.development?
11
- require 'migration_queries'
12
-
13
- MigrationQueries.init!
14
- end
15
- RUBY
16
-
17
- if File.exist?(initializer_path)
18
- puts "Initializer file already exists at #{initializer_path}"
19
- else
20
- File.write(initializer_path, content)
21
- puts "Created initializer file at #{initializer_path}"
22
- end
23
- end
24
- end