Class: Sequel::Schema::Generator
- Defined in:
- lib/sequel/extensions/schema_dumper.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#dump_columns ⇒ Object
Dump this generator’s columns to a string that could be evaled inside another instance to represent the same columns.
-
#dump_constraints ⇒ Object
Dump this generator’s constraints to a string that could be evaled inside another instance to represent the same constraints.
-
#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.
Instance Method Details
#dump_columns ⇒ Object
Dump this generator’s columns to a string that could be evaled inside another instance to represent the same columns
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 417 418 419 420 421 |
# File 'lib/sequel/extensions/schema_dumper.rb', line 384 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..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..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) case type when Class "#{type.name} #{name.inspect}#{opts}" when :Bignum "Bignum #{name.inspect}#{opts}" else "column #{name.inspect}, #{type.inspect}#{opts}" end end end strings.join("\n") end |
#dump_constraints ⇒ Object
Dump this generator’s constraints to a string that could be evaled inside another instance to represent the same constraints
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/sequel/extensions/schema_dumper.rb', line 425 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
460 461 462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/sequel/extensions/schema_dumper.rb', line 460 def dump_indexes(=OPTS) is = indexes.map do |c| c = c.dup cols = c.delete(:columns) if table = [:add_index] || [:drop_index] "#{[:drop_index] ? 'drop' : 'add'}_index #{table.inspect}, #{cols.inspect}#{', :ignore_errors=>true' if [:ignore_errors]}#{opts_inspect(c)}" else "index #{cols.inspect}#{opts_inspect(c)}" end end is = is.reverse if [:drop_index] is.join("\n") end |