Class: Dbviewer::Database::MetadataManager
- Inherits:
-
Object
- Object
- Dbviewer::Database::MetadataManager
- Defined in:
- lib/dbviewer/database/metadata_manager.rb
Overview
MetadataManager handles retrieving and managing table structure information
Instance Method Summary collapse
-
#column_exists?(table_name, column_name) ⇒ Boolean
Check if a column exists in a table.
-
#fetch_foreign_keys(table_name) ⇒ Array<Hash>
Get foreign keys.
-
#fetch_indexes(table_name) ⇒ Array<Hash>
Get table indexes.
-
#fetch_reverse_foreign_keys(table_name) ⇒ Array<Hash>
Get reverse foreign keys (tables that reference this table).
-
#initialize(connection, cache_manager) ⇒ MetadataManager
constructor
Initialize with a connection and cache manager.
-
#primary_key(table_name) ⇒ String?
Get the primary key of a table.
-
#table_columns(table_name) ⇒ Array<Hash>
Get column information for a table.
-
#table_metadata(table_name) ⇒ Hash
Get detailed metadata about a table.
-
#tables ⇒ Array<String>
Get all tables in the database.
Constructor Details
#initialize(connection, cache_manager) ⇒ MetadataManager
Initialize with a connection and cache manager
8 9 10 11 |
# File 'lib/dbviewer/database/metadata_manager.rb', line 8 def initialize(connection, cache_manager) @connection = connection @cache_manager = cache_manager end |
Instance Method Details
#column_exists?(table_name, column_name) ⇒ Boolean
Check if a column exists in a table
66 67 68 69 |
# File 'lib/dbviewer/database/metadata_manager.rb', line 66 def column_exists?(table_name, column_name) columns = table_columns(table_name) columns.any? { |col| col[:name].to_s == column_name.to_s } end |
#fetch_foreign_keys(table_name) ⇒ Array<Hash>
Get foreign keys
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/dbviewer/database/metadata_manager.rb', line 87 def fetch_foreign_keys(table_name) @connection.foreign_keys(table_name).map do |fk| { name: fk.name, from_table: fk.from_table, to_table: fk.to_table, column: fk.column, primary_key: fk.primary_key } end end |
#fetch_indexes(table_name) ⇒ Array<Hash>
Get table indexes
74 75 76 77 78 79 80 81 82 |
# File 'lib/dbviewer/database/metadata_manager.rb', line 74 def fetch_indexes(table_name) @connection.indexes(table_name).map do |index| { name: index.name, columns: index.columns, unique: index.unique } end end |
#fetch_reverse_foreign_keys(table_name) ⇒ Array<Hash>
Get reverse foreign keys (tables that reference this table)
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/dbviewer/database/metadata_manager.rb', line 102 def fetch_reverse_foreign_keys(table_name) tables .reject { |other_table| other_table == table_name } .flat_map do |other_table| @connection.foreign_keys(other_table) .select { |fk| fk.to_table == table_name } .map do |fk| { name: fk.name, from_table: fk.from_table, to_table: fk.to_table, column: fk.column, primary_key: fk.primary_key, relationship_type: "has_many" } end end end |
#primary_key(table_name) ⇒ String?
Get the primary key of a table
55 56 57 58 59 60 |
# File 'lib/dbviewer/database/metadata_manager.rb', line 55 def primary_key(table_name) @connection.primary_key(table_name) rescue => e Rails.logger.error("[DBViewer] Error retrieving primary key for table #{table_name}: #{e.}") nil end |
#table_columns(table_name) ⇒ Array<Hash>
Get column information for a table
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/dbviewer/database/metadata_manager.rb', line 22 def table_columns(table_name) # Cache columns for longer since schema changes are infrequent @cache_manager.fetch("columns-#{table_name}", expires_in: 600) do @connection.columns(table_name).map do |column| { name: column.name, type: column.type, null: column.null, default: column.default, primary: column.name == primary_key(table_name) } end end end |
#table_metadata(table_name) ⇒ Hash
Get detailed metadata about a table
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/dbviewer/database/metadata_manager.rb', line 40 def (table_name) # Cache metadata for longer since schema relationships change infrequently @cache_manager.fetch("metadata-#{table_name}", expires_in: 600) do { primary_key: primary_key(table_name), indexes: fetch_indexes(table_name), foreign_keys: fetch_foreign_keys(table_name), reverse_foreign_keys: fetch_reverse_foreign_keys(table_name) } end end |
#tables ⇒ Array<String>
Get all tables in the database
15 16 17 |
# File 'lib/dbviewer/database/metadata_manager.rb', line 15 def tables @connection.tables.sort end |