Skip to content

Instantly share code, notes, and snippets.

@jgn
Last active January 20, 2016 14:20
Show Gist options
  • Save jgn/a2fc12a818d3c99f2b8a to your computer and use it in GitHub Desktop.
Save jgn/a2fc12a818d3c99f2b8a to your computer and use it in GitHub Desktop.
Get table, column data
# this is how you bring in a gem (a module). The gem should previous be
# installed with: gem install activerecord (yes, the gem name and what
# "require" are different.)
require 'active_record'
# This is a hash, like a dictionary in Python.
# The classic syntax looks like this:
# spec = { 'adapter' => 'postgresql', 'database' => 'icis_production' }
# However, the tokens preceded with the colon are actually symbols, where
# are like strings but they can't be changed. So, the following hash
# is a little closer to:
# spec = { 'adapter' => :postgresql, 'database' => :icis_production }
# It is very common for the hash KEYS to be symbols, too, like so:
# spec = { :adapter => :postgresql, :database => :icis_production }
# Finally, in Ruby 2.x, new syntax was added so that if the key is a symbol,
# you can put the colon at the end of the key name, and leave out the
# fat arrow, e.g.,
# spec = { adapter: :postgresql, database: :icis_production }
# The ENV['GM_USERNAME'] means: get GM_USERNAME from the shell environment.
# On a local machine, typically the username and password are not required,
# so the ENV['GM_USERNAME'] evaluates to nothing if GM_USERNAME is not
# defined.
spec = {
adapter: :postgresql,
database: :icis_production,
username: ENV['GM_USERNAME'],
password: ENV['GM_PASSWORD']
}
# See http://api.rubyonrails.org/classes/ActiveRecord/ConnectionHandling.html#method-i-establish_connection
ActiveRecord::Base.establish_connection(spec)
# We could have written:
# ActiveRecord::Base.establish_connection(adapter: :postgresql, database: :icis_production, username: ENV['GM_USERNAME'], password: ENV['GM_PASSWORD'])
# Notice how if written this way, the hash with the params doesn't require
# the enclosing {}. By this means, a single hash param looks like named
# arguments.
# See
# https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb#L41
# and
# https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb#L72
# The .each here is invoking an iterator on the collection of tables.
# THe do block means . . . pass the table_name into the block.
# This is the biggest single feature of Ruby that converts people: Trivially
# easy anonymous functions.
ActiveRecord::Base.connection.tables.each do |table_name|
# Here, the #{table_name} inside the double quotes means:
# Interpolate the value of the expression table_name as a String.
puts "\n #{table_name}"
# See https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb#L215
ActiveRecord::Base.connection.columns(table_name).each do |c|
# Here, the % means: User the left side as a format, and the right
# side is an array of things to plug into the format.
# See http://ruby-doc.org/core-2.2.0/String.html#method-i-25
# and http://ruby-doc.org/core-2.2.0/Kernel.html#method-i-sprintf
puts ' %s: %s %s' % [c.name, c.type, c.limit]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment