Module: ActiveRecord::ConnectionAdapters::SQLite3::SchemaStatements

Included in:
ActiveRecord::ConnectionAdapters::SQLite3Adapter
Defined in:
lib/active_record/connection_adapters/sqlite3/schema_statements.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#add_check_constraint(table_name, expression, **options) ⇒ Object



97
98
99
100
101
# File 'lib/active_record/connection_adapters/sqlite3/schema_statements.rb', line 97

def add_check_constraint(table_name, expression, **options)
  alter_table(table_name) do |definition|
    definition.check_constraint(expression, **options)
  end
end

#add_foreign_key(from_table, to_table, **options) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/active_record/connection_adapters/sqlite3/schema_statements.rb', line 56

def add_foreign_key(from_table, to_table, **options)
  assert_valid_deferrable(options[:deferrable])

  alter_table(from_table) do |definition|
    to_table = strip_table_name_prefix_and_suffix(to_table)
    definition.foreign_key(to_table, **options)
  end
end

#check_constraints(table_name) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/active_record/connection_adapters/sqlite3/schema_statements.rb', line 81

def check_constraints(table_name)
  table_sql = query_value("    SELECT sql\n    FROM sqlite_master\n    WHERE name = \#{quote(table_name)} AND type = 'table'\n    UNION ALL\n    SELECT sql\n    FROM sqlite_temp_master\n    WHERE name = \#{quote(table_name)} AND type = 'table'\n  SQL\n\n  table_sql.to_s.scan(/CONSTRAINT\\s+(?<name>\\w+)\\s+CHECK\\s+\\((?<expression>(:?[^()]|\\(\\g<expression>\\))+)\\)/i).map do |name, expression|\n    CheckConstraintDefinition.new(table_name, expression, name: name)\n  end\nend\n", "SCHEMA")

#create_schema_dumper(options) ⇒ Object



112
113
114
# File 'lib/active_record/connection_adapters/sqlite3/schema_statements.rb', line 112

def create_schema_dumper(options)
  SQLite3::SchemaDumper.create(self, options)
end

#indexes(table_name) ⇒ Object

Returns an array of indexes for the given table.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/active_record/connection_adapters/sqlite3/schema_statements.rb', line 8

def indexes(table_name)
  internal_exec_query("PRAGMA index_list(#{quote_table_name(table_name)})", "SCHEMA").filter_map do |row|
    # Indexes SQLite creates implicitly for internal use start with "sqlite_".
    # See https://www.sqlite.org/fileformat2.html#intschema
    next if row["name"].start_with?("sqlite_")

    index_sql = query_value("      SELECT sql\n      FROM sqlite_master\n      WHERE name = \#{quote(row['name'])} AND type = 'index'\n      UNION ALL\n      SELECT sql\n      FROM sqlite_temp_master\n      WHERE name = \#{quote(row['name'])} AND type = 'index'\n    SQL\n\n    /\\bON\\b\\s*\"?(\\w+?)\"?\\s*\\((?<expressions>.+?)\\)(?:\\s*WHERE\\b\\s*(?<where>.+))?(?:\\s*\\/\\*.*\\*\\/)?\\z/i =~ index_sql\n\n    columns = internal_exec_query(\"PRAGMA index_info(\#{quote(row['name'])})\", \"SCHEMA\").map do |col|\n      col[\"name\"]\n    end\n\n    where = where.sub(/\\s*\\/\\*.*\\*\\/\\z/, \"\") if where\n    orders = {}\n\n    if columns.any?(&:nil?) # index created with an expression\n      columns = expressions\n    else\n      # Add info on sort order for columns (only desc order is explicitly specified,\n      # asc is the default)\n      if index_sql # index_sql can be null in case of primary key indexes\n        index_sql.scan(/\"(\\w+)\" DESC/).flatten.each { |order_column|\n          orders[order_column] = :desc\n        }\n      end\n    end\n\n    IndexDefinition.new(\n      table_name,\n      row[\"name\"],\n      row[\"unique\"] != 0,\n      columns,\n      where: where,\n      orders: orders\n    )\n  end\nend\n", "SCHEMA")

#remove_check_constraint(table_name, expression = nil, if_exists: false, **options) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/active_record/connection_adapters/sqlite3/schema_statements.rb', line 103

def remove_check_constraint(table_name, expression = nil, if_exists: false, **options)
  return if if_exists && !check_constraint_exists?(table_name, **options)

  check_constraints = check_constraints(table_name)
  chk_name_to_delete = check_constraint_for!(table_name, expression: expression, **options).name
  check_constraints.delete_if { |chk| chk.name == chk_name_to_delete }
  alter_table(table_name, foreign_keys(table_name), check_constraints)
end

#remove_foreign_key(from_table, to_table = nil, **options) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/active_record/connection_adapters/sqlite3/schema_statements.rb', line 65

def remove_foreign_key(from_table, to_table = nil, **options)
  return if options.delete(:if_exists) && !foreign_key_exists?(from_table, to_table, **options.slice(:column))

  to_table ||= options[:to_table]
  options = options.except(:name, :to_table, :validate)
  fkey = foreign_key_for!(from_table, to_table: to_table, **options)

  foreign_keys = foreign_keys(from_table)
  foreign_keys.delete(fkey)
  alter_table(from_table, foreign_keys)
end

#schema_creationObject

:nodoc



116
117
118
# File 'lib/active_record/connection_adapters/sqlite3/schema_statements.rb', line 116

def schema_creation # :nodoc
  SQLite3::SchemaCreation.new(self)
end

#virtual_table_exists?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/active_record/connection_adapters/sqlite3/schema_statements.rb', line 77

def virtual_table_exists?(table_name)
  query_values(data_source_sql(table_name, type: "VIRTUAL TABLE"), "SCHEMA").any?
end