Skip to content

Instantly share code, notes, and snippets.

@maxim
Created December 18, 2011 09:10
Show Gist options
  • Save maxim/1492796 to your computer and use it in GitHub Desktop.
Save maxim/1492796 to your computer and use it in GitHub Desktop.
Quick schema.rb lookup
# Quick schema.rb lookup shell function.
#
# Usage
# schema — print all tables in schema.rb
# schema [table] — print particular table in schema.rb
function schema() {
if test "$1" = ""; then
grep 'create_table' db/schema.rb | cut -d \" -f2
else
sed -n "/create_table \"$1/,/^ *end *$/p" db/schema.rb
fi
}
#!/usr/bin/env ruby
# Quick schema.rb lookup.
#
# Usage (assuming script name is schema)
# schema — print all tables in schema.rb
# schema [table] — print particular table in schema.rb
unless File.exists?('db/schema.rb')
puts 'No db/schema.rb found in this directory'
exit(1)
end
table_name = ARGV[0] && ARGV[0].strip
schema = File.open('db/schema.rb')
if table_name.nil?
while schema.gets
if $_ =~ /create_table\s+\"([^"]+)\"/
puts $1
end
end
else
while schema.gets
beginning_of_table = ($_ =~ /create_table\s+\"#{table_name}\"/)
end_of_table = ($_ =~ /^\s*end\s*$/)
if (beginning_of_table..end_of_table)
puts $_
end
end
end
schema.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment