Module: MigrationComments::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter

Defined in:
lib/migration_comments/active_record/connection_adapters/postgresql_adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/migration_comments/active_record/connection_adapters/postgresql_adapter.rb', line 3

def self.included(base)
  base.class_eval do
    alias_method_chain :create_table, :migration_comments
    alias_method_chain :add_column, :migration_comments
    alias_method_chain :change_column, :migration_comments
  end
end

Instance Method Details

#add_column_with_migration_comments(table_name, column_name, type, options = {}) ⇒ Object



50
51
52
53
54
55
# File 'lib/migration_comments/active_record/connection_adapters/postgresql_adapter.rb', line 50

def add_column_with_migration_comments(table_name, column_name, type, options = {})
  add_column_without_migration_comments(table_name, column_name, type, options)
  if options[:comment]
    set_column_comment(table_name, column_name, options[:comment])
  end
end

#change_column_with_migration_comments(table_name, column_name, type, options = {}) ⇒ Object



57
58
59
60
61
62
# File 'lib/migration_comments/active_record/connection_adapters/postgresql_adapter.rb', line 57

def change_column_with_migration_comments(table_name, column_name, type, options = {})
  change_column_without_migration_comments(table_name, column_name, type, options)
  if options.keys.include?(:comment)
    set_column_comment(table_name, column_name, options[:comment])
  end
end

#comment_sql(comment_definition) ⇒ Object



64
65
66
# File 'lib/migration_comments/active_record/connection_adapters/postgresql_adapter.rb', line 64

def comment_sql(comment_definition)
  "COMMENT ON #{comment_target(comment_definition)} IS #{escaped_comment(comment_definition.comment_text)}"
end

#comments_supported?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/migration_comments/active_record/connection_adapters/postgresql_adapter.rb', line 11

def comments_supported?
  true
end

#create_table_with_migration_comments(table_name, options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/migration_comments/active_record/connection_adapters/postgresql_adapter.rb', line 36

def create_table_with_migration_comments(table_name, options = {})
  local_table_definition = nil
  create_table_without_migration_comments(table_name, options) do |td|
    local_table_definition = td
    local_table_definition.base = self
    local_table_definition.comment options[:comment] if options.has_key?(:comment)
    yield td if block_given?
  end
  comments = local_table_definition.collect_comments(table_name)
  comments.each do |comment_definition|
    execute comment_definition.to_sql
  end
end

#retrieve_column_comments(table_name, *column_names) ⇒ Object



30
31
32
33
34
# File 'lib/migration_comments/active_record/connection_adapters/postgresql_adapter.rb', line 30

def retrieve_column_comments(table_name, *column_names)
  result = select_rows(column_comment_sql(table_name, *column_names))
  return {} if result.nil?
  return result.inject({}){|m, row| m[row[0].to_sym] = row[1]; m}
end

#retrieve_table_comment(table_name) ⇒ Object



25
26
27
28
# File 'lib/migration_comments/active_record/connection_adapters/postgresql_adapter.rb', line 25

def retrieve_table_comment(table_name)
  result = select_rows(table_comment_sql(table_name))
  result[0].nil? ? nil : result[0][0]
end

#set_column_comment(table_name, column_name, comment_text) ⇒ Object

Set a comment on a column



21
22
23
# File 'lib/migration_comments/active_record/connection_adapters/postgresql_adapter.rb', line 21

def set_column_comment(table_name, column_name, comment_text)
  execute CommentDefinition.new(self, table_name, column_name, comment_text).to_sql
end

#set_table_comment(table_name, comment_text) ⇒ Object

Set a comment on a table



16
17
18
# File 'lib/migration_comments/active_record/connection_adapters/postgresql_adapter.rb', line 16

def set_table_comment(table_name, comment_text)
  execute CommentDefinition.new(self, table_name, nil, comment_text).to_sql
end