Skip to content

Commit f9b0d4e

Browse files
committed
Enable enum and partial index tests
Add a version check to enable partial indexes if >=20.2, and test with v20.2
1 parent 0bb8a93 commit f9b0d4e

File tree

8 files changed

+236
-27
lines changed

8 files changed

+236
-27
lines changed

build/teamcity-test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
set -euox pipefail
44

55
# Download CockroachDB
6-
VERSION=v20.1.0-rc.2
6+
VERSION=v20.2.0-beta.3
77
wget -qO- https://binaries.cockroachdb.com/cockroach-$VERSION.linux-amd64.tgz | tar xvz
88
readonly COCKROACH=./cockroach-$VERSION.linux-amd64/cockroach
99

lib/active_record/connection_adapters/cockroachdb_adapter.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ def supports_materialized_views?
8383
end
8484

8585
def supports_partial_index?
86-
# See cockroachdb/cockroach#9683
87-
false
86+
@crdb_version >= 202
8887
end
8988

9089
def supports_expression_index?
@@ -130,6 +129,25 @@ def max_identifier_length
130129
alias index_name_length max_identifier_length
131130
alias table_alias_length max_identifier_length
132131

132+
def initialize(connection, logger, conn_params, config)
133+
super(connection, logger, conn_params, config)
134+
crdb_version_string = query_value("SHOW crdb_version")
135+
if crdb_version_string.include? "v1."
136+
version_num = 1
137+
elsif crdb_version_string.include? "v2."
138+
version_num 2
139+
elsif crdb_version_string.include? "v19.1."
140+
version_num = 191
141+
elsif crdb_version_string.include? "v19.2."
142+
version_num = 192
143+
elsif crdb_version_string.include? "v20.1."
144+
version_num = 201
145+
else
146+
version_num = 202
147+
end
148+
@crdb_version = version_num
149+
end
150+
133151
private
134152

135153
def initialize_type_map(m = type_map)

