Class: Sequel::Schema::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel/extensions/schema_dumper.rb

Direct Known Subclasses

Postgres::CreateTableGenerator

Instance Method Summary collapse

Instance Method Details

#dump_columnsObject

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



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/sequel/extensions/schema_dumper.rb', line 382

def dump_columns
  strings = []
  cols = columns.dup
  cols.each do |x|
    x.delete(:on_delete) if x[:on_delete] == :no_action
    x.delete(:on_update) if x[:on_update] == :no_action
  end
  if (pkn = primary_key_name) && !@primary_key[:keep_order]
    cols.delete_if{|x| x[:name] == pkn}
    pk = @primary_key.dup
    pkname = pk.delete(:name)
    @db.serial_primary_key_options.each{|k,v| pk.delete(k) if v == pk[k]}
    strings << "primary_key #{pkname.inspect}#{opts_inspect(pk)}"
  end
  cols.each do |c|
    c = c.dup
    name = c.delete(:name)
    strings << if table = c.delete(:table)
      c.delete(:type) if c[:type] == Integer || c[:type] == 'integer'
      "foreign_key #{name.inspect}, #{table.inspect}#{opts_inspect(c)}"
    elsif pkn == name
      @db.serial_primary_key_options.each{|k,v| c.delete(k) if v == c[k]}
      "primary_key #{name.inspect}#{opts_inspect(c)}"
    else
      type = c.delete(:type)
      opts = opts_inspect(c)
      if type.is_a?(Class)
        "#{type.name} #{name.inspect}#{opts}"
      else
        "column #{name.inspect}, #{type.inspect}#{opts}"
      end
    end
  end
  strings.join("\n")
end

#dump_constraintsObject

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



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/sequel/extensions/schema_dumper.rb', line 420

def dump_constraints
  cs = constraints.map do |c|
    c = c.dup
    type = c.delete(:type)
    case type
    when :check
      raise(Error, "can't dump check/constraint specified with Proc") if c[:check].is_a?(Proc)
      name = c.delete(:name)
      if !name and c[:check].length == 1 and c[:check].first.is_a?(Hash)
        "check #{c[:check].first.inspect[1...-1]}"
      else
        "#{name ? "constraint #{name.inspect}," : 'check'} #{c[:check].map(&:inspect).join(', ')}"
      end
    when :foreign_key
      c.delete(:on_delete) if c[:on_delete] == :no_action
      c.delete(:on_update) if c[:on_update] == :no_action
      c.delete(:deferrable) unless c[:deferrable]
      cols = c.delete(:columns)
      table = c.delete(:table)
      "#{type} #{cols.inspect}, #{table.inspect}#{opts_inspect(c)}"
    else
      cols = c.delete(:columns)
      "#{type} #{cols.inspect}#{opts_inspect(c)}"
    end
  end
  cs.join("\n")
end

#dump_indexes(options = OPTS) ⇒ Object

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



455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/sequel/extensions/schema_dumper.rb', line 455

def dump_indexes(options=OPTS)
  is = indexes.map do |c|
    c = c.dup
    cols = c.delete(:columns)
    if table = options[:add_index] || options[:drop_index]
      "#{options[:drop_index] ? 'drop' : 'add'}_index #{table.inspect}, #{cols.inspect}#{', :ignore_errors=>true' if options[:ignore_errors]}#{opts_inspect(c)}"
    else
      "index #{cols.inspect}#{opts_inspect(c)}"
    end
  end
  is = is.reverse if options[:drop_index]
  is.join("\n")
end