class Sequel::Schema::Generator

  1. lib/sequel/extensions/schema_dumper.rb
Parent: Schema

Methods

Public Instance

  1. dump_columns
  2. dump_constraints
  3. dump_indexes

Public Instance methods

dump_columns ()

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

[show source]
# File lib/sequel/extensions/schema_dumper.rb, line 366
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_constraints ()

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

[show source]
# File lib/sequel/extensions/schema_dumper.rb, line 401
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{|x| x.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)

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

[show source]
# File lib/sequel/extensions/schema_dumper.rb, line 436
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