Created
December 18, 2011 09:10
-
-
Save maxim/1492796 to your computer and use it in GitHub Desktop.
Quick schema.rb lookup
This file contains hidden or 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
# 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 | |
} |
This file contains hidden or 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
#!/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