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

[Validate]