test/cases/migration/index_test.rb

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
# frozen_string_literal: true
2+
3+
require "cases/helper"
4+
5+
module ActiveRecord
6+
module CockroachDB
7+
class Migration
8+
class IndexTest < ActiveRecord::TestCase
9+
# These tests are identical to the ones found in Rails, save for the fact
10+
# that transactions are turned off for test runs. It is necessary to disable
11+
# transactional tests in order to assert on schema changes due to how
12+
# CockroachDB handles transactions.
13+
self.use_transactional_tests = false
14+
15+
attr_reader :connection, :table_name
16+
17+
def setup
18+
super
19+
@connection = ActiveRecord::Base.connection
20+
@table_name = :testings
21+
22+
connection.create_table table_name do |t|
23+
t.column :foo, :string, limit: 100
24+
t.column :bar, :string, limit: 100
25+
26+
t.string :first_name
27+
t.string :last_name, limit: 100
28+
t.string :key, limit: 100
29+
t.boolean :administrator
30+
end
31+
end
32+
33+
teardown do
34+
connection.drop_table :testings rescue nil
35+
ActiveRecord::Base.primary_key_prefix_type = nil
36+
end
37+
38+
def test_rename_index
39+
# keep the names short to make Oracle and similar behave
40+
connection.add_index(table_name, [:foo], name: "old_idx")
41+
connection.rename_index(table_name, "old_idx", "new_idx")
42+
43+
assert_not connection.index_name_exists?(table_name, "old_idx")
44+
assert connection.index_name_exists?(table_name, "new_idx")
45+
end
46+
47+
def test_rename_index_too_long
48+
too_long_index_name = good_index_name + "x"
49+
# keep the names short to make Oracle and similar behave
50+
connection.add_index(table_name, [:foo], name: "old_idx")
51+
e = assert_raises(ArgumentError) {
52+
connection.rename_index(table_name, "old_idx", too_long_index_name)
53+
}
54+
assert_match(/too long; the limit is #{connection.allowed_index_name_length} characters/, e.message)
55+
56+
assert connection.index_name_exists?(table_name, "old_idx")
57+
end
58+
59+
def test_double_add_index
60+
connection.add_index(table_name, [:foo], name: "some_idx")
61+
assert_raises(ArgumentError) {
62+
connection.add_index(table_name, [:foo], name: "some_idx")
63+
}
64+
end
65+
66+
def test_add_index_works_with_long_index_names
67+
connection.add_index(table_name, "foo", name: good_index_name)
68+
69+
assert connection.index_name_exists?(table_name, good_index_name)
70+
connection.remove_index(table_name, name: good_index_name)
71+
end
72+
73+
def test_internal_index_with_name_matching_database_limit
74+
good_index_name = "x" * connection.index_name_length
75+
connection.add_index(table_name, "foo", name: good_index_name, internal: true)
76+
77+
assert connection.index_name_exists?(table_name, good_index_name)
78+
connection.remove_index(table_name, name: good_index_name)
79+
end
80+
81+
def test_index_symbol_names
82+
connection.add_index table_name, :foo, name: :symbol_index_name
83+
assert connection.index_exists?(table_name, :foo, name: :symbol_index_name)
84+
85+
connection.remove_index table_name, name: :symbol_index_name
86+
assert_not connection.index_exists?(table_name, :foo, name: :symbol_index_name)
87+
end
88+
89+
def test_index_exists
90+
connection.add_index :testings, :foo
91+
92+
assert connection.index_exists?(:testings, :foo)
93+
assert !connection.index_exists?(:testings, :bar)
94+
end
95+
96+
def test_index_exists_on_multiple_columns
97+
connection.add_index :testings, [:foo, :bar]
98+
99+
assert connection.index_exists?(:testings, [:foo, :bar])
100+
end
101+
102+
def test_index_exists_with_custom_name_checks_columns
103+
connection.add_index :testings, [:foo, :bar], name: "my_index"
104+
assert connection.index_exists?(:testings, [:foo, :bar], name: "my_index")
105+
assert_not connection.index_exists?(:testings, [:foo], name: "my_index")
106+
end
107+
108+
def test_unique_index_exists
109+
connection.add_index :testings, :foo, unique: true
110+
111+
assert connection.index_exists?(:testings, :foo, unique: true)
112+
end
113+
114+
def test_named_index_exists
115+
connection.add_index :testings, :foo, name: "custom_index_name"
116+
117+
assert connection.index_exists?(:testings, :foo)
118+
assert connection.index_exists?(:testings, :foo, name: "custom_index_name")
119+
assert !connection.index_exists?(:testings, :foo, name: "other_index_name")
120+
end
121+
122+
def test_remove_named_index
123+
connection.add_index :testings, :foo, name: "custom_index_name"
124+
125+
assert connection.index_exists?(:testings, :foo)
126+
connection.remove_index :testings, :foo
127+
assert !connection.index_exists?(:testings, :foo)
128+
end
129+
130+
def test_add_index_attribute_length_limit
131+
connection.add_index :testings, [:foo, :bar], length: { foo: 10, bar: nil }
132+
133+
assert connection.index_exists?(:testings, [:foo, :bar])
134+
end
135+
136+
def test_add_index
137+
connection.add_index("testings", "last_name")
138+
connection.remove_index("testings", "last_name")
139+
140+
connection.add_index("testings", ["last_name", "first_name"])
141+
connection.remove_index("testings", column: ["last_name", "first_name"])
142+
143+
# Oracle adapter cannot have specified index name larger than 30 characters
144+
# Oracle adapter is shortening index name when just column list is given
145+
unless current_adapter?(:OracleAdapter)
146+
connection.add_index("testings", ["last_name", "first_name"])
147+
connection.remove_index("testings", name: :index_testings_on_last_name_and_first_name)
148+
connection.add_index("testings", ["last_name", "first_name"])
149+
connection.remove_index("testings", "last_name_and_first_name")
150+
end
151+
connection.add_index("testings", ["last_name", "first_name"])
152+
connection.remove_index("testings", ["last_name", "first_name"])
153+
154+
connection.add_index("testings", ["last_name"], length: 10)
155+
connection.remove_index("testings", "last_name")
156+
157+
connection.add_index("testings", ["last_name"], length: { last_name: 10 })
158+
connection.remove_index("testings", ["last_name"])
159+
160+
connection.add_index("testings", ["last_name", "first_name"], length: 10)
161+
connection.remove_index("testings", ["last_name", "first_name"])
162+
163+
connection.add_index("testings", ["last_name", "first_name"], length: { last_name: 10, first_name: 20 })
164+
connection.remove_index("testings", ["last_name", "first_name"])
165+
166+
connection.add_index("testings", ["key"], name: "key_idx", unique: true)
167+
connection.remove_index("testings", name: "key_idx", unique: true)
168+
169+
connection.add_index("testings", %w(last_name first_name administrator), name: "named_admin")
170+
connection.remove_index("testings", name: "named_admin")
171+
172+
# Test support for index sort order
173+
connection.add_index("testings", ["last_name"], order: { last_name: :desc })
174+
connection.remove_index("testings", ["last_name"])
175+
connection.add_index("testings", ["last_name", "first_name"], order: { last_name: :desc })
176+
connection.remove_index("testings", ["last_name", "first_name"])
177+
connection.add_index("testings", ["last_name", "first_name"], order: { last_name: :desc, first_name: :asc })
178+
connection.remove_index("testings", ["last_name", "first_name"])
179+
connection.add_index("testings", ["last_name", "first_name"], order: :desc)
180+
connection.remove_index("testings", ["last_name", "first_name"])
181+
end
182+
183+
def test_add_partial_index
184+
connection.add_index("testings", "last_name", where: "first_name = 'john doe'")
185+
assert connection.index_exists?("testings", "last_name")
186+
187+
connection.remove_index("testings", "last_name")
188+
assert_not connection.index_exists?("testings", "last_name")
189+
end
190+
191+
private
192+
def good_index_name
193+
"x" * connection.index_name_length
194+
end
195+
196+
end
197+
end
198+
end
199+
end

test/excludes/ActiveRecord/ConnectionAdapters/PostgreSQLAdapterTest.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
exclude :test_default_sequence_name, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
33
exclude :test_bad_connection, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
44
exclude :test_pk_and_sequence_for_with_non_standard_primary_key, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
5-
exclude :test_partial_index, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
65
exclude :test_exec_insert_with_returning_disabled, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
76
exclude :test_only_warn_on_first_encounter_of_unrecognized_oid, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
87
exclude :test_index_with_opclass, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
exclude :test_double_add_index, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
2-
exclude :test_index_exists_with_custom_name_checks_columns, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
3-
exclude :test_named_index_exists, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
4-
exclude :test_rename_index_too_long, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
5-
exclude :test_add_index_works_with_long_index_names, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
6-
exclude :test_index_exists_on_multiple_columns, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
7-
exclude :test_add_index, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
8-
exclude :test_add_index_attribute_length_limit, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
9-
exclude :test_index_exists, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
10-
exclude :test_remove_named_index, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
11-
exclude :test_internal_index_with_name_matching_database_limit, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
12-
exclude :test_rename_index, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
13-
exclude :test_unique_index_exists, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
14-
exclude :test_add_partial_index, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
15-
exclude :test_index_symbol_names, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
1+
exclude :test_double_add_index, "This test runs schema changes in transactions. We run our own version with transactions disabled."
2+
exclude :test_index_exists_with_custom_name_checks_columns, "This test runs schema changes in transactions. We run our own version with transactions disabled."
3+
exclude :test_named_index_exists, "This test runs schema changes in transactions. We run our own version with transactions disabled."
4+
exclude :test_rename_index_too_long, "This test runs schema changes in transactions. We run our own version with transactions disabled."
5+
exclude :test_add_index_works_with_long_index_names, "This test runs schema changes in transactions. We run our own version with transactions disabled."
6+
exclude :test_index_exists_on_multiple_columns, "This test runs schema changes in transactions. We run our own version with transactions disabled."
7+
exclude :test_add_index, "This test runs schema changes in transactions. We run our own version with transactions disabled."
8+
exclude :test_add_index_attribute_length_limit, "This test runs schema changes in transactions. We run our own version with transactions disabled."
9+
exclude :test_index_exists, "This test runs schema changes in transactions. We run our own version with transactions disabled."
10+
exclude :test_remove_named_index, "This test runs schema changes in transactions. We run our own version with transactions disabled."
11+
exclude :test_internal_index_with_name_matching_database_limit, "This test runs schema changes in transactions. We run our own version with transactions disabled."
12+
exclude :test_rename_index, "This test runs schema changes in transactions. We run our own version with transactions disabled."
13+
exclude :test_unique_index_exists, "This test runs schema changes in transactions. We run our own version with transactions disabled."
14+
exclude :test_add_partial_index, "This test runs schema changes in transactions. We run our own version with transactions disabled."
15+
exclude :test_index_symbol_names, "This test runs schema changes in transactions. We run our own version with transactions disabled."

test/excludes/PostgresqlActiveSchemaTest.rb

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/excludes/PostgresqlEnumTest.rb

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
exclude "test_primary_key_column_type_with_serial/integer", "The last assertion in the test fails because CockroachDB's INT type translates to PostgreSQL's bigint type. See https://www.cockroachlabs.com/docs/v19.2/int.html#names-and-aliases and https://www.postgresql.org/docs/current/datatype-numeric.htmli."
22
exclude "test_schema_dump_primary_key_with_serial/integer", "The test creates a table with a serial primary key, but the schema dump assertion fails because serial columns are bigserial columns in CockroachDB. ActiveRecord schema dumps only include serial info if the primary key is a serial column. The serial info is excluded otherwise because Active Record will create primary keys as bigserial by default. See https://www.cockroachlabs.com/docs/v19.2/serial.html#generated-values-for-mode-sql_sequence, https://www.cockroachlabs.com/docs/v19.2/int.html#names-and-aliases, and https://github.com/rails/rails/pull/26266."
3+
exclude "test_primary_key_with_serial/integer_are_automatically_numbered", "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"

0 commit comments

Comments
 (0)