class Sequel::Schema::CreateTableGenerator

  1. lib/sequel/extensions/schema_dumper.rb
Superclass: Object

Methods

Public Instance

  1. dump_columns
  2. dump_constraints
  3. dump_indexes

Public Instance methods

dump_columns()

Dump this generator’s columns to a string that could be evaled inside another instance to represent the same columns

[show source]
    # File lib/sequel/extensions/schema_dumper.rb
442 def dump_columns
443   strings = []
444   cols = columns.dup
445   cols.each do |x|
446     x.delete(:on_delete) if x[:on_delete] == :no_action
447     x.delete(:on_update) if x[:on_update] == :no_action
448   end
449   if (pkn = primary_key_name) && !@primary_key[:keep_order]
450     cols.delete_if{|x| x[:name] == pkn}
451     pk = @primary_key.dup
452     pkname = pk.delete(:name)
453     @db.serial_primary_key_options.each{|k,v| pk.delete(k) if v == pk[k]}
454     strings << "primary_key #{pkname.inspect}#{opts_inspect(pk)}"
455   end
456   cols.each do |c|
457     c = c.dup
458     name = c.delete(:name)
459     strings << if table = c.delete(:table)
460       c.delete(:type) if c[:type] == Integer || c[:type] == 'integer'
461       "foreign_key #{name.inspect}, #{table.inspect}#{opts_inspect(c)}"
462     elsif pkn == name
463       @db.serial_primary_key_options.each{|k,v| c.delete(k) if v == c[k]}
464       "primary_key #{name.inspect}#{opts_inspect(c)}"
465     else
466       type = c.delete(:type)
467       opts = opts_inspect(c)
468       case type
469       when Class
470         "#{type.name} #{name.inspect}#{opts}"
471       when :Bignum
472         "Bignum #{name.inspect}#{opts}"
473       else
474         "column #{name.inspect}, #{type.inspect}#{opts}"
475       end
476     end
477   end
478   strings.join("\n")
479 end
dump_constraints()

Dump this generator’s constraints to a string that could be evaled inside another instance to represent the same constraints

[show source]
    # File lib/sequel/extensions/schema_dumper.rb
483 def dump_constraints
484   cs = constraints.map do |c|
485     c = c.dup
486     type = c.delete(:type)
487     case type
488     when :check
489       raise(Error, "can't dump check/constraint specified with Proc") if c[:check].is_a?(Proc)
490       name = c.delete(:name)
491       if !name and c[:check].length == 1 and c[:check].first.is_a?(Hash)
492         "check #{c[:check].first.inspect[1...-1]}"
493       else
494         "#{name ? "constraint #{name.inspect}," : 'check'} #{c[:check].map(&:inspect).join(', ')}"
495       end
496     when :foreign_key
497       c.delete(:on_delete) if c[:on_delete] == :no_action
498       c.delete(:on_update) if c[:on_update] == :no_action
499       c.delete(:deferrable) unless c[:deferrable]
500       cols = c.delete(:columns)
501       table = c.delete(:table)
502       "#{type} #{cols.inspect}, #{table.inspect}#{opts_inspect(c)}"
503     else
504       cols = c.delete(:columns)
505       "#{type} #{cols.inspect}#{opts_inspect(c)}"
506     end
507   end
508   cs.join("\n")
509 end
dump_indexes(options=OPTS)

Dump this generator’s indexes to a string that could be evaled inside another instance to represent the same indexes. Options:

:add_index

Use add_index instead of index, so the methods can be called outside of a generator but inside a migration. The value of this option should be the table name to use.

:drop_index

Same as add_index, but create drop_index statements.

:ignore_errors

Add the ignore_errors option to the outputted indexes

[show source]
    # File lib/sequel/extensions/schema_dumper.rb
518 def dump_indexes(options=OPTS)
519   is = indexes.map do |c|
520     c = c.dup
521     cols = c.delete(:columns)
522     if table = options[:add_index] || options[:drop_index]
523       "#{options[:drop_index] ? 'drop' : 'add'}_index #{table.inspect}, #{cols.inspect}#{", #{IGNORE_ERRORS_KEY}true" if options[:ignore_errors]}#{opts_inspect(c)}"
524     else
525       "index #{cols.inspect}#{opts_inspect(c)}"
526     end
527   end
528   is = is.reverse if options[:drop_index]
529   is.join("\n")
530 end