From 82ae8369925e152d507486f7520558ac09f090e8 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Tue, 24 Oct 2017 19:03:37 +0530 Subject: Save index order :desc to schema.rb (sqlite). Fixes #30902 Although the sqlite adapter supports index sort orders, they weren't being written to db/schema.rb. --- activerecord/CHANGELOG.md | 7 +++++++ .../connection_adapters/sqlite3/schema_statements.rb | 11 +++++++++-- activerecord/test/cases/schema_dumper_test.rb | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index ee73004810..3e1b467633 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,10 @@ +* Fixed a bug where column orders for an index weren't written to + db/schema.rb when using the sqlite adapter. + + Fixes #30902. + + *Paul Kuruvilla* + * Remove deprecated method `#sanitize_conditions`. *Rafael Mendonça França* 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 f0d702136d..3ab9dee370 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb @@ -19,8 +19,14 @@ module ActiveRecord /\sWHERE\s+(?.+)$/i =~ index_sql - columns = exec_query("PRAGMA index_info(#{quote(row['name'])})", "SCHEMA").map do |col| - col["name"] + columns = [] + orders = {} + exec_query("PRAGMA index_xinfo(#{quote(row['name'])})", "SCHEMA").each do |col| + # xinfo also lists non-key columns, let's filter those out + next if col["key"] == 0 + + columns << col["name"] + orders[col["name"]] = :desc if col["desc"] == 1 end IndexDefinition.new( @@ -28,6 +34,7 @@ module ActiveRecord row["name"], row["unique"] != 0, columns, + orders: orders, where: where ) end diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index 799a65c61e..b8696b33e4 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -186,7 +186,7 @@ class SchemaDumperTest < ActiveRecord::TestCase assert_equal 't.index ["firm_id", "type", "rating"], name: "company_index", length: { type: 10 }', index_definition end else - assert_equal 't.index ["firm_id", "type", "rating"], name: "company_index"', index_definition + assert_equal 't.index ["firm_id", "type", "rating"], name: "company_index", order: { rating: :desc }', index_definition end end -- cgit v1.2.3