Class: RailsAiBridge::Introspectors::Schema::StaticSchemaParser

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_bridge/introspectors/schema/static_schema_parser.rb

Overview

Parses a +db/schema.rb+ file as plain text, without a live database connection.

Each instance is single-use: construct it with the file content and a configuration object, call #call, and discard. No mutable state escapes the instance.

== Supported schema DSL lines

  • +create_table "name"+ — opens a table context
  • +t. "name"+ — adds a column to the current table
  • +add_index "table", "col"+ or +add_index "table", ["col"]+ — adds an index entry to the named table

Internal Rails tables (+ar_internal_metadata+, +schema_migrations+) and any table matching Config::Introspection#excluded_tables are silently skipped.

Examples:

content = File.read("db/schema.rb")
result  = StaticSchemaParser.new(content: content, config: RailsAiBridge.configuration).call
# => { adapter: "static_parse", tables: { ... }, total_tables: N, note: "..." }

See Also:

Constant Summary collapse

TABLE_LINE =

Regex matching a +create_table+ declaration line.

/create_table\s+"(\w+)"/
COLUMN_LINE =

Regex matching a column definition inside a table block (+t. "name"+).

/t\.(\w+)\s+"(\w+)"/
INDEX_LINE =

Regex matching an +add_index+ statement (bare or array column form). Only the first column is captured for multi-column indexes — sufficient for context output but not a complete representation.

/add_index\s+"(\w+)",\s+\[?"(\w+)"/
INTERNAL_TABLES =

Rails-managed tables that must never appear in introspection output.

%w[ar_internal_metadata schema_migrations].freeze

Instance Method Summary collapse

Constructor Details

#initialize(content:, config:) ⇒ StaticSchemaParser

Returns a new instance of StaticSchemaParser.

Parameters:



48
49
50
51
52
53
# File 'lib/rails_ai_bridge/introspectors/schema/static_schema_parser.rb', line 48

def initialize(content:, config:)
  @content       = content
  @config        = config
  @tables        = {}
  @current_table = nil
end

Instance Method Details

#callHash{Symbol => Object}

Parse the schema content and return the tables hash.

Returns:

  • (Hash{Symbol => Object})

    with keys +:adapter+, +:tables+, +:total_tables+, and +:note+



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rails_ai_bridge/introspectors/schema/static_schema_parser.rb', line 59

def call
  @content.each_line do |line|
    next if parse_table_line?(line)
    next if parse_column_line?(line)

    parse_index_line?(line)
  end

  {
    adapter: 'static_parse',
    tables: @tables,
    total_tables: @tables.size,
    note: 'Parsed from db/schema.rb (no DB connection)'
  }
end