diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2017-04-16 07:45:33 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2017-04-16 07:45:33 +0900 |
commit | 1a92ae8318c3f5720907dec671d9ebb9221c0817 (patch) | |
tree | 7441adcda3a727f2070b002e21cc045f8cbc8ced /activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb | |
parent | 066e5d62d5e83830040083b5f34c9da8bed0ba4f (diff) | |
download | rails-1a92ae8318c3f5720907dec671d9ebb9221c0817.tar.gz rails-1a92ae8318c3f5720907dec671d9ebb9221c0817.tar.bz2 rails-1a92ae8318c3f5720907dec671d9ebb9221c0817.zip |
Refactor `indexes` things in connection adapters
* Use keyword arguments in `IndexDefinition` to ease to ignore unused
options and to avoid to initialize incorrect empty value.
* Place it in `SchemaStatements` for consistency.
* And tiny tweaks.
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb index 8066a05c5e..e02491edb6 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb @@ -2,6 +2,41 @@ module ActiveRecord module ConnectionAdapters module SQLite3 module SchemaStatements # :nodoc: + # Returns an array of indexes for the given table. + def indexes(table_name, name = nil) + if name + ActiveSupport::Deprecation.warn(<<-MSG.squish) + Passing name to #indexes is deprecated without replacement. + MSG + end + + exec_query("PRAGMA index_list(#{quote_table_name(table_name)})", "SCHEMA").map do |row| + index_sql = select_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 + + /\sWHERE\s+(?<where>.+)$/i =~ index_sql + + columns = exec_query("PRAGMA index_info(#{quote(row['name'])})", "SCHEMA").map do |col| + col["name"] + end + + IndexDefinition.new( + table_name, + row["name"], + row["unique"] != 0, + columns, + where: where + ) + end + end + private def schema_creation SQLite3::SchemaCreation.new(self) |