Module: ActiveRecord::Sequence::Adapter

Included in:
ConnectionAdapters::TidbAdapter
Defined in:
lib/active_record/sequence/adapter.rb

Instance Method Summary collapse

Instance Method Details

#check_sequencesObject



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

def check_sequences
  select_all(
    'SELECT * FROM information_schema.sequences ORDER BY sequence_name'
  ).to_a
end

#create_sequence(name, options = {}) ⇒ Object



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
# File 'lib/active_record/sequence/adapter.rb', line 12

def create_sequence(name, options = {})
  increment = options[:increment] || options[:step]
  name = quote_column_name(name)

  sql = ["CREATE SEQUENCE IF NOT EXISTS #{name}"]
  sql << "INCREMENT BY #{increment}" if increment
  sql << "START WITH #{options[:start]}" if options[:start]
  sql << if options[:cache]
           "CACHE #{options[:cache]}"
         else
           'NOCACHE'
         end

  sql << if options[:cycle]
           'CYCLE'
         else
           'NOCYCLE'
         end

  sql << "MIN_VALUE #{options[:min_value]}" if options[:min_value]

  sql << "COMMENT #{quote(options[:comment].to_s)}" if options[:comment]

  execute(sql.join("\n"))
end

#drop_sequence(name) ⇒ Object



38
39
40
41
42
# File 'lib/active_record/sequence/adapter.rb', line 38

def drop_sequence(name)
  name = quote_column_name(name)
  sql = "DROP SEQUENCE IF EXISTS #{name}"
  execute(sql)
end

#recreate_sequence(name, options = {}) ⇒ Object



44
45
46
47
# File 'lib/active_record/sequence/adapter.rb', line 44

def recreate_sequence(name, options = {})
  drop_sequence(name)
  create_sequence(name, options)
end