forest_admin_datasource_active_record 1.0.0.pre.beta.82 → 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: 9d83089945909a70c3bc9887643c635bc6ffaba6bc9f595a4dea61e9732dbe6f
4
- data.tar.gz: 5bde89f5a4a1c6cbde94c5126150f7e2827b0dec12000d9da8e7c719744bcd4c
3
+ metadata.gz: 45d904af3bd4dc1f7ad3173832d0114e5514f1ca0c883e188cf90a9738a63434
4
+ data.tar.gz: 783b4531f3319bd262a5c66b7036a370791e87390580c1593e94ac157334ced3
5
5
  SHA512:
6
- metadata.gz: 51a055f444cf27aed6ed57083c8c2e449ebb149e32e5bccea8fc9dc5b7cda76a5b82fb3bf461c47ddb129569ecd42dd695f8d75611a9a33a58cf052b35c03945
7
- data.tar.gz: 300c2fbabe11109115752dbd7931a2148d00668a4f5675dbf9e5fdd3061f2004bcb74620175a9012768bfe8c15b4126e9ee5f32a844bbd693b78db2b20797821
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.82"
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.82
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-04 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