class PGTrunk::Operations::Sequences::ChangeSequence
@private
Constants
- INF
Public Instance Methods
invert()
click to toggle source
# File lib/pg_trunk/operations/sequences/change_sequence.rb, line 73 def invert irreversible!("if_exists: true") if if_exists undefined = inversion.select { |_, v| v.nil? }.keys.join(", ").presence raise IrreversibleMigration.new(self, nil, <<~MSG.squish) if undefined Undefined values to revert #{undefined}. MSG self.class.new(name: name, **inversion) if inversion.any? end
owned_by(table, column, from: nil)
click to toggle source
# File lib/pg_trunk/operations/sequences/change_sequence.rb, line 63 def owned_by(table, column, from: nil) self.table = table self.column = column self.from_table, self.from_column = Array(from) end
to_sql(_version)
click to toggle source
# File lib/pg_trunk/operations/sequences/change_sequence.rb, line 69 def to_sql(_version) [*alter_sequence, *update_comment].join(" ") end
Private Instance Methods
alter_sequence()
click to toggle source
# File lib/pg_trunk/operations/sequences/change_sequence.rb, line 98 def alter_sequence return if changes.except(:comment).blank? sql = "ALTER SEQUENCE" sql << " IF EXISTS" if if_exists sql << " #{name.to_sql}" sql << " AS #{type}" if type.present? sql << " INCREMENT BY #{increment_by}" if increment_by.present? sql << " MINVALUE #{min_value}" if min_value&.>(-INF) sql << " NO MINVALUE" if min_value&.<=(-INF) sql << " MAXVALUE #{max_value}" if max_value&.<(INF) sql << " NO MAXVALUE" if max_value&.>=(INF) sql << " START WITH #{start_with}" if start_with.present? sql << " CACHE #{cache}" if cache.present? sql << " OWNED BY #{table}.#{column}" if table.present? && column.present? sql << " OWNED BY NONE" if table == "" || column == "" sql << " CYCLE" if cycle sql << " NO CYCLE" if cycle == false sql << ";" end
changes()
click to toggle source
# File lib/pg_trunk/operations/sequences/change_sequence.rb, line 87 def changes @changes ||= attributes.symbolize_keys.except(:name, :if_exists).compact end
inversion()
click to toggle source
# File lib/pg_trunk/operations/sequences/change_sequence.rb, line 91 def inversion @inversion ||= changes.each_with_object({}) do |(key, val), obj| obj[key] = send(:"from_#{key}") obj[key] = !val if [true, false].include?(val) end end
update_comment()
click to toggle source
# File lib/pg_trunk/operations/sequences/change_sequence.rb, line 119 def update_comment return unless comment return <<~SQL.squish unless if_exists COMMENT ON SEQUENCE #{name.to_sql} IS $comment$#{comment}$comment$; SQL # change the comment conditionally <<~SQL.squish DO $$ BEGIN IF EXISTS ( SELECT FROM pg_sequence s JOIN pg_class c ON c.oid = s.seqrelid WHERE c.relname = #{name.quoted} AND c.relnamespace = #{name.namespace} ) THEN COMMENT ON SEQUENCE #{name.to_sql} IS $comment$#{comment}$comment$; END IF; END $$; SQL end