Class: ActiveRecord::ConnectionAdapters::Materialize::SchemaDumper

Inherits:
SchemaDumper
  • Object
show all
Defined in:
lib/active_record/connection_adapters/materialize/schema_dumper.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#dump(stream) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/active_record/connection_adapters/materialize/schema_dumper.rb', line 7

def dump(stream)
  header(stream)
  extensions(stream)
  tables(stream)
  views_and_sources(stream)
  trailer(stream)
  stream
end

#indexes_in_create(table, stream) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/active_record/connection_adapters/materialize/schema_dumper.rb', line 77

def indexes_in_create(table, stream)
  if (indexes = @connection.indexes(table)).any?
    index_statements = indexes
      .select { |index| !index.name.include?("_primary_idx") }
      .map do |index|
        "    t.index #{index_parts(index).join(', ')}"
      end
    stream.puts index_statements.sort.join("\n")
  end
end

#source(source_name, stream) ⇒ Object

Create source



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/active_record/connection_adapters/materialize/schema_dumper.rb', line 30

def source(source_name, stream)
  options = @connection.source_options(source_name)
  begin
    src_stream = StringIO.new

    src_stream.print "  create_source #{remove_prefix_and_suffix(source_name).inspect}"
    src_stream.print ", source_type: :#{options[:source_type]}" unless options[:source_type].nil?
    src_stream.puts ", publication: #{options[:publication].inspect}" unless options[:publication].nil?
    src_stream.puts ", materialize: #{options[:materialize]}" unless options[:materialize].nil?
    src_stream.puts

    src_stream.rewind
    stream.print src_stream.read

  rescue StandardError => e
    stream.puts "# Could not dump source #{source_name.inspect} because of following #{e.class}"
    stream.puts "#   #{e.message}"
    stream.puts
  end
end

#view(view_name, stream) ⇒ Object

Create view



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/active_record/connection_adapters/materialize/schema_dumper.rb', line 52

def view(view_name, stream)
  creation_query = @connection.view_sql(view_name)
  begin
    src_stream = StringIO.new
    src_stream.puts "  create_view #{remove_prefix_and_suffix(view_name).inspect} do"

    src_stream.puts "    <<-SQL.squish"
    src_stream.puts "      #{creation_query}"
    src_stream.puts "    SQL"

    src_stream.puts "  end"
    src_stream.puts

    # TODO indexes

    src_stream.rewind
    stream.print src_stream.read

  rescue StandardError => e
    stream.puts "# Could not dump source #{source_name.inspect} because of following #{e.class}"
    stream.puts "#   #{e.message}"
    stream.puts
  end
end

#views_and_sources(stream) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_record/connection_adapters/materialize/schema_dumper.rb', line 16

def views_and_sources(stream)
  sorted_sources = @connection.sources.sort
  sorted_sources.each do |source_name|
    source(source_name, stream)
  end

  # TODO sort views by dependencies
  sorted_views = @connection.views.sort
  sorted_views.each do |view_name|
    view(view_name, stream)
  end
end