Module: SchemaPlus::ActiveRecord::ConnectionAdapters::Sqlite3Adapter

Defined in:
lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb

Overview

SchemaPlus includes an Sqlite3 implementation of the AbstractAdapter extensions.

Defined Under Namespace

Modules: AddColumnOptions

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:enddoc:


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 29

def self.included(base)
  base.class_eval do
    alias_method_chain :indexes, :schema_plus
    alias_method_chain :rename_table, :schema_plus
    alias_method_chain :tables, :schema_plus
    alias_method_chain :copy_table, :schema_plus
  end

  if ::ActiveRecord::VERSION::MAJOR.to_i < 4
    ::ActiveRecord::ConnectionAdapters::SQLiteColumn.send(:include, SQLiteColumn) unless ::ActiveRecord::ConnectionAdapters::SQLiteColumn.include?(SQLiteColumn)
  elsif defined? ::ActiveRecord::ConnectionAdapters::SQLite3Column
    ::ActiveRecord::ConnectionAdapters::SQLite3Column.send(:include, SQLiteColumn) unless ::ActiveRecord::ConnectionAdapters::SQLite3Column.include?(SQLiteColumn)
  else # in ActiveRecord::VERSION 4.2 there's no SQLite3Column
    ::ActiveRecord::ConnectionAdapters::Column.send(:include, SQLiteColumn) unless ::ActiveRecord::ConnectionAdapters::Column.include?(SQLiteColumn)
  end
end

Instance Method Details

#add_foreign_key(table_name, column_names, references_table_name, references_column_names, options = {}) ⇒ Object

Raises:

  • (NotImplementedError)

92
93
94
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 92

def add_foreign_key(table_name, column_names, references_table_name, references_column_names, options = {})
  raise NotImplementedError, "Sqlite3 does not support altering a table to add foreign key constraints (table #{table_name.inspect} column #{column_names.inspect})"
end

#copy_table_with_schema_plus(*args, &block) ⇒ Object


76
77
78
79
80
81
82
83
84
85
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 76

def copy_table_with_schema_plus(*args, &block)
  fk_override = { :auto_create => false, :auto_index => false }
  save = Hash[fk_override.keys.collect{|key| [key, SchemaPlus.config.foreign_keys.send(key)]}]
  begin
    SchemaPlus.config.foreign_keys.update_attributes(fk_override)
    copy_table_without_schema_plus(*args, &block)
  ensure
    SchemaPlus.config.foreign_keys.update_attributes(save)
  end
end

#drop_table(name, options = {}) ⇒ Object


100
101
102
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 100

def drop_table(name, options={})
  super(name, options.except(:cascade))
end

#foreign_keys(table_name, name = nil) ⇒ Object


104
105
106
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 104

def foreign_keys(table_name, name = nil)
  get_foreign_keys(table_name, name)
end

#indexes_with_schema_plus(table_name, name = nil) ⇒ Object


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 58

def indexes_with_schema_plus(table_name, name = nil)
  indexes = indexes_without_schema_plus(table_name, name)
  exec_query("SELECT name, sql FROM sqlite_master WHERE type = 'index'").map do |row|
    sql = row['sql']
    index = nil
    getindex = -> { index ||= indexes.detect { |i| i.name == row['name'] } }
    if (desc_columns = sql.scan(/['"`]?(\w+)['"`]? DESC\b/).flatten).any?
      getindex.call()
      index.orders = Hash[index.columns.map {|column| [column, desc_columns.include?(column) ? :desc : :asc]}]
    end
    if (conditions = sql.match(/\bWHERE\s+(.*)/i))
      getindex.call()
      index.conditions = conditions[1]
    end
  end
  indexes
end

#initialize(*args) ⇒ Object


46
47
48
49
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 46

def initialize(*args)
  super
  execute('PRAGMA FOREIGN_KEYS = ON')
end

#remove_foreign_key(table_name, foreign_key_name) ⇒ Object

Raises:

  • (NotImplementedError)

96
97
98
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 96

def remove_foreign_key(table_name, foreign_key_name)
  raise NotImplementedError, "Sqlite3 does not support altering a table to remove foreign key constraints (table #{table_name.inspect} constraint #{foreign_key_name.inspect})"
end

#rename_table_with_schema_plus(oldname, newname) ⇒ Object

:nodoc:


87
88
89
90
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 87

def rename_table_with_schema_plus(oldname, newname) #:nodoc:
  rename_table_without_schema_plus(oldname, newname)
  rename_indexes_and_foreign_keys(oldname, newname)
end

#reverse_foreign_keys(table_name, name = nil) ⇒ Object


108
109
110
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 108

def reverse_foreign_keys(table_name, name = nil)
  get_foreign_keys(nil, name).select{|definition| definition.references_table_name == table_name}
end

#supports_partial_indexes?Boolean

:nodoc:

Returns:

  • (Boolean)

51
52
53
54
55
56
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 51

def supports_partial_indexes? #:nodoc:
  # unfortunately with the current setup there's no easy way to
  # test multiple SQLite3 versions.  Currently travis-ci uses
  # SQLite3 version 3.7 but local development on OS X uses 3.8.
  SQLite3.libversion >= 3008000
end

#tables_with_schema_plus(*args) ⇒ Object


112
113
114
115
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 112

def tables_with_schema_plus(*args)
  # AR 4.2 explicitly looks for views or tables, but only for sqlite3.  so take away the tables.
  tables_without_schema_plus(*args) - views
end

#view_definition(view_name, name = nil) ⇒ Object


121
122
123
124
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 121

def view_definition(view_name, name = nil)
  sql = execute("SELECT sql FROM sqlite_master WHERE type='view' AND name=#{quote(view_name)}", name).collect{|row| row["sql"]}.first
  sql.sub(/^CREATE VIEW \S* AS\s+/im, '') unless sql.nil?
end

#views(name = nil) ⇒ Object


117
118
119
# File 'lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb', line 117

def views(name = nil)
  execute("SELECT name FROM sqlite_master WHERE type='view'", name).collect{|row| row["name"]}
end