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
446 def dump_columns
447   strings = []
448   cols = columns.dup
449   cols.each do |x|
450     x.delete(:on_delete) if x[:on_delete] == :no_action
451     x.delete(:on_update) if x[:on_update] == :no_action
452   end
453   if (pkn = primary_key_name) && !@primary_key[:keep_order]
454     cols.delete_if{|x| x[:name] == pkn}
455     pk = @primary_key.dup
456     pkname = pk.delete(:name)
457     @db.serial_primary_key_options.each{|k,v| pk.delete(k) if v == pk[k]}
458     strings << "primary_key #{pkname.inspect}#{opts_inspect(pk)}"
459   end
460   cols.each do |c|
461     c = c.dup
462     name = c.delete(:name)
463     strings << if table = c.delete(:table)
464       c.delete(:type) if c[:type] == Integer || c[:type] == 'integer'
465       "foreign_key #{name.inspect}, #{table.inspect}#{opts_inspect(c)}"
466     elsif pkn == name
467       @db.serial_primary_key_options.each{|k,v| c.delete(k) if v == c[k]}
468       "primary_key #{name.inspect}#{opts_inspect(c)}"
469     else
470       type = c.delete(:type)
471       opts = opts_inspect(c)
472       case type
473       when Class
474         "#{type.name} #{name.inspect}#{opts}"
475       when :Bignum
476         "Bignum #{name.inspect}#{opts}"
477       else
478         "column #{name.inspect}, #{type.inspect}#{opts}"
479       end
480     end
481   end
482   strings.join("\n")
483 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
487 def dump_constraints
488   cs = constraints.map do |c|
489     c = c.dup
490     type = c.delete(:type)
491     case type
492     when :check
493       raise(Error, "can't dump check/constraint specified with Proc") if c[:check].is_a?(Proc)
494       name = c.delete(:name)
495       if !name and c[:check].length == 1 and c[:check].first.is_a?(Hash)
496         "check #{c[:check].first.inspect[1...-1]}"
497       else
498         "#{name ? "constraint #{name.inspect}," : 'check'} #{c[:check].map(&:inspect).join(', ')}"
499       end
500     when :foreign_key
501       c.delete(:on_delete) if c[:on_delete] == :no_action
502       c.delete(:on_update) if c[:on_update] == :no_action
503       c.delete(:deferrable) unless c[:deferrable]
504       cols = c.delete(:columns)
505       table = c.delete(:table)
506       "#{type} #{cols.inspect}, #{table.inspect}#{opts_inspect(c)}"
507     else
508       cols = c.delete(:columns)
509       "#{type} #{cols.inspect}#{opts_inspect(c)}"
510     end
511   end
512   cs.join("\n")
513 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
522 def dump_indexes(options=OPTS)
523   is = indexes.map do |c|
524     c = c.dup
525     cols = c.delete(:columns)
526     if table = options[:add_index] || options[:drop_index]
527       "#{options[:drop_index] ? 'drop' : 'add'}_index #{table.inspect}, #{cols.inspect}#{", #{IGNORE_ERRORS_KEY}true" if options[:ignore_errors]}#{opts_inspect(c)}"
528     else
529       "index #{cols.inspect}#{opts_inspect(c)}"
530     end
531   end
532   is = is.reverse if options[:drop_index]
533   is.join("\n")
534 end