forest_admin_datasource_active_record 1.0.0.pre.beta.83 → 1.0.0.pre.beta.85

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b525730d06962b290addae1d5f289763cc3caa8924f48bca63a6204ea52f11ec
4
- data.tar.gz: b4894584a3618c8a9cbca7f2d828a2ee399a5ca152def20b791aa05099896c1f
3
+ metadata.gz: 45d904af3bd4dc1f7ad3173832d0114e5514f1ca0c883e188cf90a9738a63434
4
+ data.tar.gz: 783b4531f3319bd262a5c66b7036a370791e87390580c1593e94ac157334ced3
5
5
  SHA512:
6
- metadata.gz: 07e5364b10e6158fc7a6dd115821f5f0b5981c12803bbe98b882f8a82614356f12bb7254b58b5a6876b4b3711a99f64e8759cb9afd0b18767a115fd5dbe210ad
7
- data.tar.gz: 489b1ee0ec0ea6183f16454ae0ec57a2e2ee6803e233d207317b926f160438c835ef7530ea69cdb67771cb4805bee325f0b89deb6d0ffc0889a723de6ac5bac0
6
+ metadata.gz: 42d12bbca254d8da1645dd6c24e6609b7eed0c480bc25c832654378598386913ce851a6a507cdd9adbc9c991a86479b30235e9943c68c0d1789d6a43fc404c3c
7
+ data.tar.gz: bd48b0696dd2d4093b89c43450002c9e83546df234d597a492b343332b8ebe1501d8f37ea17e238fe9589017ca42f76eeaaa8f26fd545827c5f3d38d2f42c096
@@ -4,15 +4,56 @@ module ForestAdminDatasourceActiveRecord
4
4
  class Datasource < ForestAdminDatasourceToolkit::Datasource
5
5
  attr_reader :models
6
6
 
7
- def initialize(db_config = {}, support_polymorphic_relations: false)
7
+ def initialize(
8
+ db_config = {},
9
+ support_polymorphic_relations: false,
10
+ live_query_connections: nil
11
+ )
8
12
  super()
9
13
  @models = []
10
14
  @support_polymorphic_relations = support_polymorphic_relations
11
15
  @habtm_models = {}
16
+ @connection_drivers = {}
17
+
18
+ @live_query_connections = if live_query_connections.is_a?(String)
19
+ { live_query_connections => 'primary' }
20
+ elsif live_query_connections.is_a?(Hash)
21
+ live_query_connections
22
+ else
23
+ {}
24
+ end
25
+
12
26
  init_orm(db_config)
13
27
  generate
14
28
  end
15
29
 
30
+ def execute_native_query(connection_name, query, binds)
31
+ unless @live_query_connections[connection_name]
32
+ raise ForestAdminAgent::Http::Exceptions::NotFoundError,
33
+ "Native query connection '#{connection_name}' is unknown."
34
+ end
35
+
36
+ begin
37
+ connection = @live_query_connections[connection_name]
38
+
39
+ result = ActiveRecord::Base.connects_to(database: { reading: connection.to_sym }).first.connection
40
+ .exec_query(query, "SQL Native Query on '#{connection_name}'", binds)
41
+
42
+ ForestAdminDatasourceToolkit::Utils::HashHelper.convert_keys(result.to_a)
43
+ rescue StandardError => e
44
+ raise ForestAdminDatasourceToolkit::Exceptions::ForestException,
45
+ "Error when executing SQL query: '#{e.full_message}'"
46
+ end
47
+ end
48
+
49
+ def build_binding_symbol(connection_name, binds)
50
+ if @connection_drivers[@live_query_connections[connection_name]] == 'postgresql'
51
+ "$#{binds.size + 1}"
52
+ else
53
+ '?'
54
+ end
55
+ end
56
+
16
57
  private
17
58
 
18
59
  def generate
@@ -43,6 +84,17 @@ module ForestAdminDatasourceActiveRecord
43
84
 
44
85
  def init_orm(db_config)
45
86
  ActiveRecord::Base.establish_connection(db_config)
87
+ current_config = ActiveRecord::Base.connection_db_config.env_name
88
+ configurations = ActiveRecord::Base.configurations
89
+ .configurations
90
+ .group_by(&:env_name)
91
+ .transform_values do |configs|
92
+ configs.to_h do |config|
93
+ [config.name, config.adapter]
94
+ end
95
+ end.to_h
96
+
97
+ @connection_drivers = configurations[current_config]
46
98
  end
47
99
 
48
100
  def build_habtm(model)
@@ -1,3 +1,3 @@
1
1
  module ForestAdminDatasourceActiveRecord
2
- VERSION = "1.0.0-beta.83"
2
+ VERSION = "1.0.0-beta.85"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forest_admin_datasource_active_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.pre.beta.83
4
+ version: 1.0.0.pre.beta.85
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthieu
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2024-12-13 00:00:00.000000000 Z
12
+ date: 2024-12-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord