database_recorder 0.2.1 → 0.2.2

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: d0b2575a34a9e68ee59f68884a492a33c071a05b9d9999caf3baeca5c070c11d
4
- data.tar.gz: 29b78920c82393f3e9a23d334b7675d29dc96f3b77f13ff1bf33384cc3206a0d
3
+ metadata.gz: 48d1f4f88105fa9ac49075181f90208ca051bec2273daaeaf77cca9ad2c1ed77
4
+ data.tar.gz: 9ef096a48a9a0dafd7bbab635a20805d4b5ec0f06710d9b596554a1f03016c8e
5
5
  SHA512:
6
- metadata.gz: 0c7c56cc0c95e24b410ec5d63ac64b1944a20d7084d63ae64b0d80ea265b815435d2c3ce14eec3fb9d2f69261c567ddd59eaba24b4ca0c28f3993272fbb2e33b
7
- data.tar.gz: 78db7a6377ba564f5e039bd74f32ffe4c4f6d6c6ff5719340818738a7a4ea40caf4f2164da1cf72e1ebb3800863e1921d3c2bbf32952ff00dc1154487367fe4a
6
+ metadata.gz: 177da34267bb1fb58fb1b99e68d68430f8d36822facffb9fcd42e2e024176bbfc7be4d57ef807ce834bf1522151255c1614e5a9bb73833d86b70eb502aac0a30
7
+ data.tar.gz: 43d907dd4dc7902280bb379a4ae20dadca9c89712e62a188be7609795efd0c87db968c0b938e303e1f3e38bf44e23d34ed91be567169e118b42b3c00bd7e212e
data/README.md CHANGED
@@ -6,7 +6,7 @@
6
6
  [![Specs PostgreSQL](https://github.com/blocknotes/database_recorder/actions/workflows/specs_postgres.yml/badge.svg)](https://github.com/blocknotes/database_recorder/actions/workflows/specs_postgres.yml)
7
7
 
8
8
  Record database queries for testing and development purposes.
9
- Supports only RSpec at the moment. Store queries information on files or Redis.
9
+ Store queries information on files or Redis.
10
10
 
11
11
  Main features:
12
12
  - store the history of the queries of a test when it run (for monitoring);
@@ -15,10 +15,16 @@ Main features:
15
15
 
16
16
  Sample output: [test.yml](extra/sample.yml)
17
17
 
18
+ Creating an environment variable to enable it with RSpec:
19
+
20
+ ![image1](extra/image1.png)
21
+
18
22
  ## Install
19
23
 
24
+ ### With RSpec
25
+
20
26
  - Add to your Gemfile: `gem 'database_recorder', require: false` (:development, :test groups recommended)
21
- - Using RSpec, add in **rails_helper.rb**:
27
+ - Add in **rails_helper.rb**:
22
28
 
23
29
  ```rb
24
30
  require 'database_recorder'
@@ -49,9 +55,20 @@ RSpec.configure do |config|
49
55
  end
50
56
  ```
51
57
 
52
- Using an environment variable to enable it:
58
+ ### With plain Ruby
53
59
 
54
- ![image1](extra/image1.png)
60
+ ```rb
61
+ DatabaseRecorder::Config.db_driver = :pg
62
+ DatabaseRecorder::Core.setup
63
+ DatabaseRecorder::Recording.new(options: { name: 'pg_file' }).tap do |recording|
64
+ pp recording.start do
65
+ PG.connect(DB_CONFIG).exec("INSERT INTO tags(name, created_at, updated_at) VALUES('tag1', NOW(), NOW())")
66
+ PG.connect(DB_CONFIG).exec("SELECT * FROM tags")
67
+ end
68
+ end
69
+ ```
70
+
71
+ Please check more [examples](examples).
55
72
 
56
73
  ## Config
57
74
 
@@ -61,7 +78,10 @@ Add to your _spec_helper.rb_:
61
78
  # Database driver to use: :active_record | :mysql2 | :pg
62
79
  DatabaseRecorder::Config.db_driver = :pg
63
80
 
64
- # To print the queries while executing the specs: false | true | :color
81
+ # Log queries format (default: '[DB] %sql [%name]')
82
+ DatabaseRecorder::Config.log_format = '>>> %name -- %sql'
83
+
84
+ # To print/log the queries while executing the specs: false | true | :color
65
85
  DatabaseRecorder::Config.print_queries = true
66
86
 
67
87
  # Replay the recordings intercepting the queries
@@ -8,6 +8,7 @@ module DatabaseRecorder
8
8
  include Singleton
9
9
 
10
10
  DEFAULT_DB_DRIVER = :active_record
11
+ DEFAULT_LOG_FORMAT = '[DB] %sql [%name]'
11
12
  DEFAULT_STORAGE = DatabaseRecorder::Storage::File
12
13
 
13
14
  DB_DRIVER_VALUES = %i[active_record mysql2 pg].freeze
@@ -17,16 +18,17 @@ module DatabaseRecorder
17
18
  redis: DatabaseRecorder::Storage::Redis
18
19
  }.freeze
19
20
 
20
- attr_accessor :db_driver, :print_queries, :replay_recordings, :storage, :storage_options
21
+ attr_accessor :db_driver, :log_format, :print_queries, :replay_recordings, :storage, :storage_options
21
22
 
22
23
  class << self
23
24
  extend Forwardable
24
25
 
25
- def_delegators :instance, :db_driver, :print_queries, :replay_recordings, :replay_recordings=, :storage,
26
- :storage_options, :storage_options=
26
+ def_delegators :instance, :db_driver, :log_format, :log_format=, :print_queries, :replay_recordings,
27
+ :replay_recordings=, :storage, :storage_options, :storage_options=
27
28
 
28
29
  def load_defaults
29
30
  instance.db_driver = DEFAULT_DB_DRIVER
31
+ instance.log_format = DEFAULT_LOG_FORMAT
30
32
  instance.print_queries = false
31
33
  instance.replay_recordings = false
32
34
  instance.storage = DEFAULT_STORAGE
@@ -7,8 +7,11 @@ module DatabaseRecorder
7
7
  def log_query(sql, source = nil)
8
8
  log =
9
9
  case DatabaseRecorder::Config.print_queries
10
- when true then "[DB] #{sql} [#{source}]"
11
- when :color then "[DB] #{CodeRay.scan(sql, :sql).term} [#{source}]"
10
+ when true
11
+ DatabaseRecorder::Config.log_format.sub('%name', source.to_s).sub('%sql', sql)
12
+ when :color
13
+ code_ray_sql = CodeRay.scan(sql, :sql).term
14
+ DatabaseRecorder::Config.log_format.sub('%name', source.to_s).sub('%sql', code_ray_sql || '')
12
15
  end
13
16
 
14
17
  puts log if log
@@ -13,8 +13,8 @@ module DatabaseRecorder
13
13
  config.around(:each, :dbr) do |example|
14
14
  ref = (example.metadata[:scoped_id] || '').split(':')[-1]
15
15
  options = {}
16
+ options[:name] = "#{example.full_description}__#{ref}"
16
17
  options.merge!(example.metadata[:dbr]) if example.metadata[:dbr].is_a?(Hash)
17
- options.merge!(example: example, name: "#{example.full_description}__#{ref}")
18
18
  Recording.new(options: options).tap do |recording|
19
19
  recording.metadata = { example: example.id, started_at: Time.now }
20
20
  result = recording.start { example.run }
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'fileutils'
4
+ require 'yaml'
5
+
3
6
  module DatabaseRecorder
4
7
  module Storage
5
8
  class File < Base
@@ -21,7 +24,7 @@ module DatabaseRecorder
21
24
  data[:metadata] = @recording.metadata unless @recording.metadata.empty?
22
25
  data[:queries] = @recording.queries if @recording.queries.any?
23
26
  data[:entities] = @recording.entities if @recording.entities.any?
24
- serialized_data = Core.string_keys_recursive(data).to_yaml
27
+ serialized_data = ::YAML.dump(Core.string_keys_recursive(data))
25
28
  ::File.write(storage_path, serialized_data)
26
29
  true
27
30
  end
@@ -30,7 +33,7 @@ module DatabaseRecorder
30
33
  @storage_path ||= begin
31
34
  name = normalize_name(@name)
32
35
  path = @options[:recordings_path] || 'spec/dbr'
33
- FileUtils.mkdir_p(path)
36
+ ::FileUtils.mkdir_p(path)
34
37
  "#{path}/#{name}.yml"
35
38
  end
36
39
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'json'
4
+
3
5
  module DatabaseRecorder
4
6
  module Storage
5
7
  class Redis < Base
@@ -2,6 +2,6 @@
2
2
 
3
3
  # :nocov:
4
4
  module DatabaseRecorder
5
- VERSION = '0.2.1'
5
+ VERSION = '0.2.2'
6
6
  end
7
7
  # :nocov:
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: database_recorder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mattia Roccoberton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-16 00:00:00.000000000 Z
11
+ date: 2022-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: coderay