bemi-rails 0.1.0 → 0.2.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: 7c0601f1bb209e0d1d0e0b08f7e9f6ed1b32575b5bb6b3dfd1c6262a249afaec
4
- data.tar.gz: ea404c16b60f45fedfef06dfbd017aaba63649d99a4fc0e001bf015ede6c42c5
3
+ metadata.gz: fa3333de9e0a741dd9ea055875b359e3cc5762e0cb8cda2ca7993fb013e761ad
4
+ data.tar.gz: 24080ca04dee07312adb7e41bf08286aae441ec404fc8bc1d06ed64693041c27
5
5
  SHA512:
6
- metadata.gz: 78e7ab750b1adcab1bb9675fdb5425ed9404f01fb920b1ed5c9712ce94c6f1a8972add88d94e0d6b3d977ff9835c14eff9bec78c346e80c73e934f959825e1b5
7
- data.tar.gz: 1bc3a6fb17ad990a96eb92f05f2d85f813c3580f343715a64d45df25da2aa5d0451911df681210fe9c100acc9cc43ac6b52d39e6e302009cc4cb27334b32a01d
6
+ metadata.gz: d927d26dc83bdc1d1d0f44353506729c885b2de498f077fda655556f76ca14fb6dde5fb01f0c72e9f4a372e7970510bc9f16b4f60066539e0fe2bc3325259c77
7
+ data.tar.gz: ba9d3e21f4d365e4dc419a6444fe3fddfe8bd98e753eb08f24064be20d3883e864d514ab2cc89b050657410a835c4fdfebed82ac0cdaa44f7f154d3081eb7d35
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ #### [v0.2.0](https://github.com/BemiHQ/bemi-rails/compare/v0.1.0...v0.2.0) - 2024-04-16
2
+
3
+ - Fix compatibility with ActiveRecord versions prior to 7.1
4
+ - Validate context size to keep it under 1MB
5
+
1
6
  #### v0.1.0 - 2024-03-08
2
7
 
3
8
  - 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,43 @@
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
+ result = context.to_json
41
+ result if result.size <= MAX_CONTEXT_SIZE
42
+ end
22
43
  end
23
44
  end
24
45
 
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.0"
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.0
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