|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Git |
| 4 | + module Pkgs |
| 5 | + module Commands |
| 6 | + class Schema |
| 7 | + FORMATS = %w[text sql json markdown].freeze |
| 8 | + |
| 9 | + def initialize(args) |
| 10 | + @args = args |
| 11 | + @options = parse_options |
| 12 | + end |
| 13 | + |
| 14 | + def run |
| 15 | + repo = Repository.new |
| 16 | + |
| 17 | + unless Database.exists?(repo.git_dir) |
| 18 | + $stderr.puts "Database not initialized. Run 'git pkgs init' first." |
| 19 | + exit 1 |
| 20 | + end |
| 21 | + |
| 22 | + Database.connect(repo.git_dir) |
| 23 | + tables = fetch_schema |
| 24 | + |
| 25 | + case @options[:format] |
| 26 | + when "sql" |
| 27 | + output_sql(tables) |
| 28 | + when "json" |
| 29 | + output_json(tables) |
| 30 | + when "markdown" |
| 31 | + output_markdown(tables) |
| 32 | + else |
| 33 | + output_text(tables) |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + def fetch_schema |
| 38 | + conn = ActiveRecord::Base.connection |
| 39 | + tables = {} |
| 40 | + |
| 41 | + conn.tables.sort.each do |table_name| |
| 42 | + next if table_name == "ar_internal_metadata" |
| 43 | + next if table_name == "schema_migrations" |
| 44 | + |
| 45 | + columns = conn.columns(table_name).map do |col| |
| 46 | + { |
| 47 | + name: col.name, |
| 48 | + type: col.type, |
| 49 | + sql_type: col.sql_type, |
| 50 | + null: col.null, |
| 51 | + default: col.default |
| 52 | + } |
| 53 | + end |
| 54 | + |
| 55 | + indexes = conn.indexes(table_name).map do |idx| |
| 56 | + { |
| 57 | + name: idx.name, |
| 58 | + columns: idx.columns, |
| 59 | + unique: idx.unique |
| 60 | + } |
| 61 | + end |
| 62 | + |
| 63 | + tables[table_name] = { columns: columns, indexes: indexes } |
| 64 | + end |
| 65 | + |
| 66 | + tables |
| 67 | + end |
| 68 | + |
| 69 | + def output_text(tables) |
| 70 | + tables.each do |table_name, info| |
| 71 | + puts "#{table_name}" |
| 72 | + puts "-" * table_name.length |
| 73 | + |
| 74 | + info[:columns].each do |col| |
| 75 | + nullable = col[:null] ? "NULL" : "NOT NULL" |
| 76 | + default = col[:default] ? " DEFAULT #{col[:default]}" : "" |
| 77 | + puts " #{col[:name].ljust(25)} #{col[:sql_type].ljust(15)} #{nullable}#{default}" |
| 78 | + end |
| 79 | + |
| 80 | + if info[:indexes].any? |
| 81 | + puts |
| 82 | + puts " Indexes:" |
| 83 | + info[:indexes].each do |idx| |
| 84 | + unique = idx[:unique] ? "UNIQUE " : "" |
| 85 | + puts " #{unique}#{idx[:name]} (#{idx[:columns].join(', ')})" |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + puts |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + def output_sql(tables) |
| 94 | + conn = ActiveRecord::Base.connection |
| 95 | + |
| 96 | + tables.each do |table_name, info| |
| 97 | + sql = conn.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name='#{table_name}'").first |
| 98 | + puts sql["sql"] + ";" if sql |
| 99 | + puts |
| 100 | + |
| 101 | + info[:indexes].each do |idx| |
| 102 | + idx_sql = conn.execute("SELECT sql FROM sqlite_master WHERE type='index' AND name='#{idx[:name]}'").first |
| 103 | + puts idx_sql["sql"] + ";" if idx_sql && idx_sql["sql"] |
| 104 | + end |
| 105 | + |
| 106 | + puts if info[:indexes].any? |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + def output_json(tables) |
| 111 | + require "json" |
| 112 | + puts JSON.pretty_generate(tables) |
| 113 | + end |
| 114 | + |
| 115 | + def output_markdown(tables) |
| 116 | + tables.each do |table_name, info| |
| 117 | + puts "## #{table_name}" |
| 118 | + puts |
| 119 | + puts "| Column | Type | Nullable | Default |" |
| 120 | + puts "|--------|------|----------|---------|" |
| 121 | + |
| 122 | + info[:columns].each do |col| |
| 123 | + nullable = col[:null] ? "Yes" : "No" |
| 124 | + default = col[:default] || "" |
| 125 | + puts "| #{col[:name]} | #{col[:sql_type]} | #{nullable} | #{default} |" |
| 126 | + end |
| 127 | + |
| 128 | + if info[:indexes].any? |
| 129 | + puts |
| 130 | + puts "**Indexes:**" |
| 131 | + info[:indexes].each do |idx| |
| 132 | + unique = idx[:unique] ? " (unique)" : "" |
| 133 | + puts "- `#{idx[:name]}`#{unique}: #{idx[:columns].join(', ')}" |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + puts |
| 138 | + end |
| 139 | + end |
| 140 | + |
| 141 | + def parse_options |
| 142 | + options = { format: "text" } |
| 143 | + |
| 144 | + parser = OptionParser.new do |opts| |
| 145 | + opts.banner = "Usage: git pkgs schema [options]" |
| 146 | + |
| 147 | + opts.on("--format=FORMAT", FORMATS, "Output format: #{FORMATS.join(', ')} (default: text)") do |v| |
| 148 | + options[:format] = v |
| 149 | + end |
| 150 | + |
| 151 | + opts.on("-h", "--help", "Show this help") do |
| 152 | + puts opts |
| 153 | + exit |
| 154 | + end |
| 155 | + end |
| 156 | + |
| 157 | + parser.parse!(@args) |
| 158 | + options |
| 159 | + end |
| 160 | + end |
| 161 | + end |
| 162 | + end |
| 163 | +end |
0 commit comments