Module: LlmCostTracker::Ledger::Schema::Adapter

Defined in:
lib/llm_cost_tracker/ledger/schema/adapter.rb

Constant Summary collapse

MYSQL_ADAPTERS =
%w[
  ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
  ActiveRecord::ConnectionAdapters::Mysql2Adapter
  ActiveRecord::ConnectionAdapters::TrilogyAdapter
].freeze
POSTGRESQL_ADAPTERS =
%w[
  ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
].freeze
MYSQL_PATTERN =
/mysql|trilogy|mariadb/i
POSTGRESQL_PATTERN =
/postgres/i

Class Method Summary collapse

Class Method Details

.ensure_supported!(value) ⇒ Object

Raises:



29
30
31
32
33
# File 'lib/llm_cost_tracker/ledger/schema/adapter.rb', line 29

def ensure_supported!(value)
  return if mysql?(value) || postgresql?(value)

  raise Error, "Unsupported database adapter: #{adapter_name(value)}. Use PostgreSQL or MySQL."
end

.json_column_errors(column, adapter_value, column_name) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/llm_cost_tracker/ledger/schema/adapter.rb', line 35

def json_column_errors(column, adapter_value, column_name)
  return [] unless column

  expected_type = postgresql?(adapter_value) ? "jsonb" : "json"
  return [] if json_column_type?(column, adapter_value)

  ["#{column_name} column must use #{expected_type} (got #{column.sql_type})"]
end

.json_column_type?(column, adapter_value) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
# File 'lib/llm_cost_tracker/ledger/schema/adapter.rb', line 44

def json_column_type?(column, adapter_value)
  sql_type = column.sql_type.to_s.downcase
  if postgresql?(adapter_value)
    column.type == :jsonb || sql_type == "jsonb"
  else
    column.type == :json || sql_type == "json" || sql_type == "longtext"
  end
end

.mysql?(value) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/llm_cost_tracker/ledger/schema/adapter.rb', line 21

def mysql?(value)
  adapter_instance?(value, MYSQL_ADAPTERS) || adapter_name(value).match?(MYSQL_PATTERN)
end

.postgresql?(value) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/llm_cost_tracker/ledger/schema/adapter.rb', line 25

def postgresql?(value)
  adapter_instance?(value, POSTGRESQL_ADAPTERS) || adapter_name(value).match?(POSTGRESQL_PATTERN)
end