diff --git a/lib/scenic/schema_dumper.rb b/lib/scenic/schema_dumper.rb index 340acfde..59a26ccc 100644 --- a/lib/scenic/schema_dumper.rb +++ b/lib/scenic/schema_dumper.rb @@ -15,7 +15,14 @@ def views(stream) dumpable_views_in_database.each do |view| stream.puts(view.to_schema) - indexes(view.name, stream) + + index_stream = StringIO.new + indexes(view.name, index_stream) + + if index_stream.string.present? + stream.puts + stream.puts(index_stream.string) + end end end diff --git a/lib/scenic/view.rb b/lib/scenic/view.rb index 3f39d7ed..2b21b68b 100644 --- a/lib/scenic/view.rb +++ b/lib/scenic/view.rb @@ -1,3 +1,5 @@ +require "niceql" + module Scenic # The in-memory representation of a view definition. # @@ -46,11 +48,19 @@ def to_schema <<-DEFINITION create_view #{UnaffixedName.for(name).inspect}, #{materialized_option}sql_definition: <<-\SQL - #{escaped_definition.indent(2)} + #{formatted_definition.indent(2)} SQL DEFINITION end + def formatted_definition + colorize = false + + Niceql::Prettifier + .prettify_sql(escaped_definition, colorize) + .chomp(";") + end + def escaped_definition definition.gsub("\\", "\\\\\\") end diff --git a/scenic.gemspec b/scenic.gemspec index 276beb75..e7632859 100644 --- a/scenic.gemspec +++ b/scenic.gemspec @@ -35,6 +35,7 @@ Gem::Specification.new do |spec| spec.add_dependency "activerecord", ">= 4.0.0" spec.add_dependency "railties", ">= 4.0.0" + spec.add_dependency "niceql", ">= 0.6.1" spec.required_ruby_version = ">= 2.3.0" end