Module: MigrationComments::ActiveRecord::ConnectionAdapters::AbstractSQLiteAdapter

Included in:
SQLite3Adapter, SQLiteAdapter
Defined in:
lib/migration_comments/active_record/connection_adapters/abstract_sqlite_adapter.rb

Instance Method Summary collapse

Instance Method Details

#add_column_options!(sql, options) ⇒ Object



69
70
71
72
73
74
# File 'lib/migration_comments/active_record/connection_adapters/abstract_sqlite_adapter.rb', line 69

def add_column_options!(sql, options)
  super(sql, options)
  if options.keys.include?(:comment)
    sql << CommentDefinition.new(self, nil, nil, options[:comment]).to_sql
  end
end

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

:nodoc:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/migration_comments/active_record/connection_adapters/abstract_sqlite_adapter.rb', line 40

def change_column_with_migration_comments(table_name, column_name, type, options = {}) #:nodoc:
  adapter = self
  alter_table(table_name) do |definition|
    include_default = options_include_default?(options)
    definition[column_name].instance_eval do
      self.type    = type
      self.limit   = options[:limit] if options.include?(:limit)
      self.default = options[:default] if include_default
      self.null    = options[:null] if options.include?(:null)
      self.precision = options[:precision] if options.include?(:precision)
      self.scale   = options[:scale] if options.include?(:scale)
      self.comment = CommentDefinition.new(adapter, table_name, column_name, options[:comment]) if options.include?(:comment)
    end
  end
end

#column_for(table_name, column_name) ⇒ Object



56
57
58
# File 'lib/migration_comments/active_record/connection_adapters/abstract_sqlite_adapter.rb', line 56

def column_for(table_name, column_name)
  columns(table_name).detect{|col| col.name == column_name.to_s}
end

#comment_sql(comment_definition) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/migration_comments/active_record/connection_adapters/abstract_sqlite_adapter.rb', line 60

def comment_sql(comment_definition)
  if comment_definition.nil? || comment_definition.comment_text.blank?
    ""
  else
    " /*#{escaped_comment(comment_definition.comment_text)}*/"
  end

end

#comments_supported?Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/migration_comments/active_record/connection_adapters/abstract_sqlite_adapter.rb', line 3

def comments_supported?
  true
end

#inline_comments?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/migration_comments/active_record/connection_adapters/abstract_sqlite_adapter.rb', line 7

def inline_comments?
  true
end

#retrieve_column_comments(table_name, *column_names) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/migration_comments/active_record/connection_adapters/abstract_sqlite_adapter.rb', line 29

def retrieve_column_comments(table_name, *column_names)
  if column_names.empty?
    return columns(table_name).inject({}) { |m, v| m[v.name.to_sym] = v.comment if v.comment.present?; m }
  end
  result = select_rows(lookup_comment_sql(table_name))
  result[0][0] =~ /^CREATE (?:TEMPORARY )?TABLE "\w*" [^\(]*(?:\/\*.*\*\/ )?\((.*)\)[^\)]*$/
  col_defs = $1
  comment_matches = col_defs.scan(/"([^",]+)"[^,]*\/\*(.+?)\*\//)
  comment_matches.inject({}){|m, row| m[row.first.to_sym] = row.last; m}
end

#retrieve_table_comment(table_name) ⇒ Object



22
23
24
25
26
27
# File 'lib/migration_comments/active_record/connection_adapters/abstract_sqlite_adapter.rb', line 22

def retrieve_table_comment(table_name)
  result = select_rows(lookup_comment_sql(table_name))
  if result[0][0] =~ /CREATE (?:TEMPORARY )?TABLE #{quote_table_name table_name} [^\(]*\/\*(.*)\*\/ \(/
    $1
  end
end

#set_column_comment(table_name, column_name, comment_text) ⇒ Object



15
16
17
18
19
20
# File 'lib/migration_comments/active_record/connection_adapters/abstract_sqlite_adapter.rb', line 15

def set_column_comment(table_name, column_name, comment_text)
  sql_type = primary_key(table_name) == column_name.to_s ?
      :primary_key :
      column_for(table_name, column_name).sql_type
  change_column table_name, column_name, sql_type, :comment => comment_text
end

#set_table_comment(table_name, comment_text) ⇒ Object



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

def set_table_comment(table_name, comment_text)
  alter_table(table_name, :comment => comment_text)
end