Class Sequel::Schema::Generator
In: lib/sequel/extensions/schema_dumper.rb
Parent: Object

Methods

Public Instance methods

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

[Source]

     # File lib/sequel/extensions/schema_dumper.rb, line 387
387:       def dump_columns
388:         strings = []
389:         cols = columns.dup
390:         cols.each do |x|
391:           x.delete(:on_delete) if x[:on_delete] == :no_action
392:           x.delete(:on_update) if x[:on_update] == :no_action
393:         end
394:         if (pkn = primary_key_name) && !@primary_key[:keep_order]
395:           cols.delete_if{|x| x[:name] == pkn}
396:           pk = @primary_key.dup
397:           pkname = pk.delete(:name)
398:           @db.serial_primary_key_options.each{|k,v| pk.delete(k) if v == pk[k]}
399:           strings << "primary_key #{pkname.inspect}#{opts_inspect(pk)}"
400:         end
401:         cols.each do |c|
402:           c = c.dup
403:           name = c.delete(:name)
404:           strings << if table = c.delete(:table)
405:             c.delete(:type) if c[:type] == Integer || c[:type] == 'integer'
406:             "foreign_key #{name.inspect}, #{table.inspect}#{opts_inspect(c)}"
407:           elsif pkn == name
408:             @db.serial_primary_key_options.each{|k,v| c.delete(k) if v == c[k]}
409:             "primary_key #{name.inspect}#{opts_inspect(c)}"
410:           else
411:             type = c.delete(:type)
412:             opts = opts_inspect(c)
413:             case type
414:             when Class
415:               "#{type.name} #{name.inspect}#{opts}"
416:             when :Bignum
417:               "Bignum #{name.inspect}#{opts}"
418:             else
419:               "column #{name.inspect}, #{type.inspect}#{opts}"
420:             end
421:           end
422:         end
423:         strings.join("\n")
424:       end

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

[Source]

     # File lib/sequel/extensions/schema_dumper.rb, line 428
428:       def dump_constraints
429:         cs = constraints.map do |c|
430:           c = c.dup
431:           type = c.delete(:type)
432:           case type
433:           when :check
434:             raise(Error, "can't dump check/constraint specified with Proc") if c[:check].is_a?(Proc)
435:             name = c.delete(:name)
436:             if !name and c[:check].length == 1 and c[:check].first.is_a?(Hash)
437:               "check #{c[:check].first.inspect[1...-1]}"
438:             else
439:               "#{name ? "constraint #{name.inspect}," : 'check'} #{c[:check].map(&:inspect).join(', ')}"
440:             end
441:           when :foreign_key
442:             c.delete(:on_delete) if c[:on_delete] == :no_action
443:             c.delete(:on_update) if c[:on_update] == :no_action
444:             c.delete(:deferrable) unless c[:deferrable]
445:             cols = c.delete(:columns)
446:             table = c.delete(:table)
447:             "#{type} #{cols.inspect}, #{table.inspect}#{opts_inspect(c)}"
448:           else
449:             cols = c.delete(:columns)
450:             "#{type} #{cols.inspect}#{opts_inspect(c)}"
451:           end
452:         end
453:         cs.join("\n")
454:       end

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

[Source]

     # File lib/sequel/extensions/schema_dumper.rb, line 463
463:       def dump_indexes(options=OPTS)
464:         is = indexes.map do |c|
465:           c = c.dup
466:           cols = c.delete(:columns)
467:           if table = options[:add_index] || options[:drop_index]
468:             "#{options[:drop_index] ? 'drop' : 'add'}_index #{table.inspect}, #{cols.inspect}#{', :ignore_errors=>true' if options[:ignore_errors]}#{opts_inspect(c)}"
469:           else
470:             "index #{cols.inspect}#{opts_inspect(c)}"
471:           end
472:         end
473:         is = is.reverse if options[:drop_index]
474:         is.join("\n")
475:       end

[Validate]