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



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/sequel/extensions/schema_dumper.rb', line 374

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
    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)}"
    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



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/sequel/extensions/schema_dumper.rb', line 409

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



444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/sequel/extensions/schema_dumper.rb', line 444

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