bemi-rails 0.1.0 → 0.2.1

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: 7c0601f1bb209e0d1d0e0b08f7e9f6ed1b32575b5bb6b3dfd1c6262a249afaec
4
- data.tar.gz: ea404c16b60f45fedfef06dfbd017aaba63649d99a4fc0e001bf015ede6c42c5
3
+ metadata.gz: f19aaf859eda061205bee3a1a49aa3985982a80ebb90b94fb44160431753f058
4
+ data.tar.gz: 947da8058a12c7171e312dc8a2069e6b303b16af670247c287830336def82dad
5
5
  SHA512:
6
- metadata.gz: 78e7ab750b1adcab1bb9675fdb5425ed9404f01fb920b1ed5c9712ce94c6f1a8972add88d94e0d6b3d977ff9835c14eff9bec78c346e80c73e934f959825e1b5
7
- data.tar.gz: 1bc3a6fb17ad990a96eb92f05f2d85f813c3580f343715a64d45df25da2aa5d0451911df681210fe9c100acc9cc43ac6b52d39e6e302009cc4cb27334b32a01d
6
+ metadata.gz: fe752060e2291a4e39cf126a5984a4453db821055e49a80d81e4efd4dfc0d96717d4a6ebdf18cc3800869409438e81bac1b291292497c0c8412d4eb5df9417a6
7
+ data.tar.gz: 238dfc3d2ed1ce9a0aa6fbdcbf0697009dcd1a29b6858aa01b7518751d3ac0da36e84637b6945c22ca63e6eaf0971aaa1e52981dbb8b8a479d0bf6b8b0800cd4
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ #### [v0.2.1](https://github.com/BemiHQ/bemi-rails/compare/v0.2.0...v0.2.1) - 2024-04-16
2
+
3
+ - Validate context type to make sure it's a hash or hash-like object
4
+
5
+ #### [v0.2.0](https://github.com/BemiHQ/bemi-rails/compare/v0.1.0...v0.2.0) - 2024-04-16
6
+
7
+ - Fix compatibility with ActiveRecord versions prior to 7.1
8
+ - Validate context size to keep it under 1MB
9
+
1
10
  #### v0.1.0 - 2024-03-08
2
11
 
3
12
  - Create the gem
data/README.md CHANGED
@@ -24,7 +24,7 @@
24
24
 
25
25
  # Bemi Rails
26
26
 
27
- [Bemi](https://bemi.io) plugs into [Ruby on Rails](https://github.com/rails/rails) with Active Record and PostgreSQL to track database changes automatically. It unlocks robust context-aware audit trails and time travel querying inside your application.
27
+ [Bemi](https://bemi.io) plugs into [Ruby on Rails](https://github.com/rails/rails) with Active Record and your existing PostgreSQL database to track data changes automatically. It unlocks robust context-aware audit trails and time travel querying inside your application.
28
28
 
29
29
  Designed with simplicity and non-invasiveness in mind, Bemi doesn't require any alterations to your existing database structure. It operates in the background, empowering you with data change tracking features.
30
30
 
data/lib/bemi/context.rb CHANGED
@@ -3,22 +3,46 @@
3
3
  require 'active_record'
4
4
 
5
5
  class Bemi
6
- def self.set_context(context)
7
- Thread.current[:bemi_context] = context
8
- end
6
+ READ_QUERY = ActiveRecord::ConnectionAdapters::AbstractAdapter.build_read_query_regexp(
7
+ :close, :declare, :fetch, :move, :set, :show
8
+ )
9
9
 
10
- def self.context
11
- Thread.current[:bemi_context]
12
- end
10
+ MAX_CONTEXT_SIZE = 1_000_000 # ~1MB
11
+
12
+ class << self
13
+ def set_context(ctx)
14
+ Thread.current[:bemi_context] = ctx
15
+ end
16
+
17
+ def context
18
+ Thread.current[:bemi_context]
19
+ end
13
20
 
14
- def self.append_context
15
- ->(sql, adapter) do
16
- if adapter.write_query?(sql)
17
- "#{sql} /*Bemi #{Bemi.context.to_json} Bemi*/"
18
- else
19
- sql
21
+ def append_context
22
+ Proc.new do |sql, adapter = nil| # Adapter is automatically passed only with Rails v7.1+
23
+ if (adapter ? adapter.write_query?(sql) : write_query?(sql)) && ctx = serialized_context
24
+ "#{sql} /*Bemi #{ctx} Bemi*/"
25
+ else
26
+ sql
27
+ end
20
28
  end
21
29
  end
30
+
31
+ private
32
+
33
+ def write_query?(sql)
34
+ !READ_QUERY.match?(sql)
35
+ rescue ArgumentError
36
+ !READ_QUERY.match?(sql.b)
37
+ end
38
+
39
+ def serialized_context
40
+ return if context.nil?
41
+ return if !context.is_a?(Hash) && !context.respond_to?(:has_key?)
42
+
43
+ result = context.to_json
44
+ result if result.size <= MAX_CONTEXT_SIZE
45
+ end
22
46
  end
23
47
  end
24
48
 
data/lib/bemi/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Bemi
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bemi-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - exAspArk
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-03-08 00:00:00.000000000 Z
11
+ date: 2024-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties