Module: RailVerdict::RailsContext::SchemaParser

Defined in:
lib/rail_verdict/rails_context/schema_parser.rb

Class Method Summary collapse

Class Method Details

.parse(repository_root:) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rail_verdict/rails_context/schema_parser.rb', line 6

def self.parse(repository_root:)
  path = File.join(repository_root, "db/schema.rb")
  return {} unless File.file?(path)
  return {} if File.size(path) > Limits::MAX_FILE_BYTES rescue nil

  text = File.binread(path)
  return {} if text.bytesize > Limits::MAX_FILE_BYTES

  text.force_encoding(Encoding::UTF_8)
  return {} unless text.valid_encoding?

  tables = {}
  text.scan(/create_table\s+["']([^"']+)["']/) do |match|
    table = match[0].to_s.strip
    next if table.empty?
    next if tables.key?(table)

    break if tables.length >= Limits::MAX_SCHEMA_TABLES

    snippet = extract_table_snippet(text, table)
    tables[table] = snippet
  end
  tables
rescue StandardError
  {}
end