Module: ActiveRecord::Sequence::SchemaDumper

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

Instance Method Summary collapse

Instance Method Details

#header(stream) ⇒ Object



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

def header(stream)
  super
  sequences(stream)
end

#sequences(stream) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
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
# File 'lib/active_record/sequence/schema_dumper.rb', line 11

def sequences(stream)
  sequences = @connection.check_sequences
  return if sequences.empty?

  sequences.each do |seq|
    start_value = seq['START']
    increment = seq['INCREMENT']
    cache = seq['CACHE']
    cache_value = seq['CACHE_VALUE']
    min_value = seq['MIN_VALUE']
    cycle = seq['CYCLE']
    comment = seq['COMMENT']

    options = []

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

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

    options << "cache: #{cache_value}" if cache_value && Integer(cache_value) != 0

    options << "min_value: #{min_value}" if min_value

    options << 'cycle: true' if cycle.to_i != 0

    options << "comment: #{comment.inspect}" if comment.present?

    statement = [
      'create_sequence',
      seq['SEQUENCE_NAME'].inspect
    ].join(' ')

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

    stream.puts "  #{statement}"
  end

  stream.puts
end