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
407 def dump_columns
408   strings = []
409   cols = columns.dup
410   cols.each do |x|
411     x.delete(:on_delete) if x[:on_delete] == :no_action
412     x.delete(:on_update) if x[:on_update] == :no_action
413   end
414   if (pkn = primary_key_name) && !@primary_key[:keep_order]
415     cols.delete_if{|x| x[:name] == pkn}
416     pk = @primary_key.dup
417     pkname = pk.delete(:name)
418     @db.serial_primary_key_options.each{|k,v| pk.delete(k) if v == pk[k]}
419     strings << "primary_key #{pkname.inspect}#{opts_inspect(pk)}"
420   end
421   cols.each do |c|
422     c = c.dup
423     name = c.delete(:name)
424     strings << if table = c.delete(:table)
425       c.delete(:type) if c[:type] == Integer || c[:type] == 'integer'
426       "foreign_key #{name.inspect}, #{table.inspect}#{opts_inspect(c)}"
427     elsif pkn == name
428       @db.serial_primary_key_options.each{|k,v| c.delete(k) if v == c[k]}
429       "primary_key #{name.inspect}#{opts_inspect(c)}"
430     else
431       type = c.delete(:type)
432       opts = opts_inspect(c)
433       case type
434       when Class
435         "#{type.name} #{name.inspect}#{opts}"
436       when :Bignum
437         "Bignum #{name.inspect}#{opts}"
438       else
439         "column #{name.inspect}, #{type.inspect}#{opts}"
440       end
441     end
442   end
443   strings.join("\n")
444 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
448 def dump_constraints
449   cs = constraints.map do |c|
450     c = c.dup
451     type = c.delete(:type)
452     case type
453     when :check
454       raise(Error, "can't dump check/constraint specified with Proc") if c[:check].is_a?(Proc)
455       name = c.delete(:name)
456       if !name and c[:check].length == 1 and c[:check].first.is_a?(Hash)
457         "check #{c[:check].first.inspect[1...-1]}"
458       else
459         "#{name ? "constraint #{name.inspect}," : 'check'} #{c[:check].map(&:inspect).join(', ')}"
460       end
461     when :foreign_key
462       c.delete(:on_delete) if c[:on_delete] == :no_action
463       c.delete(:on_update) if c[:on_update] == :no_action
464       c.delete(:deferrable) unless c[:deferrable]
465       cols = c.delete(:columns)
466       table = c.delete(:table)
467       "#{type} #{cols.inspect}, #{table.inspect}#{opts_inspect(c)}"
468     else
469       cols = c.delete(:columns)
470       "#{type} #{cols.inspect}#{opts_inspect(c)}"
471     end
472   end
473   cs.join("\n")
474 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
483 def dump_indexes(options=OPTS)
484   is = indexes.map do |c|
485     c = c.dup
486     cols = c.delete(:columns)
487     if table = options[:add_index] || options[:drop_index]
488       "#{options[:drop_index] ? 'drop' : 'add'}_index #{table.inspect}, #{cols.inspect}#{', :ignore_errors=>true' if options[:ignore_errors]}#{opts_inspect(c)}"
489     else
490       "index #{cols.inspect}#{opts_inspect(c)}"
491     end
492   end
493   is = is.reverse if options[:drop_index]
494   is.join("\n")
495 end