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

[Validate]