Skip to Content Skip to Search

The SQLite3 adapter works with the sqlite3-ruby drivers (available as gem from rubygems.org/gems/sqlite3).

Options:

  • :database - Path to the database file.

Methods
A
D
E
F
N
R
S
Included Modules

Constants

ADAPTER_NAME = "SQLite"
 
COLLATE_REGEX = /.*"(\w+)".*collate\s+"(\w+)".*/i.freeze
 
EXTENDED_TYPE_MAPS = Concurrent::Map.new
 
NATIVE_DATABASE_TYPES = { primary_key: "integer PRIMARY KEY AUTOINCREMENT NOT NULL", string: { name: "varchar" }, text: { name: "text" }, integer: { name: "integer" }, float: { name: "float" }, decimal: { name: "decimal" }, datetime: { name: "datetime" }, time: { name: "time" }, date: { name: "date" }, binary: { name: "blob" }, boolean: { name: "boolean" }, json: { name: "json" }, }
 
TYPE_MAP = Type::TypeMap.new.tap { |m| initialize_type_map(m) }
 

Class Public methods

new(...)

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 83
def initialize(...)
  super

  @memory_database = false
  case @config[:database].to_s
  when ""
    raise ArgumentError, "No database file specified. Missing argument: database"
  when ":memory:"
    @memory_database = true
  when /\Afile:/
  else
    # Otherwise we have a path relative to Rails.root
    @config[:database] = File.expand_path(@config[:database], Rails.root) if defined?(Rails.root)
    dirname = File.dirname(@config[:database])
    unless File.directory?(dirname)
      begin
        Dir.mkdir(dirname)
      rescue Errno::ENOENT => error
        if error.message.include?("No such file or directory")
          raise ActiveRecord::NoDatabaseError
        else
          raise
        end
      end
    end
  end

  @config[:strict] = ConnectionAdapters::SQLite3Adapter.strict_strings_by_default unless @config.key?(:strict)
  @connection_parameters = @config.merge(database: @config[:database].to_s, results_as_hash: true)
end

new_client(config)

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 34
def new_client(config)
  ::SQLite3::Database.new(config[:database].to_s, config)
rescue Errno::ENOENT => error
  if error.message.include?("No such file or directory")
    raise ActiveRecord::NoDatabaseError
  else
    raise
  end
end

strict_strings_by_default

Configure the SQLite3Adapter to be used in a strict strings mode. This will disable double-quoted string literals, because otherwise typos can silently go unnoticed. For example, it is possible to create an index for a non existing column. If you wish to enable this mode you can add the following line to your application.rb file:

config.active_record.sqlite3_adapter_strict_strings_by_default = true
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 57
class_attribute :strict_strings_by_default, default: false

Instance Public methods

active?()

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 177
def active?
  @raw_connection && !@raw_connection.closed?
end

add_timestamps(table_name, **options)

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 321
def add_timestamps(table_name, **options)
  options[:null] = false if options[:null].nil?

  if !options.key?(:precision)
    options[:precision] = 6
  end

  alter_table(table_name) do |definition|
    definition.column :created_at, :datetime, **options
    definition.column :updated_at, :datetime, **options
  end
end

database_exists?()

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 114
def database_exists?
  @config[:database] == ":memory:" || File.exist?(@config[:database].to_s)
end

disconnect!()

Disconnects from the database if already connected. Otherwise, this method does nothing.

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 185
def disconnect!
  super

  @raw_connection&.close rescue nil
  @raw_connection = nil
end

encoding()

Returns the current database encoding format as a string, e.g. 'UTF-8'

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 201
def encoding
  any_raw_connection.encoding.to_s
end

foreign_keys(table_name)

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 339
def foreign_keys(table_name)
  fk_info = exec_query("PRAGMA foreign_key_list(#{quote(table_name)})", "SCHEMA")
  fk_info.map do |row|
    options = {
      column: row["from"],
      primary_key: row["to"],
      on_delete: extract_foreign_key_action(row["on_delete"]),
      on_update: extract_foreign_key_action(row["on_update"])
    }
    ForeignKeyDefinition.new(table_name, row["table"], options)
  end
end

rename_table(table_name, new_name, **options)

Renames a table.

Example:

rename_table('octopuses', 'octopi')
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 252
def rename_table(table_name, new_name, **options)
  validate_table_length!(new_name) unless options[:_uses_legacy_table_name]
  schema_cache.clear_data_source_cache!(table_name.to_s)
  schema_cache.clear_data_source_cache!(new_name.to_s)
  exec_query "ALTER TABLE #{quote_table_name(table_name)} RENAME TO #{quote_table_name(new_name)}"
  rename_table_indexes(table_name, new_name)
end

requires_reloading?()

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 138
def requires_reloading?
  true
end

supports_check_constraints?()

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 146
def supports_check_constraints?
  true
end

supports_common_table_expressions?()

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 162
def supports_common_table_expressions?
  database_version >= "3.8.3"
end

supports_concurrent_connections?()

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 173
def supports_concurrent_connections?
  !@memory_database
end

supports_datetime_with_precision?()

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 154
def supports_datetime_with_precision?
  true
end

supports_ddl_transactions?()

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 118
def supports_ddl_transactions?
  true
end

supports_explain?()

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 205
def supports_explain?
  true
end

supports_expression_index?()

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 134
def supports_expression_index?
  database_version >= "3.9.0"
end

supports_foreign_keys?()

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 142
def supports_foreign_keys?
  true
end

supports_index_sort_order?()

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 192
def supports_index_sort_order?
  true
end

supports_insert_conflict_target?()

supports_insert_on_conflict?()

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 166
def supports_insert_on_conflict?
  database_version >= "3.24.0"
end

supports_insert_on_duplicate_skip?()

supports_insert_on_duplicate_update?()

supports_json?()

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 158
def supports_json?
  true
end

supports_lazy_transactions?()

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 209
def supports_lazy_transactions?
  true
end

supports_partial_index?()

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 130
def supports_partial_index?
  true
end

supports_savepoints?()

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 122
def supports_savepoints?
  true
end

supports_transaction_isolation?()

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 126
def supports_transaction_isolation?
  true
end

supports_views?()

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 150
def supports_views?
  true
end