Class: ConsoleAgent::Tools::SchemaTools

Inherits:
Object
  • Object
show all
Defined in:
lib/console_agent/tools/schema_tools.rb

Instance Method Summary collapse

Instance Method Details

#describe_table(table_name) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/console_agent/tools/schema_tools.rb', line 16

def describe_table(table_name)
  return "ActiveRecord is not connected." unless ar_connected?
  return "Error: table_name is required." if table_name.nil? || table_name.strip.empty?

  table_name = table_name.strip
  unless connection.tables.include?(table_name)
    return "Table '#{table_name}' not found. Use list_tables to see available tables."
  end

  cols = connection.columns(table_name).map do |c|
    parts = ["#{c.name}:#{c.type}"]
    parts << "nullable" if c.null
    parts << "default=#{c.default}" unless c.default.nil?
    parts.join(" ")
  end

  indexes = connection.indexes(table_name).map do |idx|
    unique = idx.unique ? "UNIQUE " : ""
    "#{unique}INDEX on (#{idx.columns.join(', ')})"
  end

  result = "Table: #{table_name}\n"
  result += "Columns:\n"
  cols.each { |c| result += "  #{c}\n" }
  unless indexes.empty?
    result += "Indexes:\n"
    indexes.each { |i| result += "  #{i}\n" }
  end
  result
rescue => e
  "Error describing table '#{table_name}': #{e.message}"
end

#list_tablesObject



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/console_agent/tools/schema_tools.rb', line 4

def list_tables
  return "ActiveRecord is not connected." unless ar_connected?

  tables = connection.tables.sort
  tables.reject! { |t| t == 'schema_migrations' || t == 'ar_internal_metadata' }
  return "No tables found." if tables.empty?

  tables.join(", ")
rescue => e
  "Error listing tables: #{e.message}"
end