Module: ActualDbSchema::StructureSqlParser
- Defined in:
- lib/actual_db_schema/structure_sql_parser.rb
Overview
Parses the content of a structure.sql file into a structured hash representation.
Class Method Summary collapse
- .normalize_table_name(table_name) ⇒ Object
- .parse_columns(columns_section) ⇒ Object
- .parse_string(sql_content) ⇒ Object
Class Method Details
.normalize_table_name(table_name) ⇒ Object
35 36 37 38 39 |
# File 'lib/actual_db_schema/structure_sql_parser.rb', line 35 def normalize_table_name(table_name) return table_name unless table_name.include?(".") table_name.split(".").last end |
.parse_columns(columns_section) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/actual_db_schema/structure_sql_parser.rb', line 17 def parse_columns(columns_section) columns = {} columns_section.each_line do |line| line.strip! next if line.empty? || line =~ /^(CONSTRAINT|PRIMARY KEY|FOREIGN KEY)/i match = line.match(/\A"?(?<col>\w+)"?\s+(?<type>\w+)(?<size>\s*\([\d,]+\))?/i) next unless match col_name = match[:col] col_type = match[:type].strip.downcase.to_sym = {} columns[col_name] = { type: col_type, options: } end columns end |
.parse_string(sql_content) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/actual_db_schema/structure_sql_parser.rb', line 8 def parse_string(sql_content) schema = {} table_regex = /CREATE TABLE\s+(?:"?([\w.]+)"?)\s*\((.*?)\);/m sql_content.scan(table_regex) do |table_name, columns_section| schema[normalize_table_name(table_name)] = parse_columns(columns_section) end schema end |