activerecord-sqlserver-adapter 6.0.0.rc1 → 6.0.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +29 -0
- data/CHANGELOG.md +20 -0
- data/Gemfile +11 -5
- data/Guardfile +9 -8
- data/Rakefile +12 -16
- data/VERSION +1 -1
- data/activerecord-sqlserver-adapter.gemspec +3 -3
- data/lib/active_record/connection_adapters/sqlserver/core_ext/active_record.rb +0 -4
- data/lib/active_record/connection_adapters/sqlserver/core_ext/attribute_methods.rb +1 -4
- data/lib/active_record/connection_adapters/sqlserver/core_ext/calculations.rb +3 -4
- data/lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb +1 -3
- data/lib/active_record/connection_adapters/sqlserver/core_ext/finder_methods.rb +2 -3
- data/lib/active_record/connection_adapters/sqlserver/core_ext/query_methods.rb +2 -3
- data/lib/active_record/connection_adapters/sqlserver/database_statements.rb +35 -32
- data/lib/active_record/connection_adapters/sqlserver/database_tasks.rb +7 -12
- data/lib/active_record/connection_adapters/sqlserver/errors.rb +0 -3
- data/lib/active_record/connection_adapters/sqlserver/quoting.rb +8 -8
- data/lib/active_record/connection_adapters/sqlserver/schema_creation.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/schema_dumper.rb +7 -7
- data/lib/active_record/connection_adapters/sqlserver/schema_statements.rb +106 -103
- data/lib/active_record/connection_adapters/sqlserver/showplan.rb +6 -8
- data/lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb +2 -2
- data/lib/active_record/connection_adapters/sqlserver/showplan/printer_xml.rb +1 -1
- data/lib/active_record/connection_adapters/sqlserver/sql_type_metadata.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/table_definition.rb +1 -4
- data/lib/active_record/connection_adapters/sqlserver/transaction.rb +4 -8
- data/lib/active_record/connection_adapters/sqlserver/type.rb +35 -35
- data/lib/active_record/connection_adapters/sqlserver/type/big_integer.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/type/binary.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/type/boolean.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/type/char.rb +2 -2
- data/lib/active_record/connection_adapters/sqlserver/type/data.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/type/date.rb +2 -3
- data/lib/active_record/connection_adapters/sqlserver/type/datetime.rb +2 -3
- data/lib/active_record/connection_adapters/sqlserver/type/datetime2.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/type/datetimeoffset.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/type/decimal.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/type/float.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/type/integer.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/type/json.rb +0 -1
- data/lib/active_record/connection_adapters/sqlserver/type/money.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/type/real.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/type/small_integer.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/type/small_money.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/type/smalldatetime.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/type/string.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/type/text.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/type/time.rb +2 -3
- data/lib/active_record/connection_adapters/sqlserver/type/time_value_fractional.rb +6 -9
- data/lib/active_record/connection_adapters/sqlserver/type/timestamp.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/type/tiny_integer.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/type/unicode_char.rb +1 -3
- data/lib/active_record/connection_adapters/sqlserver/type/unicode_string.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/type/unicode_text.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/type/unicode_varchar.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/type/unicode_varchar_max.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/type/uuid.rb +1 -2
- data/lib/active_record/connection_adapters/sqlserver/type/varbinary.rb +1 -3
- data/lib/active_record/connection_adapters/sqlserver/type/varbinary_max.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/type/varchar.rb +1 -3
- data/lib/active_record/connection_adapters/sqlserver/type/varchar_max.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver/utils.rb +8 -11
- data/lib/active_record/connection_adapters/sqlserver/version.rb +0 -2
- data/lib/active_record/connection_adapters/sqlserver_adapter.rb +85 -83
- data/lib/active_record/connection_adapters/sqlserver_column.rb +0 -2
- data/lib/active_record/sqlserver_base.rb +1 -1
- data/lib/active_record/tasks/sqlserver_database_tasks.rb +26 -32
- data/lib/activerecord-sqlserver-adapter.rb +1 -1
- data/lib/arel/visitors/sqlserver.rb +18 -14
- data/lib/arel_sqlserver.rb +2 -2
- data/test/cases/adapter_test_sqlserver.rb +161 -182
- data/test/cases/change_column_null_test_sqlserver.rb +12 -12
- data/test/cases/coerced_tests.rb +88 -270
- data/test/cases/column_test_sqlserver.rb +281 -283
- data/test/cases/connection_test_sqlserver.rb +15 -20
- data/test/cases/execute_procedure_test_sqlserver.rb +18 -20
- data/test/cases/fetch_test_sqlserver.rb +14 -22
- data/test/cases/fully_qualified_identifier_test_sqlserver.rb +12 -18
- data/test/cases/helper_sqlserver.rb +13 -15
- data/test/cases/in_clause_test_sqlserver.rb +9 -9
- data/test/cases/index_test_sqlserver.rb +13 -15
- data/test/cases/json_test_sqlserver.rb +23 -25
- data/test/cases/migration_test_sqlserver.rb +22 -28
- data/test/cases/order_test_sqlserver.rb +51 -54
- data/test/cases/pessimistic_locking_test_sqlserver.rb +25 -33
- data/test/cases/rake_test_sqlserver.rb +31 -45
- data/test/cases/schema_dumper_test_sqlserver.rb +104 -108
- data/test/cases/schema_test_sqlserver.rb +18 -26
- data/test/cases/scratchpad_test_sqlserver.rb +2 -4
- data/test/cases/showplan_test_sqlserver.rb +24 -33
- data/test/cases/specific_schema_test_sqlserver.rb +66 -65
- data/test/cases/transaction_test_sqlserver.rb +16 -19
- data/test/cases/trigger_test_sqlserver.rb +12 -12
- data/test/cases/utils_test_sqlserver.rb +68 -70
- data/test/cases/uuid_test_sqlserver.rb +11 -13
- data/test/debug.rb +6 -6
- data/test/migrations/create_clients_and_change_column_null.rb +1 -1
- data/test/migrations/transaction_table/1_table_will_never_be_created.rb +2 -4
- data/test/models/sqlserver/booking.rb +1 -1
- data/test/models/sqlserver/customers_view.rb +1 -1
- data/test/models/sqlserver/dollar_table_name.rb +1 -1
- data/test/models/sqlserver/edge_schema.rb +1 -3
- data/test/models/sqlserver/fk_has_fk.rb +1 -1
- data/test/models/sqlserver/fk_has_pk.rb +1 -1
- data/test/models/sqlserver/natural_pk_data.rb +2 -2
- data/test/models/sqlserver/natural_pk_int_data.rb +1 -1
- data/test/models/sqlserver/no_pk_data.rb +1 -1
- data/test/models/sqlserver/object_default.rb +1 -1
- data/test/models/sqlserver/quoted_table.rb +2 -2
- data/test/models/sqlserver/quoted_view_1.rb +1 -1
- data/test/models/sqlserver/quoted_view_2.rb +1 -1
- data/test/models/sqlserver/sst_memory.rb +1 -1
- data/test/models/sqlserver/string_default.rb +1 -1
- data/test/models/sqlserver/string_defaults_big_view.rb +1 -1
- data/test/models/sqlserver/string_defaults_view.rb +1 -1
- data/test/models/sqlserver/tinyint_pk.rb +1 -1
- data/test/models/sqlserver/trigger.rb +2 -2
- data/test/models/sqlserver/trigger_history.rb +1 -1
- data/test/models/sqlserver/upper.rb +1 -1
- data/test/models/sqlserver/uppered.rb +1 -1
- data/test/models/sqlserver/uuid.rb +1 -1
- data/test/schema/sqlserver_specific_schema.rb +20 -22
- data/test/support/coerceable_test_sqlserver.rb +1 -4
- data/test/support/connection_reflection.rb +1 -2
- data/test/support/core_ext/query_cache.rb +1 -1
- data/test/support/load_schema_sqlserver.rb +3 -5
- data/test/support/minitest_sqlserver.rb +1 -1
- data/test/support/paths_sqlserver.rb +9 -11
- data/test/support/rake_helpers.rb +12 -10
- data/test/support/sql_counter_sqlserver.rb +0 -4
- data/test/support/test_in_memory_oltp.rb +7 -7
- metadata +5 -4
@@ -1,34 +1,32 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "cases/helper_sqlserver"
|
4
4
|
|
5
5
|
if ActiveRecord::Base.connection.supports_json?
|
6
|
-
class JsonTestSQLServer < ActiveRecord::TestCase
|
6
|
+
class JsonTestSQLServer < ActiveRecord::TestCase
|
7
|
+
before do
|
8
|
+
@o1 = SSTestDatatypeMigrationJson.create! json_col: { "a" => "a", "b" => "b", "c" => "c" }
|
9
|
+
@o2 = SSTestDatatypeMigrationJson.create! json_col: { "a" => nil, "b" => "b", "c" => "c" }
|
10
|
+
@o3 = SSTestDatatypeMigrationJson.create! json_col: { "x" => 1, "y" => 2, "z" => 3 }
|
11
|
+
@o4 = SSTestDatatypeMigrationJson.create! json_col: { "array" => [1, 2, 3] }
|
12
|
+
@o5 = SSTestDatatypeMigrationJson.create! json_col: nil
|
13
|
+
end
|
7
14
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
it 'can return and save JSON data' do
|
17
|
-
_(SSTestDatatypeMigrationJson.find(@o1.id).json_col).must_equal({ 'a' => 'a', 'b' => 'b', 'c' => 'c' })
|
18
|
-
@o1.json_col = { 'a' => 'a' }
|
19
|
-
_(@o1.json_col).must_equal({ 'a' => 'a' })
|
20
|
-
@o1.save!
|
21
|
-
_(@o1.reload.json_col).must_equal({ 'a' => 'a' })
|
22
|
-
end
|
15
|
+
it "can return and save JSON data" do
|
16
|
+
_(SSTestDatatypeMigrationJson.find(@o1.id).json_col).must_equal({ "a" => "a", "b" => "b", "c" => "c" })
|
17
|
+
@o1.json_col = { "a" => "a" }
|
18
|
+
_(@o1.json_col).must_equal({ "a" => "a" })
|
19
|
+
@o1.save!
|
20
|
+
_(@o1.reload.json_col).must_equal({ "a" => "a" })
|
21
|
+
end
|
23
22
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
23
|
+
it "can use ISJSON function" do
|
24
|
+
_(SSTestDatatypeMigrationJson.where("ISJSON(json_col) > 0").count).must_equal 4
|
25
|
+
_(SSTestDatatypeMigrationJson.where("ISJSON(json_col) IS NULL").count).must_equal 1
|
26
|
+
end
|
28
27
|
|
29
|
-
|
30
|
-
|
28
|
+
it "can use JSON_VALUE function" do
|
29
|
+
_(SSTestDatatypeMigrationJson.where("JSON_VALUE(json_col, '$.b') = 'b'").count).must_equal 2
|
30
|
+
end
|
31
31
|
end
|
32
|
-
|
33
|
-
end
|
34
32
|
end
|
@@ -1,16 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require "cases/helper_sqlserver"
|
4
|
+
require "models/person"
|
5
5
|
|
6
6
|
class MigrationTestSQLServer < ActiveRecord::TestCase
|
7
|
-
|
8
|
-
describe 'For transactions' do
|
9
|
-
|
7
|
+
describe "For transactions" do
|
10
8
|
before do
|
11
|
-
@trans_test_table1 =
|
12
|
-
@trans_test_table2 =
|
13
|
-
@trans_tables = [@trans_test_table1
|
9
|
+
@trans_test_table1 = "sqlserver_trans_table1"
|
10
|
+
@trans_test_table2 = "sqlserver_trans_table2"
|
11
|
+
@trans_tables = [@trans_test_table1, @trans_test_table2]
|
14
12
|
end
|
15
13
|
|
16
14
|
after do
|
@@ -19,9 +17,9 @@ class MigrationTestSQLServer < ActiveRecord::TestCase
|
|
19
17
|
end
|
20
18
|
end
|
21
19
|
|
22
|
-
it
|
20
|
+
it "not create a tables if error in migrations" do
|
23
21
|
begin
|
24
|
-
migrations_dir = File.join ARTest::SQLServer.migrations_root,
|
22
|
+
migrations_dir = File.join ARTest::SQLServer.migrations_root, "transaction_table"
|
25
23
|
quietly { ActiveRecord::MigrationContext.new(migrations_dir, ActiveRecord::SchemaMigration).up }
|
26
24
|
rescue Exception => e
|
27
25
|
assert_match %r|this and all later migrations canceled|, e.message
|
@@ -29,45 +27,41 @@ class MigrationTestSQLServer < ActiveRecord::TestCase
|
|
29
27
|
_(connection.tables).wont_include @trans_test_table1
|
30
28
|
_(connection.tables).wont_include @trans_test_table2
|
31
29
|
end
|
32
|
-
|
33
30
|
end
|
34
31
|
|
35
|
-
describe
|
36
|
-
|
37
|
-
|
38
|
-
lock_version_column = Person.columns_hash['lock_version']
|
32
|
+
describe "For changing column" do
|
33
|
+
it "not raise exception when column contains default constraint" do
|
34
|
+
lock_version_column = Person.columns_hash["lock_version"]
|
39
35
|
assert_equal :integer, lock_version_column.type
|
40
36
|
assert lock_version_column.default.present?
|
41
|
-
assert_nothing_raised { connection.change_column
|
37
|
+
assert_nothing_raised { connection.change_column "people", "lock_version", :string }
|
42
38
|
Person.reset_column_information
|
43
|
-
lock_version_column = Person.columns_hash[
|
39
|
+
lock_version_column = Person.columns_hash["lock_version"]
|
44
40
|
assert_equal :string, lock_version_column.type
|
45
41
|
assert lock_version_column.default.nil?
|
46
|
-
assert_nothing_raised { connection.change_column
|
42
|
+
assert_nothing_raised { connection.change_column "people", "lock_version", :integer }
|
47
43
|
Person.reset_column_information
|
48
44
|
end
|
49
45
|
|
50
|
-
it
|
46
|
+
it "not drop the default constraint if just renaming" do
|
51
47
|
find_default = lambda do
|
52
|
-
connection.execute_procedure(:sp_helpconstraint,
|
53
|
-
row[
|
48
|
+
connection.execute_procedure(:sp_helpconstraint, "sst_string_defaults", "nomsg").select do |row|
|
49
|
+
row["constraint_type"] == "DEFAULT on column string_with_pretend_paren_three"
|
54
50
|
end.last
|
55
51
|
end
|
56
52
|
default_before = find_default.call
|
57
53
|
connection.change_column :sst_string_defaults, :string_with_pretend_paren_three, :string, limit: 255
|
58
54
|
default_after = find_default.call
|
59
55
|
assert default_after
|
60
|
-
assert_equal default_before[
|
56
|
+
assert_equal default_before["constraint_keys"], default_after["constraint_keys"]
|
61
57
|
end
|
62
|
-
|
63
|
-
it
|
58
|
+
|
59
|
+
it "change limit" do
|
64
60
|
assert_nothing_raised { connection.change_column :people, :lock_version, :integer, limit: 8 }
|
65
61
|
end
|
66
|
-
|
67
|
-
it
|
62
|
+
|
63
|
+
it "change null and default" do
|
68
64
|
assert_nothing_raised { connection.change_column :people, :first_name, :text, null: true, default: nil }
|
69
65
|
end
|
70
|
-
|
71
66
|
end
|
72
|
-
|
73
67
|
end
|
@@ -1,149 +1,146 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require "cases/helper_sqlserver"
|
4
|
+
require "models/post"
|
5
5
|
|
6
6
|
class OrderTestSQLServer < ActiveRecord::TestCase
|
7
|
-
|
8
7
|
fixtures :posts
|
9
8
|
|
10
|
-
it
|
9
|
+
it "not mangel complex order clauses" do
|
11
10
|
xyz_order = "CASE WHEN [title] LIKE N'XYZ%' THEN 0 ELSE 1 END"
|
12
|
-
xyz_post = Post.create title:
|
11
|
+
xyz_post = Post.create title: "XYZ Post", body: "Test cased orders."
|
13
12
|
assert_equal xyz_post, Post.order(Arel.sql(xyz_order)).first
|
14
13
|
end
|
15
14
|
|
16
|
-
it
|
15
|
+
it "support column" do
|
17
16
|
order = "title"
|
18
|
-
post1 = Post.create title:
|
17
|
+
post1 = Post.create title: "AAA Post", body: "Test cased orders."
|
19
18
|
assert_equal post1, Post.order(order).first
|
20
19
|
end
|
21
20
|
|
22
|
-
it
|
21
|
+
it "support column ASC" do
|
23
22
|
order = "title ASC"
|
24
|
-
post1 = Post.create title:
|
23
|
+
post1 = Post.create title: "AAA Post", body: "Test cased orders."
|
25
24
|
assert_equal post1, Post.order(order).first
|
26
25
|
end
|
27
26
|
|
28
|
-
it
|
27
|
+
it "support column DESC" do
|
29
28
|
order = "title DESC"
|
30
|
-
post1 = Post.create title:
|
29
|
+
post1 = Post.create title: "ZZZ Post", body: "Test cased orders."
|
31
30
|
assert_equal post1, Post.order(order).first
|
32
31
|
end
|
33
32
|
|
34
|
-
it
|
33
|
+
it "support column as symbol" do
|
35
34
|
order = :title
|
36
|
-
post1 = Post.create title:
|
35
|
+
post1 = Post.create title: "AAA Post", body: "Test cased orders."
|
37
36
|
assert_equal post1, Post.order(order).first
|
38
37
|
end
|
39
38
|
|
40
|
-
it
|
39
|
+
it "support table and column" do
|
41
40
|
order = "posts.title"
|
42
|
-
post1 = Post.create title:
|
41
|
+
post1 = Post.create title: "AAA Post", body: "Test cased orders."
|
43
42
|
assert_equal post1, Post.order(order).first
|
44
43
|
end
|
45
44
|
|
46
|
-
it
|
45
|
+
it "support quoted column" do
|
47
46
|
order = "[title]"
|
48
|
-
post1 = Post.create title:
|
47
|
+
post1 = Post.create title: "AAA Post", body: "Test cased orders."
|
49
48
|
assert_equal post1, Post.order(Arel.sql(order)).first
|
50
49
|
end
|
51
50
|
|
52
|
-
it
|
51
|
+
it "support quoted table and column" do
|
53
52
|
order = "[posts].[title]"
|
54
|
-
post1 = Post.create title:
|
53
|
+
post1 = Post.create title: "AAA Post", body: "Test cased orders."
|
55
54
|
assert_equal post1, Post.order(Arel.sql(order)).first
|
56
55
|
end
|
57
56
|
|
58
|
-
it
|
57
|
+
it "support primary: column, secondary: column" do
|
59
58
|
order = "title DESC, body"
|
60
|
-
post1 = Post.create title:
|
61
|
-
post2 = Post.create title:
|
59
|
+
post1 = Post.create title: "ZZZ Post", body: "Test cased orders."
|
60
|
+
post2 = Post.create title: "ZZZ Post", body: "ZZZ Test cased orders."
|
62
61
|
assert_equal post1, Post.order(order).first
|
63
62
|
assert_equal post2, Post.order(order).second
|
64
63
|
end
|
65
64
|
|
66
|
-
it
|
65
|
+
it "support primary: table and column, secondary: column" do
|
67
66
|
order = "posts.title DESC, body"
|
68
|
-
post1 = Post.create title:
|
69
|
-
post2 = Post.create title:
|
67
|
+
post1 = Post.create title: "ZZZ Post", body: "Test cased orders."
|
68
|
+
post2 = Post.create title: "ZZZ Post", body: "ZZZ Test cased orders."
|
70
69
|
assert_equal post1, Post.order(order).first
|
71
70
|
assert_equal post2, Post.order(order).second
|
72
71
|
end
|
73
72
|
|
74
|
-
it
|
73
|
+
it "support primary: case expression, secondary: column" do
|
75
74
|
order = "(CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END) DESC, body"
|
76
|
-
post1 = Post.create title:
|
77
|
-
post2 = Post.create title:
|
75
|
+
post1 = Post.create title: "ZZZ Post", body: "Test cased orders."
|
76
|
+
post2 = Post.create title: "ZZZ Post", body: "ZZZ Test cased orders."
|
78
77
|
assert_equal post1, Post.order(Arel.sql(order)).first
|
79
78
|
assert_equal post2, Post.order(Arel.sql(order)).second
|
80
79
|
end
|
81
80
|
|
82
|
-
it
|
81
|
+
it "support primary: quoted table and column, secondary: case expresion" do
|
83
82
|
order = "[posts].[body] DESC, (CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END) DESC"
|
84
|
-
post1 = Post.create title:
|
85
|
-
post2 = Post.create title:
|
83
|
+
post1 = Post.create title: "ZZZ Post", body: "ZZZ Test cased orders."
|
84
|
+
post2 = Post.create title: "ZZY Post", body: "ZZZ Test cased orders."
|
86
85
|
assert_equal post1, Post.order(Arel.sql(order)).first
|
87
86
|
assert_equal post2, Post.order(Arel.sql(order)).second
|
88
87
|
end
|
89
88
|
|
90
|
-
it
|
89
|
+
it "support inline function" do
|
91
90
|
order = "LEN(title)"
|
92
|
-
post1 = Post.create title:
|
91
|
+
post1 = Post.create title: "A", body: "AAA Test cased orders."
|
93
92
|
assert_equal post1, Post.order(Arel.sql(order)).first
|
94
93
|
end
|
95
94
|
|
96
|
-
it
|
95
|
+
it "support inline function with parameters" do
|
97
96
|
order = "SUBSTRING(title, 1, 3)"
|
98
|
-
post1 = Post.create title:
|
97
|
+
post1 = Post.create title: "AAA Post", body: "Test cased orders."
|
99
98
|
assert_equal post1, Post.order(Arel.sql(order)).first
|
100
99
|
end
|
101
100
|
|
102
|
-
it
|
101
|
+
it "support inline function with parameters DESC" do
|
103
102
|
order = "SUBSTRING(title, 1, 3) DESC"
|
104
|
-
post1 = Post.create title:
|
103
|
+
post1 = Post.create title: "ZZZ Post", body: "Test cased orders."
|
105
104
|
assert_equal post1, Post.order(Arel.sql(order)).first
|
106
105
|
end
|
107
106
|
|
108
|
-
it
|
107
|
+
it "support primary: inline function, secondary: column" do
|
109
108
|
order = "LEN(title), body"
|
110
|
-
post1 = Post.create title:
|
111
|
-
post2 = Post.create title:
|
109
|
+
post1 = Post.create title: "A", body: "AAA Test cased orders."
|
110
|
+
post2 = Post.create title: "A", body: "Test cased orders."
|
112
111
|
assert_equal post1, Post.order(Arel.sql(order)).first
|
113
112
|
assert_equal post2, Post.order(Arel.sql(order)).second
|
114
113
|
end
|
115
114
|
|
116
|
-
it
|
115
|
+
it "support primary: inline function, secondary: column with direction" do
|
117
116
|
order = "LEN(title) ASC, body DESC"
|
118
|
-
post1 = Post.create title:
|
119
|
-
post2 = Post.create title:
|
117
|
+
post1 = Post.create title: "A", body: "ZZZ Test cased orders."
|
118
|
+
post2 = Post.create title: "A", body: "Test cased orders."
|
120
119
|
assert_equal post1, Post.order(Arel.sql(order)).first
|
121
120
|
assert_equal post2, Post.order(Arel.sql(order)).second
|
122
121
|
end
|
123
122
|
|
124
|
-
it
|
123
|
+
it "support primary: column, secondary: inline function" do
|
125
124
|
order = "body DESC, LEN(title)"
|
126
|
-
post1 = Post.create title:
|
127
|
-
post2 = Post.create title:
|
125
|
+
post1 = Post.create title: "Post", body: "ZZZ Test cased orders."
|
126
|
+
post2 = Post.create title: "Longer Post", body: "ZZZ Test cased orders."
|
128
127
|
assert_equal post1, Post.order(Arel.sql(order)).first
|
129
128
|
assert_equal post2, Post.order(Arel.sql(order)).second
|
130
129
|
end
|
131
130
|
|
132
|
-
it
|
131
|
+
it "support primary: case expression, secondary: inline function" do
|
133
132
|
order = "CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END DESC, LEN(body) ASC"
|
134
|
-
post1 = Post.create title:
|
135
|
-
post2 = Post.create title:
|
133
|
+
post1 = Post.create title: "ZZZ Post", body: "Z"
|
134
|
+
post2 = Post.create title: "ZZZ Post", body: "Test cased orders."
|
136
135
|
assert_equal post1, Post.order(Arel.sql(order)).first
|
137
136
|
assert_equal post2, Post.order(Arel.sql(order)).second
|
138
137
|
end
|
139
138
|
|
140
|
-
it
|
139
|
+
it "support primary: inline function, secondary: case expression" do
|
141
140
|
order = "LEN(body), CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END DESC"
|
142
|
-
post1 = Post.create title:
|
143
|
-
post2 = Post.create title:
|
141
|
+
post1 = Post.create title: "ZZZ Post", body: "Z"
|
142
|
+
post2 = Post.create title: "Post", body: "Z"
|
144
143
|
assert_equal post1, Post.order(Arel.sql(order)).first
|
145
144
|
assert_equal post2, Post.order(Arel.sql(order)).second
|
146
145
|
end
|
147
|
-
|
148
|
-
|
149
146
|
end
|
@@ -1,11 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
3
|
+
require "cases/helper_sqlserver"
|
4
|
+
require "models/person"
|
5
|
+
require "models/reader"
|
6
6
|
|
7
7
|
class PessimisticLockingTestSQLServer < ActiveRecord::TestCase
|
8
|
-
|
9
8
|
fixtures :people, :readers
|
10
9
|
|
11
10
|
before do
|
@@ -13,15 +12,14 @@ class PessimisticLockingTestSQLServer < ActiveRecord::TestCase
|
|
13
12
|
Reader.columns
|
14
13
|
end
|
15
14
|
|
16
|
-
it
|
15
|
+
it "uses with updlock by default" do
|
17
16
|
assert_sql %r|SELECT \[people\]\.\* FROM \[people\] WITH\(UPDLOCK\)| do
|
18
17
|
_(Person.lock(true).to_a).must_equal Person.all.to_a
|
19
18
|
end
|
20
19
|
end
|
21
20
|
|
22
|
-
describe
|
23
|
-
|
24
|
-
it 'lock with simple find' do
|
21
|
+
describe "For simple finds with default lock option" do
|
22
|
+
it "lock with simple find" do
|
25
23
|
assert_nothing_raised do
|
26
24
|
Person.transaction do
|
27
25
|
_(Person.lock(true).find(1)).must_equal Person.find(1)
|
@@ -29,7 +27,7 @@ class PessimisticLockingTestSQLServer < ActiveRecord::TestCase
|
|
29
27
|
end
|
30
28
|
end
|
31
29
|
|
32
|
-
it
|
30
|
+
it "lock with scoped find" do
|
33
31
|
assert_nothing_raised do
|
34
32
|
Person.transaction do
|
35
33
|
Person.lock(true).scoping do
|
@@ -39,8 +37,8 @@ class PessimisticLockingTestSQLServer < ActiveRecord::TestCase
|
|
39
37
|
end
|
40
38
|
end
|
41
39
|
|
42
|
-
it
|
43
|
-
|
40
|
+
it "lock with eager find" do
|
41
|
+
assert_nothing_raised do
|
44
42
|
Person.transaction do
|
45
43
|
person = Person.lock(true).includes(:readers).find(1)
|
46
44
|
_(person).must_equal Person.find(1)
|
@@ -48,62 +46,56 @@ class PessimisticLockingTestSQLServer < ActiveRecord::TestCase
|
|
48
46
|
end
|
49
47
|
end
|
50
48
|
|
51
|
-
it
|
49
|
+
it "can add a custom lock directive" do
|
52
50
|
assert_sql %r|SELECT \[people\]\.\* FROM \[people\] WITH\(HOLDLOCK, ROWLOCK\)| do
|
53
|
-
Person.lock(
|
51
|
+
Person.lock("WITH(HOLDLOCK, ROWLOCK)").load
|
54
52
|
end
|
55
53
|
end
|
56
54
|
|
57
|
-
describe
|
58
|
-
|
59
|
-
it 'joined tables use updlock by default' do
|
55
|
+
describe "joining tables" do
|
56
|
+
it "joined tables use updlock by default" do
|
60
57
|
assert_sql %r|SELECT \[people\]\.\* FROM \[people\] WITH\(UPDLOCK\) INNER JOIN \[readers\] WITH\(UPDLOCK\)\s+ON \[readers\]\.\[person_id\] = \[people\]\.\[id\]| do
|
61
58
|
Person.lock(true).joins(:readers).load
|
62
59
|
end
|
63
60
|
end
|
64
61
|
|
65
|
-
it
|
62
|
+
it "joined tables can use custom lock directive" do
|
66
63
|
assert_sql %r|SELECT \[people\]\.\* FROM \[people\] WITH\(NOLOCK\) INNER JOIN \[readers\] WITH\(NOLOCK\)\s+ON \[readers\]\.\[person_id\] = \[people\]\.\[id\]| do
|
67
|
-
Person.lock(
|
64
|
+
Person.lock("WITH(NOLOCK)").joins(:readers).load
|
68
65
|
end
|
69
66
|
end
|
70
67
|
|
71
|
-
it
|
68
|
+
it "left joined tables use updlock by default" do
|
72
69
|
assert_sql %r|SELECT \[people\]\.\* FROM \[people\] WITH\(UPDLOCK\) LEFT OUTER JOIN \[readers\] WITH\(UPDLOCK\)\s+ON \[readers\]\.\[person_id\] = \[people\]\.\[id\]| do
|
73
70
|
Person.lock(true).left_joins(:readers).load
|
74
71
|
end
|
75
72
|
end
|
76
73
|
|
77
|
-
it
|
74
|
+
it "left joined tables can use custom lock directive" do
|
78
75
|
assert_sql %r|SELECT \[people\]\.\* FROM \[people\] WITH\(NOLOCK\) LEFT OUTER JOIN \[readers\] WITH\(NOLOCK\)\s+ON \[readers\]\.\[person_id\] = \[people\]\.\[id\]| do
|
79
|
-
Person.lock(
|
76
|
+
Person.lock("WITH(NOLOCK)").left_joins(:readers).load
|
80
77
|
end
|
81
78
|
end
|
82
|
-
|
83
79
|
end
|
84
|
-
|
85
80
|
end
|
86
81
|
|
87
|
-
describe
|
88
|
-
|
82
|
+
describe "For paginated finds" do
|
89
83
|
before do
|
90
84
|
Person.delete_all
|
91
85
|
20.times { |n| Person.create!(first_name: "Thing_#{n}") }
|
92
86
|
end
|
93
87
|
|
94
|
-
it
|
88
|
+
it "copes with eager loading un-locked paginated" do
|
95
89
|
eager_ids_sql = /SELECT\s+DISTINCT \[people\].\[id\] FROM \[people\] WITH\(UPDLOCK\) LEFT OUTER JOIN \[readers\] WITH\(UPDLOCK\)\s+ON \[readers\].\[person_id\] = \[people\].\[id\]\s+ORDER BY \[people\].\[id\] ASC OFFSET @0 ROWS FETCH NEXT @1 ROWS ONLY/
|
96
90
|
loader_sql = /SELECT.*FROM \[people\] WITH\(UPDLOCK\).*WHERE \[people\]\.\[id\] IN/
|
97
91
|
assert_sql(eager_ids_sql, loader_sql) do
|
98
92
|
people = Person.lock(true).limit(5).offset(10).includes(:readers).references(:readers).to_a
|
99
|
-
_(people[0].first_name).must_equal
|
100
|
-
_(people[1].first_name).must_equal
|
101
|
-
_(people[2].first_name).must_equal
|
102
|
-
_(people[3].first_name).must_equal
|
103
|
-
_(people[4].first_name).must_equal
|
93
|
+
_(people[0].first_name).must_equal "Thing_10"
|
94
|
+
_(people[1].first_name).must_equal "Thing_11"
|
95
|
+
_(people[2].first_name).must_equal "Thing_12"
|
96
|
+
_(people[3].first_name).must_equal "Thing_13"
|
97
|
+
_(people[4].first_name).must_equal "Thing_14"
|
104
98
|
end
|
105
99
|
end
|
106
|
-
|
107
100
|
end
|
108
|
-
|
109
101
|
end
|