Created
November 3, 2010 14:09
-
-
Save jcsjcs/661114 to your computer and use it in GitHub Desktop.
Modified version of "Database documentation in 50 lines of Sinatra"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Modified version of "Database documentation in 50 lines of Sinatra" | |
# at http://www.alandelevie.com/2010/10/26/database-documentation-in-50-lines-of-sinatra/ | |
# (Works with Postgres and MySQL. Possibly sqlite too.) | |
# | |
# Useful for quickly comparing two database structures: | |
# 1) Run with a connection to db1 and choose the sorted view. | |
# 2) Copy the table structures into the left-hand side of a diff viewer. | |
# 3) Run with a connection to db2 and choose the sorted view. | |
# 4) Copy the table structures into the right-hand side of a diff viewer. | |
# 5) Compare... | |
require 'sinatra' | |
require 'active_record' | |
ActiveRecord::Base.establish_connection( | |
:adapter => 'mysql', | |
:encoding => 'utf8', | |
:database => 'db_name', | |
:username => 'user_name', | |
:password => 'pswd', | |
:host => 'localhost' | |
) | |
class Table | |
attr_accessor :name | |
def initialize(name) | |
@name = name | |
end | |
def columns | |
ActiveRecord::Base.connection.columns(@name).map {|c| [c.name, c.sql_type, c.null ? 'NULL' : 'NOT NULL'] } | |
end | |
end | |
def list_of_tables | |
tables = [] | |
ActiveRecord::Base.connection.tables.each do |t| | |
tables << Table.new(t) | |
end | |
tables.sort_by {|t| t.name } | |
end | |
get '/' do | |
@tables = list_of_tables | |
@dbname = ActiveRecord::Base.connection.instance_variable_get(:@config)[:database].split('/').last | |
erb :index | |
end | |
get '/sorted' do | |
@tables = list_of_tables | |
@dbname = ActiveRecord::Base.connection.instance_variable_get(:@config)[:database].split('/').last | |
erb :indexsorted | |
end | |
__END__ | |
@@ index | |
<style type="text/css"> table {border-collapse:collapse;} td {padding: 0 12px; color: #444;} th {color: #303030; font-size:120%; background:#B0C5E0; padding: 4px 8px 8px; text-align:left; border-top: thin solid #bbb; border-left: thin solid #bbb; border-bottom: thin solid #555; border-right: thin solid #555;} tr.data:hover {background:#B0C5E0;} </style> | |
<a href="/sorted">Go to list with sorted fields</a> | |
<hr /> | |
<h2>Tables in <em><%= @dbname %></em></h2> | |
<table> | |
<% @tables.each_slice(5) do |table| %> | |
<tr><td> | |
<%= table.map {|t| "<a href='##{t.name}'>#{t.name}</a>" }.join('</td><td>') %> | |
</td></tr> | |
<% end %> | |
</table> | |
<hr /><br /> | |
<table> | |
<% @tables.each do |table| %> | |
<tr><th colspan="3"><a name="<%= table.name %>"><%= table.name %></a></th></tr> | |
<% table.columns.each do |column| %> | |
<tr class="data"><td><strong><%= column[0] %></strong></td><td><i><%= column[1] %></i></td><td><%= column[2] %></td></tr> | |
<% end %> | |
<tr><td colspan="3"> </td></tr> | |
<% end %> | |
</table> | |
@@ indexsorted | |
<style type="text/css"> table {border-collapse:collapse;} td {padding: 0 12px; color: #444;} th {color: #303030; font-size:120%; background:#B0C5E0; padding: 4px 8px 8px; text-align:left; border-top: thin solid #bbb; border-left: thin solid #bbb; border-bottom: thin solid #555; border-right: thin solid #555;} tr.data:hover {background:#B0C5E0;} </style> | |
<a href="/">Go home</a> | |
<hr /> | |
<h2>Tables in <em><%= @dbname %></em> with fields sorted alphabetically</h2> | |
<table> | |
<% @tables.each_slice(5) do |table| %> | |
<tr><td> | |
<%= table.map {|t| "<a href='##{t.name}'>#{t.name}</a>" }.join('</td><td>') %> | |
</td></tr> | |
<% end %> | |
</table> | |
<hr /><br /> | |
<table> | |
<% @tables.each do |table| %> | |
<tr><th colspan="3"><a name="<%= table.name %>"><%= table.name %></a></th></tr> | |
<% table.columns.sort.each do |column| %> | |
<tr class="data"><td><strong><%= column[0] %></strong></td><td><i><%= column[1] %></i></td><td><%= column[2] %></td></tr> | |
<% end %> | |
<tr><td colspan="3"> </td></tr> | |
<% end %> | |
</table> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment