Module: Sequel::Extension::PgComment::AlterTableGeneratorMethods

Includes:
Sequel::Extension::PgComment
Defined in:
lib/sequel/extensions/pg_comment/alter_table_generator_methods.rb

Overview

:nodoc: Enhancements to the standard schema modification methods in a block-form alter_table method, to support setting comments via the :comment option.

Note that not every schema modification method is enhanced in this module; some modifications are implemneted in terms of more fundamental methods, and so do not require their own method here. For example, add_foreign_key with a single column is handled by add_column, and so doesn't require its own implementation. Rest assured that all schema modification methods should accept a :comment option, and set a comment in the database. If you find one that doesn't, please file a bug.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commentsObject (readonly)

Returns the value of attribute comments.



15
16
17
# File 'lib/sequel/extensions/pg_comment/alter_table_generator_methods.rb', line 15

def comments
  @comments
end

Instance Method Details

#add_column(*args) ⇒ Object

Enhanced version of the add_column schema modification method, which supports setting a comment on the column.

Parameters:

  • [String] (Hash)

    a customizable set of options



25
26
27
28
29
30
31
# File 'lib/sequel/extensions/pg_comment/alter_table_generator_methods.rb', line 25

def add_column(*args)
	super

	if args.last.is_a?(Hash) && args.last[:comment]
		comments << SqlGenerator.create(:column, args.first, args.last[:comment])
	end
end

#add_composite_foreign_key(columns, table, opts) ⇒ Object

Enhanced version of the add_composite_foreign_key schema modification method, which supports setting a comment on the constraint.

Parameters:

  • [String] (Hash)

    a customizable set of options



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sequel/extensions/pg_comment/alter_table_generator_methods.rb', line 53

def add_composite_foreign_key(columns, table, opts)
	super
	
	if opts[:comment]
		comments << if opts[:name]
			SqlGenerator.create(:constraint, opts[:name], opts[:comment])
		else
			PrefixSqlGenerator.new(
		     :constraint,
		     "_#{columns.first}_fkey".to_sym,
		     opts[:comment]
		   )
		end
	end
end

#add_composite_primary_key(columns, opts) ⇒ Object

Enhanced version of the add_composite_primary_key schema modification method, which supports setting a comment on the index.

Parameters:

  • [String] (Hash)

    a customizable set of options



39
40
41
42
43
44
45
# File 'lib/sequel/extensions/pg_comment/alter_table_generator_methods.rb', line 39

def add_composite_primary_key(columns, opts)
	super

	if opts[:comment]
		comments << PrefixSqlGenerator.new(:index, :_pkey, opts[:comment])
	end
end

#add_constraint(name, *args, &block) ⇒ Object

Enhanced version of the add_constraint schema modification method, which supports setting a comment on the constraint.

Parameters:

  • [String] (Hash)

    a customizable set of options



95
96
97
98
99
100
101
102
103
# File 'lib/sequel/extensions/pg_comment/alter_table_generator_methods.rb', line 95

def add_constraint(name, *args, &block)
	super

	opts = args.last.is_a?(Hash) ? args.last : {}

	if opts[:comment]
		comments << SqlGenerator.create(:constraint, name, opts[:comment])
	end
end

#add_index(columns, opts = OPTS) ⇒ Object

Enhanced version of the add_index schema modification method, which supports setting a comment on the index.

Parameters:

  • [String] (Hash)

    a customizable set of options



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/sequel/extensions/pg_comment/alter_table_generator_methods.rb', line 75

def add_index(columns, opts = OPTS)
	if opts[:comment]
		comments << if opts[:name]
			SqlGenerator.create(:index, opts[:name], opts[:comment])
		else
			PrefixSqlGenerator.new(
			  :index,
			  "_#{[columns].flatten.map(&:to_s).join("_")}_index".to_sym,
			  opts[:comment]
			)
		end
	end
end

#add_unique_constraint(columns, opts = OPTS) ⇒ Object

Enhanced version of the add_unique_constraint schema modification method, which supports setting a comment on the index.

Parameters:

  • [String] (Hash)

    a customizable set of options



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/sequel/extensions/pg_comment/alter_table_generator_methods.rb', line 111

def add_unique_constraint(columns, opts = OPTS)
	super

	if opts[:comment]
		comments << if opts[:name]
			SqlGenerator.create(:index, opts[:name], opts[:comment])
		else
			PrefixSqlGenerator.new(
		     :index,
		     "_#{[columns].flatten.map(&:to_s).join("_")}_key".to_sym,
		     opts[:comment]
		   )
		end
	end
end