Module: MigrationComments::ActiveRecord::ConnectionAdapters::SQLiteAdapter

Includes:
AbstractSQLiteAdapter
Defined in:
lib/migration_comments/active_record/connection_adapters/sqlite_adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AbstractSQLiteAdapter

#add_column_options!, #change_column_with_migration_comments, #column_for, #comment_sql, #comments_supported?, #inline_comments?, #retrieve_column_comments, #retrieve_table_comment, #set_column_comment, #set_table_comment

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/migration_comments/active_record/connection_adapters/sqlite_adapter.rb', line 5

def self.included(base)
  base.module_eval do
    alias_method_chain :columns, :migration_comments
    alias_method_chain :copy_table, :migration_comments
    alias_method_chain :change_column, :migration_comments
  end
end

Instance Method Details

#columns_with_migration_comments(table_name, name = nil) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/migration_comments/active_record/connection_adapters/sqlite_adapter.rb', line 34

def columns_with_migration_comments(table_name, name = nil)
  cols = columns_without_migration_comments(table_name, name)
  comments = retrieve_column_comments(table_name, *(cols.map(&:name)))
  cols.each do |col|
    col.comment = comments[col.name.to_sym] if comments.has_key?(col.name.to_sym)
  end
  cols
end

#copy_table_with_migration_comments(from, to, options = {}) ⇒ Object

:nodoc:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/migration_comments/active_record/connection_adapters/sqlite_adapter.rb', line 43

def copy_table_with_migration_comments(from, to, options = {}) #:nodoc:
  options = options.merge(:id => (!columns(from).detect{|c| c.name == 'id'}.nil? && 'id' == primary_key(from).to_s))
  unless options.has_key?(:comment)
    table_comment = retrieve_table_comment(from)
    options = options.merge(:comment => table_comment) if table_comment
  end
  create_table(to, options) do |definition|
    @definition = definition
    columns(from).each do |column|
      column_name = options[:rename] ?
        (options[:rename][column.name] ||
         options[:rename][column.name.to_sym] ||
         column.name) : column.name
      @definition.column(column_name, column.type,
        :limit => column.limit, :default => column.default,
        :precision => column.precision, :scale => column.scale,
        :null => column.null, :comment => column.comment)
    end
    @definition.primary_key(primary_key(from)) if primary_key(from)
    yield @definition if block_given?
  end

  copy_table_indexes(from, to, options[:rename] || {})
  copy_table_contents(from, to,
    @definition.columns.map {|column| column.name},
    options[:rename] || {})
end

#create_table(table_name, options = {}) {|td| ... } ⇒ Object

Yields:

  • (td)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/migration_comments/active_record/connection_adapters/sqlite_adapter.rb', line 13

def create_table(table_name, options = {})
  td = ActiveRecord::ConnectionAdapters::TableDefinition.new(self)
  td.primary_key(options[:primary_key] || ActiveRecord::Base.get_primary_key(table_name.to_s.singularize)) unless options[:id] == false
  td.comment options[:comment] if options.has_key?(:comment)
  td.base = self

  yield td if block_given?

  if options[:force] && table_exists?(table_name)
    drop_table(table_name)
  end

  create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE "
  create_sql << "#{quote_table_name(table_name)}#{td.table_comment} ("
  create_sql << td.columns.map do |column|
    column.to_sql + column.comment.to_s
  end * ", "
  create_sql << ") #{options[:options]}"
  execute create_sql
end