Module: AR::Sequence::SchemaDumper

Defined in:
lib/ar/sequence/schema_dumper.rb

Instance Method Summary collapse

Instance Method Details

#header(stream) ⇒ Object



6
7
8
9
# File 'lib/ar/sequence/schema_dumper.rb', line 6

def header(stream)
  super
  sequences(stream)
end

#retrieve_search_pathObject



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

def retrieve_search_path
  user = @connection.select_one("select user").values.first

  @connection
    .select_one("show search_path")
    .values
    .first
    .split(", ")
    .map {|path| path == '"$user"' ? user : path }
end

#sequences(stream) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ar/sequence/schema_dumper.rb', line 22

def sequences(stream)
  sequences = @connection.check_sequences
  search_path = retrieve_search_path

  return if sequences.empty?

  sequences.each do |seq|
    schema = seq["sequence_schema"]

    sequence_full_name = [
      search_path.include?(schema) ? nil : schema,
      seq["sequence_name"]
    ].compact.join(".")

    next unless @connection.custom_sequence?(sequence_full_name)

    start_value = seq["start_value"]
    increment = seq["increment"]

    options = []

    if start_value && Integer(start_value) != 1
      options << "start: #{start_value}"
    end

    if increment && Integer(increment) != 1
      options << "increment: #{increment}"
    end

    statement = [
      "create_sequence",
      sequence_full_name.inspect
    ].join(" ")

    if options.any?
      statement << (options.any? ? ", #{options.join(', ')}" : "")
    end

    stream.puts "  #{statement}"
  end

  stream.puts
end