Skip to content

Instantly share code, notes, and snippets.

@erik-megarad
Created April 14, 2015 14:50
Show Gist options
  • Save erik-megarad/fac21da9359d41549ed4 to your computer and use it in GitHub Desktop.
Save erik-megarad/fac21da9359d41549ed4 to your computer and use it in GitHub Desktop.
Get views dumped into schema.rb
module ActiveRecord
class SchemaDumper
def views(stream)
# Order by reltype is important so that the views are dumped in the order
# they are created, in case one view is dependent on others
views = @connection.query(%Q{
SELECT n.nspname AS schemaname,
c.relname AS viewname,
pg_get_viewdef(c.oid) AS definition
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind = 'v'::"char"
AND n.nspname = ANY(current_schemas(false))
ORDER by reltype ASC
})
views.each do |view_details|
schema, viewname, definition = *view_details
definition.gsub!(/\n/, ' ')
definition.squish!
output = <<-RUBY
execute %Q{
CREATE VIEW #{schema}.#{viewname}
AS #{definition}
}
RUBY
stream.puts output
end
end
def trailer_with_execute(stream)
views(stream)
trailer_without_execute(stream)
end
alias_method_chain :trailer, :execute
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment