discourse-ai/lib/ai_bot/commands/db_schema_command.rb
Sam 6ddc17fd61
DEV: port directory structure to Zeitwerk (#319)
Previous to this change we relied on explicit loading for a files in Discourse AI.

This had a few downsides:

- Busywork whenever you add a file (an extra require relative)
- We were not keeping to conventions internally ... some places were OpenAI others are OpenAi
- Autoloader did not work which lead to lots of full application broken reloads when developing.

This moves all of DiscourseAI into a Zeitwerk compatible structure.

It also leaves some minimal amount of manual loading (automation - which is loading into an existing namespace that may or may not be there)

To avoid needing /lib/discourse_ai/... we mount a namespace thus we are able to keep /lib pointed at ::DiscourseAi

Various files were renamed to get around zeitwerk rules and minimize usage of custom inflections

Though we can get custom inflections to work it is not worth it, will require a Discourse core patch which means we create a hard dependency.
2023-11-29 15:17:46 +11:00

55 lines
1.3 KiB
Ruby

#frozen_string_literal: true
module DiscourseAi::AiBot::Commands
class DbSchemaCommand < Command
class << self
def name
"schema"
end
def desc
"Will load schema information for specific tables in the database"
end
def parameters
[
Parameter.new(
name: "tables",
description:
"list of tables to load schema information for, comma seperated list eg: (users,posts))",
type: "string",
required: true,
),
]
end
end
def result_name
"results"
end
def description_args
{ tables: @tables.join(", ") }
end
def process(tables:)
@tables = tables.split(",").map(&:strip)
table_info = {}
DB
.query(<<~SQL, @tables)
select table_name, column_name, data_type from information_schema.columns
where table_schema = 'public'
and table_name in (?)
order by table_name
SQL
.each { |row| (table_info[row.table_name] ||= []) << "#{row.column_name} #{row.data_type}" }
schema_info =
table_info.map { |table_name, columns| "#{table_name}(#{columns.join(",")})" }.join("\n")
{ schema_info: schema_info, tables: tables }
end
end
end