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
- #add_check_constraint(table_name, expression, **options) ⇒ Object
- #add_foreign_key(from_table, to_table, **options) ⇒ Object
- #check_constraints(table_name) ⇒ Object
- #create_schema_dumper(options) ⇒ Object
-
#indexes(table_name) ⇒ Object
Returns an array of indexes for the given table.
- #remove_check_constraint(table_name, expression = nil, if_exists: false, **options) ⇒ Object
- #remove_foreign_key(from_table, to_table = nil, **options) ⇒ Object
-
#schema_creation ⇒ Object
:nodoc.
- #virtual_table_exists?(table_name) ⇒ Boolean
Instance Method Details
#add_check_constraint(table_name, expression, **options) ⇒ Object
105 106 107 108 109 |
# File 'lib/active_record/connection_adapters/sqlite3/schema_statements.rb', line 105 def add_check_constraint(table_name, expression, **) alter_table(table_name) do |definition| definition.check_constraint(expression, **) end end |
#add_foreign_key(from_table, to_table, **options) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/active_record/connection_adapters/sqlite3/schema_statements.rb', line 55 def add_foreign_key(from_table, to_table, **) assert_valid_deferrable([:deferrable]) alter_table(from_table) do |definition| to_table = strip_table_name_prefix_and_suffix(to_table) definition.foreign_key(to_table, **) end end |
#check_constraints(table_name) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/active_record/connection_adapters/sqlite3/schema_statements.rb', line 89 def check_constraints(table_name) table_sql = query_value(<<-SQL, "SCHEMA") SELECT sql FROM sqlite_master WHERE name = #{quote(table_name)} AND type = 'table' UNION ALL SELECT sql FROM sqlite_temp_master WHERE name = #{quote(table_name)} AND type = 'table' SQL table_sql.to_s.scan(/CONSTRAINT\s+(?<name>\w+)\s+CHECK\s+\((?<expression>(:?[^()]|\(\g<expression>\))+)\)/i).map do |name, expression| CheckConstraintDefinition.new(table_name, expression, name: name) end end |
#create_schema_dumper(options) ⇒ Object
120 121 122 |
# File 'lib/active_record/connection_adapters/sqlite3/schema_statements.rb', line 120 def create_schema_dumper() SQLite3::SchemaDumper.create(self, ) 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 |
# 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(<<~SQL, "SCHEMA") SELECT sql FROM sqlite_master WHERE name = #{quote(row['name'])} AND type = 'index' UNION ALL SELECT sql FROM sqlite_temp_master WHERE name = #{quote(row['name'])} AND type = 'index' SQL /\bON\b\s*"?(\w+?)"?\s*\((?<expressions>.+?)\)(?:\s*WHERE\b\s*(?<where>.+))?(?:\s*\/\*.*\*\/)?\z/i =~ index_sql columns = internal_exec_query("PRAGMA index_info(#{quote(row['name'])})", "SCHEMA").map do |col| col["name"] end orders = {} if columns.any?(&:nil?) # index created with an expression columns = expressions else # Add info on sort order for columns (only desc order is explicitly specified, # asc is the default) if index_sql # index_sql can be null in case of primary key indexes index_sql.scan(/"(\w+)" DESC/).flatten.each { |order_column| orders[order_column] = :desc } end end IndexDefinition.new( table_name, row["name"], row["unique"] != 0, columns, where: where, orders: orders ) end end |
#remove_check_constraint(table_name, expression = nil, if_exists: false, **options) ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'lib/active_record/connection_adapters/sqlite3/schema_statements.rb', line 111 def remove_check_constraint(table_name, expression = nil, if_exists: false, **) return if if_exists && !check_constraint_exists?(table_name, **) check_constraints = check_constraints(table_name) chk_name_to_delete = check_constraint_for!(table_name, expression: expression, **).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
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/active_record/connection_adapters/sqlite3/schema_statements.rb', line 64 def remove_foreign_key(from_table, to_table = nil, **) return if .delete(:if_exists) == true && !foreign_key_exists?(from_table, to_table) to_table ||= [:to_table] = .except(:name, :to_table, :validate) foreign_keys = foreign_keys(from_table) fkey = foreign_keys.detect do |fk| table = to_table || begin table = [:column].to_s.delete_suffix("_id") Base.pluralize_table_names ? table.pluralize : table end table = strip_table_name_prefix_and_suffix(table) fk_to_table = strip_table_name_prefix_and_suffix(fk.to_table) fk_to_table == table && .all? { |k, v| fk.[k].to_s == v.to_s } end || raise(ArgumentError, "Table '#{from_table}' has no foreign key for #{to_table || }") foreign_keys.delete(fkey) alter_table(from_table, foreign_keys) end |
#schema_creation ⇒ Object
:nodoc
124 125 126 |
# File 'lib/active_record/connection_adapters/sqlite3/schema_statements.rb', line 124 def schema_creation # :nodoc SQLite3::SchemaCreation.new(self) end |
#virtual_table_exists?(table_name) ⇒ Boolean
85 86 87 |
# File 'lib/active_record/connection_adapters/sqlite3/schema_statements.rb', line 85 def virtual_table_exists?(table_name) query_values(data_source_sql(table_name, type: "VIRTUAL TABLE"), "SCHEMA").any? end |