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 377
377:       def dump_columns
378:         strings = []
379:         cols = columns.dup
380:         cols.each do |x|
381:           x.delete(:on_delete) if x[:on_delete] == :no_action
382:           x.delete(:on_update) if x[:on_update] == :no_action
383:         end
384:         if pkn = primary_key_name
385:           cols.delete_if{|x| x[:name] == pkn}
386:           pk = @primary_key.dup
387:           pkname = pk.delete(:name)
388:           @db.serial_primary_key_options.each{|k,v| pk.delete(k) if v == pk[k]}
389:           strings << "primary_key #{pkname.inspect}#{opts_inspect(pk)}"
390:         end
391:         cols.each do |c|
392:           c = c.dup
393:           name = c.delete(:name)
394:           strings << if table = c.delete(:table)
395:             c.delete(:type) if c[:type] == Integer || c[:type] == 'integer'
396:             "foreign_key #{name.inspect}, #{table.inspect}#{opts_inspect(c)}"
397:           else
398:             type = c.delete(:type)
399:             opts = opts_inspect(c)
400:             if type.is_a?(Class)
401:               "#{type.name} #{name.inspect}#{opts}"
402:             else
403:               "column #{name.inspect}, #{type.inspect}#{opts}"
404:             end
405:           end
406:         end
407:         strings.join("\n")
408:       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 412
412:       def dump_constraints
413:         cs = constraints.map do |c|
414:           c = c.dup
415:           type = c.delete(:type)
416:           case type
417:           when :check
418:             raise(Error, "can't dump check/constraint specified with Proc") if c[:check].is_a?(Proc)
419:             name = c.delete(:name)
420:             if !name and c[:check].length == 1 and c[:check].first.is_a?(Hash)
421:               "check #{c[:check].first.inspect[1...-1]}"
422:             else
423:               "#{name ? "constraint #{name.inspect}," : 'check'} #{c[:check].map{|x| x.inspect}.join(', ')}"
424:             end
425:           when :foreign_key
426:             c.delete(:on_delete) if c[:on_delete] == :no_action
427:             c.delete(:on_update) if c[:on_update] == :no_action
428:             c.delete(:deferrable) unless c[:deferrable]
429:             cols = c.delete(:columns)
430:             table = c.delete(:table)
431:             "#{type} #{cols.inspect}, #{table.inspect}#{opts_inspect(c)}"
432:           else
433:             cols = c.delete(:columns)
434:             "#{type} #{cols.inspect}#{opts_inspect(c)}"
435:           end
436:         end
437:         cs.join("\n")
438:       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 447
447:       def dump_indexes(options=OPTS)
448:         is = indexes.map do |c|
449:           c = c.dup
450:           cols = c.delete(:columns)
451:           if table = options[:add_index] || options[:drop_index]
452:             "#{options[:drop_index] ? 'drop' : 'add'}_index #{table.inspect}, #{cols.inspect}#{', :ignore_errors=>true' if options[:ignore_errors]}#{opts_inspect(c)}"
453:           else
454:             "index #{cols.inspect}#{opts_inspect(c)}"
455:           end
456:         end
457:         is = is.reverse if options[:drop_index]
458:         is.join("\n")
459:       end

[Validate]