dbviewer 0.5.6 → 0.5.8

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: 98bab0acf6c5aef237e7ef161661cfdfa11c9067973efe1f189e01a7fad51175
4
- data.tar.gz: 0b35c2802191d5f23f8a5baf2740cd006e7b9dbf109f35c9cec77605b004bcf8
3
+ metadata.gz: d46526c59b9c355c9e717e76ebe81299bbdb051be58fb1b789fbe2fa1c50c0e4
4
+ data.tar.gz: 713277fda9b00b0fe4a48b2e8324632f2c7920b25cbaad369afe2d159c2bcf29
5
5
  SHA512:
6
- metadata.gz: a220184a4186cf107ffeb0a12a93b252370cd704be1ee54f8c3040c9818ec82fa7cd866dc170ede1180cf2ad640efb9605669d5f918f7c9b1f94d170b1e843e8
7
- data.tar.gz: 7a30db71097c9066c8a0069ec8c4223d705e88e0e6c83266b87ab34112987c088cd5749abfae71f519efa47daeadbb25a4baafe8c024cf0de07631ac3486fce1
6
+ metadata.gz: 77444f6cf1b7d847d114bdef9f965d9c90e4e216666db1fccb7de5652f92950cae7793bab07da982e473517f4c350d0dc186e094291138258c9a589de983bf5c
7
+ data.tar.gz: fc6801c693d632bf8a4bfe26d0aa4b0614ba8570867cf14575f279813c2b8599e5f3288f1bc0175d2db108d62ec2362d484c47a2e0059d5eb28c1a4d4a091d52
@@ -16,6 +16,61 @@ module Dbviewer
16
16
  render_success(total_relationships: total_relationships)
17
17
  end
18
18
 
19
+ def relationship_counts
20
+ table_name = params[:id]
21
+ record_id = params[:record_id]
22
+
23
+ unless table_name.present? && record_id.present?
24
+ render_error("Table name and record ID are required", 400)
25
+ return
26
+ end
27
+
28
+ begin
29
+ # Get table metadata to find relationships
30
+ metadata = fetch_table_metadata(table_name)
31
+
32
+ unless metadata
33
+ render_error("Table not found", 404)
34
+ return
35
+ end
36
+
37
+ # Get reverse foreign keys (has_many relationships)
38
+ reverse_foreign_keys = metadata.dig(:reverse_foreign_keys) || []
39
+
40
+ relationship_counts = reverse_foreign_keys.map do |rel|
41
+ begin
42
+ # Count records in the related table that reference this record
43
+ count_query = "SELECT COUNT(*) as count FROM #{rel[:from_table]} WHERE #{rel[:column]} = ?"
44
+ result = database_manager.connection.exec_query(count_query, "Count Query", [ record_id ])
45
+ count = result.rows.first&.first || 0
46
+
47
+ {
48
+ table: rel[:from_table],
49
+ foreign_key: rel[:column],
50
+ count: count.to_i
51
+ }
52
+ rescue => e
53
+ Rails.logger.error "Error counting relationships for #{rel[:from_table]}: #{e.message}"
54
+ {
55
+ table: rel[:from_table],
56
+ foreign_key: rel[:column],
57
+ count: 0,
58
+ error: e.message
59
+ }
60
+ end
61
+ end
62
+
63
+ render_success({
64
+ table_name: table_name,
65
+ record_id: record_id,
66
+ relationships: relationship_counts
67
+ })
68
+ rescue => e
69
+ Rails.logger.error "Error fetching relationship counts: #{e.message}"
70
+ render_error("Error fetching relationship counts: #{e.message}", 500)
71
+ end
72
+ end
73
+
19
74
  private
20
75
 
21
76
  def fetch_tables_count
@@ -40,13 +40,7 @@
40
40
  <% @tables.each do |table| %>
41
41
  <tr>
42
42
  <td class="fw-medium">
43
- <%
44
- # Include creation filter params in table links
45
- filter_params = {}
46
- filter_params[:creation_filter_start] = session[:creation_filter_start] if session[:creation_filter_start].present?
47
- filter_params[:creation_filter_end] = session[:creation_filter_end] if session[:creation_filter_end].present?
48
- %>
49
- <%= link_to table[:name], table_path(table[:name], filter_params), class: "text-decoration-none" %>
43
+ <%= link_to table[:name], table_path(table[:name]), class: "text-decoration-none" %>
50
44
  </td>
51
45
  <td class="text-end">
52
46
  <span class="badge bg-secondary-subtle"><%= table[:record_count] %></span>