From 3a36cb08687aa337efca4dff12d20901d4d8029e Mon Sep 17 00:00:00 2001 From: gkemmey Date: Thu, 13 Sep 2018 16:00:51 -0400 Subject: SQLite3 adapter supports expression indexes --- .../cases/adapters/sqlite3/sqlite3_adapter_test.rb | 35 ++++++++++++++++++++++ activerecord/test/cases/schema_dumper_test.rb | 19 ++++++++---- 2 files changed, 49 insertions(+), 5 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb b/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb index d1d4d545a3..320f6efd56 100644 --- a/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb +++ b/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb @@ -345,6 +345,41 @@ module ActiveRecord end end + if ActiveRecord::Base.connection.supports_expression_index? + def test_expression_index + with_example_table do + @conn.add_index "ex", "abs(number)", name: "expression" + index = @conn.indexes("ex").find { |idx| idx.name == "expression" } + assert_equal %w{ abs(number) }, index.columns + end + end + + def test_expression_index_with_where + with_example_table do + @conn.add_index "ex", "id % 10, abs(number)", name: "expression", where: "id > 1000" + index = @conn.indexes("ex").find { |idx| idx.name == "expression" } + assert_equal ["id % 10", "abs(number)"], index.columns + assert_equal "id > 1000", index.where + end + end + + def test_complicated_expression + with_example_table do + @conn.add_index "ex", "id % 10, (CASE WHEN number > 0 THEN abs(number) END)", name: "expression" + index = @conn.indexes("ex").find { |idx| idx.name == "expression" } + assert_equal ["id % 10", "(CASE WHEN number > 0 THEN abs(number) END)"], index.columns + end + end + + def test_not_everything_an_expression + with_example_table do + @conn.add_index "ex", %w{ id abs(number) }, name: "expression" + index = @conn.indexes("ex").find { |idx| idx.name == "expression" } + assert_equal %w{ id abs(number) }.sort, index.columns.sort + end + end + end + def test_primary_key with_example_table do assert_equal "id", @conn.primary_key("ex") diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index 75b68b521c..db13f20a39 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -226,6 +226,20 @@ class SchemaDumperTest < ActiveRecord::TestCase assert_match %r{t\.float\s+"temperature"$}, output end + if ActiveRecord::Base.connection.supports_expression_index? + def test_schema_dump_expression_indices + index_definition = dump_table_schema("companies").split(/\n/).grep(/t\.index.*company_expression_index/).first.strip + + if current_adapter?(:PostgreSQLAdapter) + assert_match %r{CASE.+lower\(\(name\)::text\)}i, index_definition + elsif current_adapter?(:SQLite3Adapter) + assert_match %r{CASE.+lower\(name\)}i, index_definition + else + assert false + end + end + end + if current_adapter?(:Mysql2Adapter) def test_schema_dump_includes_length_for_mysql_binary_fields output = standard_dump @@ -278,11 +292,6 @@ class SchemaDumperTest < ActiveRecord::TestCase assert_match %r{t\.decimal\s+"decimal_array_default",\s+default: \["1.23", "3.45"\],\s+array: true}, output end - def test_schema_dump_expression_indices - index_definition = dump_table_schema("companies").split(/\n/).grep(/t\.index.*company_expression_index/).first.strip - assert_match %r{CASE.+lower\(\(name\)::text\)}i, index_definition - end - def test_schema_dump_interval_type output = dump_table_schema "postgresql_times" assert_match %r{t\.interval\s+"time_interval"$}, output -- cgit v1.2.3