public
Created — forked from adelevie/dbdocs.rb

  • Download Gist
dbdocs.rb
Ruby
1 2 3 4 5 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
require 'sinatra'
require 'active_record'
 
ActiveRecord::Base.establish_connection(
:adapter => 'mysql',
:encoding => 'utf8',
:database => 'foo',
:username => 'bar',
:password => 'baz',
:host => 'localhost'
)
 
class Table
attr_accessor :name
def columns
ActiveRecord::Base.connection.execute("show fields from #{@name};")
end
end
 
def yes_no_to_null(x)
case x
when "YES"
"NULL"
when "NO"
"NOT NULL"
end
end
 
get '/' do
@tables = Array.new
ActiveRecord::Base.connection.execute('show tables;').each do |t|
table = Table.new
table.name = t[0]
@tables << table
end
erb :index
end
 
__END__
 
@@ index
<% @tables.each do |table| %>
<h3><u><%= table.name %></u></h3>
<% table.columns.each do |column| %>
<p>-<strong><%= column[0] %></strong> <i><%= column[1] %></i> <%= yes_no_to_null(column[2]) %></p>
<% end %>
<br />
<% end %>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.