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