Module: Spectacles::SchemaDumper

Defined in:
lib/spectacles/schema_dumper.rb

Class Method Summary collapse

Class Method Details

.dump_materialized_view(stream, connection, view_name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/spectacles/schema_dumper.rb', line 32

def self.dump_materialized_view(stream, connection, view_name)
  definition, options = connection.materialized_view_build_query(view_name)
  options[:force] = true

  stream.print <<-CREATEVIEW

  create_materialized_view #{view_name.to_sym.inspect}, #{format_option_hash(options)} do
<<-SQL
  #{definition}
SQL
  end
  CREATEVIEW
end

.dump_materialized_views(dumper, stream, connection) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/spectacles/schema_dumper.rb', line 11

def self.dump_materialized_views(dumper, stream, connection)
  unless (Spectacles.config.enable_schema_dump == false)
    if connection.supports_materialized_views?
      connection.materialized_views.sort.each do |view|
        dump_materialized_view(stream, connection, view)
        dumper.send(:indexes, view, stream)
      end
    end
  end
end

.dump_view(stream, connection, view_name) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/spectacles/schema_dumper.rb', line 22

def self.dump_view(stream, connection, view_name)
  stream.print <<-CREATEVIEW

  create_view :#{view_name}, :force => true do
"#{connection.view_build_query(view_name)}"
  end

  CREATEVIEW
end

.dump_views(stream, connection) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/spectacles/schema_dumper.rb', line 3

def self.dump_views(stream, connection)
  unless (Spectacles.config.enable_schema_dump == false)
    connection.views.sort.each do |view|
      dump_view(stream, connection, view)
    end
  end
end

.format_option_hash(hash) ⇒ Object



46
47
48
49
50
# File 'lib/spectacles/schema_dumper.rb', line 46

def self.format_option_hash(hash)
  hash.map do |key, value|
    "#{key}: #{format_option_value(value)}"
  end.join(", ")
end

.format_option_value(value) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/spectacles/schema_dumper.rb', line 52

def self.format_option_value(value)
  case value
  when Hash then "{ #{format_option_hash(value)} }"
  when /^\d+$/ then value.to_i
  when /^\d+\.\d+$/ then value.to_f
  when true, false then value.inspect
  else raise "can't format #{value.inspect}"
  end
end