activerecord-sqlserver-adapter 6.0.2 → 6.1.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +18 -71
- data/README.md +13 -2
- data/VERSION +1 -1
- data/activerecord-sqlserver-adapter.gemspec +1 -1
- data/lib/active_record/connection_adapters/sqlserver/core_ext/calculations.rb +0 -9
- data/lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb +7 -2
- data/lib/active_record/connection_adapters/sqlserver/database_limits.rb +0 -4
- data/lib/active_record/connection_adapters/sqlserver/database_statements.rb +27 -15
- data/lib/active_record/connection_adapters/sqlserver/quoting.rb +3 -3
- data/lib/active_record/connection_adapters/sqlserver/schema_creation.rb +22 -1
- data/lib/active_record/connection_adapters/sqlserver/schema_dumper.rb +9 -3
- data/lib/active_record/connection_adapters/sqlserver/schema_statements.rb +7 -5
- data/lib/active_record/connection_adapters/sqlserver/sql_type_metadata.rb +27 -7
- data/lib/active_record/connection_adapters/sqlserver/table_definition.rb +0 -1
- data/lib/active_record/connection_adapters/sqlserver/transaction.rb +2 -2
- data/lib/active_record/connection_adapters/sqlserver/utils.rb +1 -1
- data/lib/active_record/connection_adapters/sqlserver_adapter.rb +83 -66
- data/lib/active_record/connection_adapters/sqlserver_column.rb +17 -0
- data/lib/active_record/sqlserver_base.rb +9 -15
- data/lib/active_record/tasks/sqlserver_database_tasks.rb +17 -14
- data/lib/arel/visitors/sqlserver.rb +60 -28
- data/test/cases/adapter_test_sqlserver.rb +17 -15
- data/test/cases/change_column_collation_test_sqlserver.rb +33 -0
- data/test/cases/coerced_tests.rb +531 -77
- data/test/cases/disconnected_test_sqlserver.rb +39 -0
- data/test/cases/execute_procedure_test_sqlserver.rb +9 -0
- data/test/cases/in_clause_test_sqlserver.rb +27 -0
- data/test/cases/migration_test_sqlserver.rb +7 -0
- data/test/cases/order_test_sqlserver.rb +7 -0
- data/test/cases/primary_keys_test_sqlserver.rb +103 -0
- data/test/cases/rake_test_sqlserver.rb +3 -2
- data/test/cases/schema_dumper_test_sqlserver.rb +9 -0
- data/test/migrations/create_clients_and_change_column_collation.rb +19 -0
- data/test/models/sqlserver/sst_string_collation.rb +3 -0
- data/test/schema/sqlserver_specific_schema.rb +7 -0
- data/test/support/marshal_compatibility_fixtures/SQLServer/rails_6_0_topic.dump +0 -0
- data/test/support/marshal_compatibility_fixtures/SQLServer/rails_6_0_topic_associations.dump +0 -0
- data/test/support/sql_counter_sqlserver.rb +14 -12
- metadata +22 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 257660386743662b1e8052c5073aaa07bdff955924fae3da4ba83c41229deb85
|
4
|
+
data.tar.gz: 288cf426206b713aeca8b0647f57059a4a781d00cab800340d8f5c0bad0acbdc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 22efb62e73df4116659bbac023f3694192476cc5d4d2840c469b19b0f6c37145cf989803c94c53d45aa78943b2a72df419ffbc15b452f612ab6f420fee10a19c
|
7
|
+
data.tar.gz: 70d8088ec4278d48beb73cc1319f681d6f3ee874d200b2868370425a2a0a9b2e2859171b5cc3817b3abf63cd512911655813bafe65ae5e319dc1bb889bf0f575
|
data/CHANGELOG.md
CHANGED
@@ -1,78 +1,25 @@
|
|
1
|
-
## v6.0.
|
1
|
+
## v6.1.0.0.rc1
|
2
2
|
|
3
3
|
#### Fixed
|
4
4
|
|
5
|
-
- [#
|
5
|
+
- [#872](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/872) Use native String#start_with
|
6
|
+
- [#876](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/876) Use native String#end_with
|
7
|
+
- [#873](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/873) Various fixes to get the tests running for Rails 6.1
|
8
|
+
- [#874](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/874) Deduplicate schema cache structures
|
9
|
+
- [#875](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/875) Handle default boolean column values when deduplicating
|
10
|
+
- [#879](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/879) Added visit method for HomogeneousIn
|
11
|
+
- [#880](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/880) Handle any default column class when deduplicating
|
12
|
+
- [#861](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/861) Fix Rails 6.1 database config
|
13
|
+
- [#890](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/890) Fix removal of invalid ordering from select statements
|
14
|
+
- [#881](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/881) Dump column collation to schema.rb and allow collation changes using column_change
|
15
|
+
- [#891](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/891) Add support for if_not_exists to indexes
|
16
|
+
- [#892](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/892) Add support for if_exists on remove_column
|
17
|
+
- [#883](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/885) Fix quoting of ActiveRecord::Relation::QueryAttribute and ActiveModel::Attributes
|
18
|
+
- [#893](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/893) Add Active Record Marshal forward compatibility tests
|
19
|
+
- [#903](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/903) Raise ActiveRecord::ConnectionNotEstablished on calls to execute with a disconnected connection
|
6
20
|
|
7
21
|
#### Changed
|
8
22
|
|
9
|
-
- [#
|
23
|
+
- [#917](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/917) Refactored to use new_client connection pattern
|
10
24
|
|
11
|
-
|
12
|
-
|
13
|
-
- [#855](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/855) Add helpers to create/change/drop a schema.
|
14
|
-
- [#857](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/857) Included WAITFOR as read query type.
|
15
|
-
- [#865](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/865) Implemented optimizer hints.
|
16
|
-
- [#845](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/845) Add support for lateral using CROSS/OUTER APPLY.
|
17
|
-
- [#870](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/870) Added DecimalWithoutScale type
|
18
|
-
|
19
|
-
## v6.0.1
|
20
|
-
|
21
|
-
#### Fixed
|
22
|
-
|
23
|
-
- [#851](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/851) Updated 'column_definitions_sql' to ensure that only primary key key constraints are queried for
|
24
|
-
|
25
|
-
## v6.0.0
|
26
|
-
|
27
|
-
**No Changes**
|
28
|
-
|
29
|
-
## v6.0.0.rc2
|
30
|
-
|
31
|
-
#### Fixed
|
32
|
-
|
33
|
-
- [#639](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/639) Primary key should be lowercase if schema forced to lowercase
|
34
|
-
- [#720](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/720) quoted_date doesn't work for Type::DateTime
|
35
|
-
|
36
|
-
#### Changed
|
37
|
-
|
38
|
-
- [#826](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/826) Rubocop: Enable Style/StringLiterals cop
|
39
|
-
- [#827](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/827) Rubocop: Enable Layout/EmptyLinesAroundClassBody cop
|
40
|
-
- [#828](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/828) Rubocop: Enable Layout/EmptyLines cop
|
41
|
-
- [#829](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/829) Rubocop: Enable Layout/Layout/EmptyLinesAround* cops
|
42
|
-
- [#830](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/830) Rubocop: Enable Layout/IndentationWidth and Layout/TrailingWhitespace cops
|
43
|
-
- [#831](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/831) Rubocop: Enable Spacing cops
|
44
|
-
- [#832](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/832) Rubocop: Enable Bundler cops
|
45
|
-
- [#833](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/833) Rubocop: Enable Layout/* cops
|
46
|
-
- [#834](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/834) Rubocop: Enable Lint/UselessAssignment cop
|
47
|
-
- [#835](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/835) Rubocop: Configure Naming cops
|
48
|
-
|
49
|
-
## v6.0.0.rc1
|
50
|
-
|
51
|
-
#### Fixed
|
52
|
-
|
53
|
-
- [#690](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/690) Rails 6 support
|
54
|
-
- [#805](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/805) Rails 6: Fix database tasks tests for SQL Server
|
55
|
-
- [#807](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/807) Rails 6: Skip binary fixtures test on Windows
|
56
|
-
- [#809](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/809) Rails 6: Coerce reaper test using fork
|
57
|
-
- [#810](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/810) Rails 6: Fix randomly failing tests due to schema load
|
58
|
-
- [#812](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/812) Rails 6: Coerce ReloadModelsTest test on Windows
|
59
|
-
- [#818](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/818) Handle false return by TinyTDS if connection fails and fixed CI
|
60
|
-
- [#819](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/819) Fix Ruby 2.7 kwargs warnings
|
61
|
-
- [#825](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/825) Adjust error message when connection is dead
|
62
|
-
|
63
|
-
#### Changed
|
64
|
-
|
65
|
-
- [#716](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/716) Translate the connection timed out error
|
66
|
-
- [#763](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/763) Refactor columns introspection query to make it faster
|
67
|
-
- [#783](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/783) Update test matrix
|
68
|
-
- [#820](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/820) Enable frozen strings for tests
|
69
|
-
- [#821](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/821) Enable frozen strings - part 1
|
70
|
-
- [#822](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/822) Enable frozen strings - part 2
|
71
|
-
- [#823](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/823) Enable frozen strings - final
|
72
|
-
- [#824](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/824) Tidy up Gemfile
|
73
|
-
|
74
|
-
#### Added
|
75
|
-
|
76
|
-
- [#726](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/726) How to Develop ActiveRecord SQL Server Adapter with Pre-Installed MS SQL
|
77
|
-
|
78
|
-
Please check [5-2-stable](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/5-2-stable/CHANGELOG.md) for previous changes.
|
25
|
+
Please check [6-0-stable](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/6-0-stable/CHANGELOG.md) for previous changes.
|
data/README.md
CHANGED
@@ -7,9 +7,20 @@
|
|
7
7
|
|
8
8
|
## About The Adapter
|
9
9
|
|
10
|
-
The SQL Server adapter for ActiveRecord
|
10
|
+
The SQL Server adapter for ActiveRecord using SQL Server 2012 or higher.
|
11
11
|
|
12
|
-
Interested in older versions? We follow a rational versioning policy that tracks Rails. That means that our
|
12
|
+
Interested in older versions? We follow a rational versioning policy that tracks Rails. That means that our 6.x version of the adapter is only for the latest 6.x version of Rails. If you need the adapter for SQL Server 2008 or 2005, you are still in the right spot. Just install the latest 3.2.x to 4.1.x version of the adapter that matches your Rails version. We also have stable branches for each major/minor release of ActiveRecord.
|
13
|
+
|
14
|
+
| Adapter Version | Rails Version | Support |
|
15
|
+
| ------------- | --- | --- |
|
16
|
+
| `6.1.0.0.rc1` | `6.1.x` | [wip](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/main) |
|
17
|
+
| `6.0.2` | `6.0.x` | [active](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/6-0-stable) |
|
18
|
+
| `5.2.1` | `5.2.x` | [active](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/5-2-stable) |
|
19
|
+
| `5.1.6` | `5.1.x` | [ended](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/5-1-stable) |
|
20
|
+
| `4.2.18` | `4.2.x` | [ended](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/4-2-stable) |
|
21
|
+
| `4.1.8` | `4.1.x` | [ended](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/4-1-stable) |
|
22
|
+
|
23
|
+
For older versions, please check their stable branches.
|
13
24
|
|
14
25
|
#### Native Data Type Support
|
15
26
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
6.0.
|
1
|
+
6.1.0.0.rc1
|
@@ -27,6 +27,6 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
28
28
|
spec.require_paths = ["lib"]
|
29
29
|
|
30
|
-
spec.add_dependency "activerecord", "~> 6.
|
30
|
+
spec.add_dependency "activerecord", "~> 6.1.0"
|
31
31
|
spec.add_dependency "tiny_tds"
|
32
32
|
end
|
@@ -31,15 +31,6 @@ module ActiveRecord
|
|
31
31
|
def build_count_subquery(relation, column_name, distinct)
|
32
32
|
super(relation.unscope(:order), column_name, distinct)
|
33
33
|
end
|
34
|
-
|
35
|
-
def type_cast_calculated_value(value, type, operation = nil)
|
36
|
-
case operation
|
37
|
-
when "count" then value.to_i
|
38
|
-
when "sum" then type.deserialize(value || 0)
|
39
|
-
when "average" then value&.respond_to?(:to_d) ? value.to_d : value
|
40
|
-
else type.deserialize(value)
|
41
|
-
end
|
42
|
-
end
|
43
34
|
end
|
44
35
|
end
|
45
36
|
end
|
@@ -21,13 +21,18 @@ module ActiveRecord
|
|
21
21
|
# which uses sp_executesql to just the first argument, then unquote it. Likewise our
|
22
22
|
# `sp_executesql` method should substitude the @n args with the quoted values.
|
23
23
|
def unprepare_sqlserver_statement(sql, binds)
|
24
|
-
return sql unless sql.
|
24
|
+
return sql unless sql.start_with?(SQLSERVER_STATEMENT_PREFIX)
|
25
25
|
|
26
26
|
executesql = sql.from(SQLSERVER_STATEMENT_PREFIX.length)
|
27
27
|
executesql = executesql.match(SQLSERVER_STATEMENT_REGEXP).to_a[1]
|
28
28
|
|
29
29
|
binds.each_with_index do |bind, index|
|
30
|
-
|
30
|
+
|
31
|
+
value = if bind.is_a?(::ActiveModel::Attribute) then
|
32
|
+
connection.quote(bind.value_for_database)
|
33
|
+
else
|
34
|
+
connection.quote(bind)
|
35
|
+
end
|
31
36
|
executesql = executesql.sub("@#{index}", value)
|
32
37
|
end
|
33
38
|
|
@@ -17,6 +17,7 @@ module ActiveRecord
|
|
17
17
|
end
|
18
18
|
|
19
19
|
materialize_transactions
|
20
|
+
mark_transaction_written_if_write(sql)
|
20
21
|
|
21
22
|
if id_insert_table_name = query_requires_identity_insert?(sql)
|
22
23
|
with_identity_insert_enabled(id_insert_table_name) { do_execute(sql, name) }
|
@@ -31,6 +32,7 @@ module ActiveRecord
|
|
31
32
|
end
|
32
33
|
|
33
34
|
materialize_transactions
|
35
|
+
mark_transaction_written_if_write(sql)
|
34
36
|
|
35
37
|
sp_executesql(sql, name, binds, prepare: prepare)
|
36
38
|
end
|
@@ -54,7 +56,7 @@ module ActiveRecord
|
|
54
56
|
end
|
55
57
|
|
56
58
|
def begin_db_transaction
|
57
|
-
do_execute "BEGIN TRANSACTION"
|
59
|
+
do_execute "BEGIN TRANSACTION", "TRANSACTION"
|
58
60
|
end
|
59
61
|
|
60
62
|
def transaction_isolation_levels
|
@@ -67,25 +69,25 @@ module ActiveRecord
|
|
67
69
|
end
|
68
70
|
|
69
71
|
def set_transaction_isolation_level(isolation_level)
|
70
|
-
do_execute "SET TRANSACTION ISOLATION LEVEL #{isolation_level}"
|
72
|
+
do_execute "SET TRANSACTION ISOLATION LEVEL #{isolation_level}", "TRANSACTION"
|
71
73
|
end
|
72
74
|
|
73
75
|
def commit_db_transaction
|
74
|
-
do_execute "COMMIT TRANSACTION"
|
76
|
+
do_execute "COMMIT TRANSACTION", "TRANSACTION"
|
75
77
|
end
|
76
78
|
|
77
79
|
def exec_rollback_db_transaction
|
78
|
-
do_execute "IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION"
|
80
|
+
do_execute "IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION", "TRANSACTION"
|
79
81
|
end
|
80
82
|
|
81
83
|
include Savepoints
|
82
84
|
|
83
85
|
def create_savepoint(name = current_savepoint_name)
|
84
|
-
do_execute "SAVE TRANSACTION #{name}"
|
86
|
+
do_execute "SAVE TRANSACTION #{name}", "TRANSACTION"
|
85
87
|
end
|
86
88
|
|
87
89
|
def exec_rollback_to_savepoint(name = current_savepoint_name)
|
88
|
-
do_execute "ROLLBACK TRANSACTION #{name}"
|
90
|
+
do_execute "ROLLBACK TRANSACTION #{name}", "TRANSACTION"
|
89
91
|
end
|
90
92
|
|
91
93
|
def release_savepoint(name = current_savepoint_name)
|
@@ -165,7 +167,7 @@ module ActiveRecord
|
|
165
167
|
log(sql, name) do
|
166
168
|
case @connection_options[:mode]
|
167
169
|
when :dblib
|
168
|
-
result =
|
170
|
+
result = ensure_established_connection! { dblib_execute(sql) }
|
169
171
|
options = { as: :hash, cache_rows: true, timezone: ActiveRecord::Base.default_timezone || :utc }
|
170
172
|
result.each(options) do |row|
|
171
173
|
r = row.with_indifferent_access
|
@@ -291,6 +293,7 @@ module ActiveRecord
|
|
291
293
|
|
292
294
|
def do_execute(sql, name = "SQL")
|
293
295
|
materialize_transactions
|
296
|
+
mark_transaction_written_if_write(sql)
|
294
297
|
|
295
298
|
log(sql, name) { raw_connection_do(sql) }
|
296
299
|
end
|
@@ -354,13 +357,7 @@ module ActiveRecord
|
|
354
357
|
def raw_connection_do(sql)
|
355
358
|
case @connection_options[:mode]
|
356
359
|
when :dblib
|
357
|
-
result =
|
358
|
-
|
359
|
-
# TinyTDS returns false instead of raising an exception if connection fails.
|
360
|
-
# Getting around this by raising an exception ourselves while this PR
|
361
|
-
# https://github.com/rails-sqlserver/tiny_tds/pull/469 is not released.
|
362
|
-
raise TinyTds::Error, "failed to execute statement" if result.is_a?(FalseClass)
|
363
|
-
|
360
|
+
result = ensure_established_connection! { dblib_execute(sql) }
|
364
361
|
result.do
|
365
362
|
end
|
366
363
|
ensure
|
@@ -425,7 +422,7 @@ module ActiveRecord
|
|
425
422
|
def raw_connection_run(sql)
|
426
423
|
case @connection_options[:mode]
|
427
424
|
when :dblib
|
428
|
-
|
425
|
+
ensure_established_connection! { dblib_execute(sql) }
|
429
426
|
end
|
430
427
|
end
|
431
428
|
|
@@ -459,6 +456,21 @@ module ActiveRecord
|
|
459
456
|
end
|
460
457
|
handle
|
461
458
|
end
|
459
|
+
|
460
|
+
def dblib_execute(sql)
|
461
|
+
@connection.execute(sql).tap do |result|
|
462
|
+
# TinyTDS returns false instead of raising an exception if connection fails.
|
463
|
+
# Getting around this by raising an exception ourselves while this PR
|
464
|
+
# https://github.com/rails-sqlserver/tiny_tds/pull/469 is not released.
|
465
|
+
raise TinyTds::Error, "failed to execute statement" if result.is_a?(FalseClass)
|
466
|
+
end
|
467
|
+
end
|
468
|
+
|
469
|
+
def ensure_established_connection!
|
470
|
+
raise TinyTds::Error, 'SQL Server client is not connected' unless @connection
|
471
|
+
|
472
|
+
yield
|
473
|
+
end
|
462
474
|
end
|
463
475
|
end
|
464
476
|
end
|
@@ -10,14 +10,14 @@ module ActiveRecord
|
|
10
10
|
|
11
11
|
def fetch_type_metadata(sql_type, sqlserver_options = {})
|
12
12
|
cast_type = lookup_cast_type(sql_type)
|
13
|
-
|
13
|
+
simple_type = SqlTypeMetadata.new(
|
14
14
|
sql_type: sql_type,
|
15
15
|
type: cast_type.type,
|
16
16
|
limit: cast_type.limit,
|
17
17
|
precision: cast_type.precision,
|
18
|
-
scale: cast_type.scale
|
19
|
-
sqlserver_options: sqlserver_options
|
18
|
+
scale: cast_type.scale
|
20
19
|
)
|
20
|
+
SQLServer::TypeMetadata.new(simple_type, sqlserver_options: sqlserver_options)
|
21
21
|
end
|
22
22
|
|
23
23
|
def quote_string(s)
|
@@ -3,9 +3,13 @@
|
|
3
3
|
module ActiveRecord
|
4
4
|
module ConnectionAdapters
|
5
5
|
module SQLServer
|
6
|
-
class SchemaCreation <
|
6
|
+
class SchemaCreation < SchemaCreation
|
7
7
|
private
|
8
8
|
|
9
|
+
def supports_index_using?
|
10
|
+
false
|
11
|
+
end
|
12
|
+
|
9
13
|
def visit_TableDefinition(o)
|
10
14
|
if_not_exists = o.if_not_exists
|
11
15
|
|
@@ -29,11 +33,28 @@ module ActiveRecord
|
|
29
33
|
sql
|
30
34
|
end
|
31
35
|
|
36
|
+
def visit_CreateIndexDefinition(o)
|
37
|
+
if_not_exists = o.if_not_exists
|
38
|
+
|
39
|
+
o.if_not_exists = false
|
40
|
+
|
41
|
+
sql = super
|
42
|
+
|
43
|
+
if if_not_exists
|
44
|
+
sql = "IF NOT EXISTS (SELECT name FROM sysindexes WHERE name = '#{o.index.name}') #{sql}"
|
45
|
+
end
|
46
|
+
|
47
|
+
sql
|
48
|
+
end
|
49
|
+
|
32
50
|
def add_column_options!(sql, options)
|
33
51
|
sql << " DEFAULT #{quote_default_expression(options[:default], options[:column])}" if options_include_default?(options)
|
34
52
|
if options[:null] == false
|
35
53
|
sql << " NOT NULL"
|
36
54
|
end
|
55
|
+
if options[:collation].present?
|
56
|
+
sql << " COLLATE #{options[:collation]}"
|
57
|
+
end
|
37
58
|
if options[:is_identity] == true
|
38
59
|
sql << " IDENTITY(1,1)"
|
39
60
|
end
|
@@ -15,7 +15,7 @@ module ActiveRecord
|
|
15
15
|
private
|
16
16
|
|
17
17
|
def explicit_primary_key_default?(column)
|
18
|
-
column.
|
18
|
+
column.type == :integer && !column.is_identity?
|
19
19
|
end
|
20
20
|
|
21
21
|
def schema_limit(column)
|
@@ -27,11 +27,17 @@ module ActiveRecord
|
|
27
27
|
def schema_collation(column)
|
28
28
|
return unless column.collation
|
29
29
|
|
30
|
-
|
30
|
+
# use inspect to ensure collation is dumped as string. Without this it's dumped as
|
31
|
+
# a constant ('collation: SQL_Latin1_General_CP1_CI_AS')
|
32
|
+
collation = column.collation.inspect
|
33
|
+
# use inspect to ensure string comparison
|
34
|
+
default_collation = @connection.collation.inspect
|
35
|
+
|
36
|
+
collation if collation != default_collation
|
31
37
|
end
|
32
38
|
|
33
39
|
def default_primary_key?(column)
|
34
|
-
super && column.
|
40
|
+
super && column.is_identity?
|
35
41
|
end
|
36
42
|
end
|
37
43
|
end
|
@@ -27,7 +27,7 @@ module ActiveRecord
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
if options[:if_exists] && @version_year < 2016
|
30
|
-
execute "IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = #{quote(table_name)}) DROP TABLE #{quote_table_name(table_name)}"
|
30
|
+
execute "IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = #{quote(table_name)}) DROP TABLE #{quote_table_name(table_name)}", "SCHEMA"
|
31
31
|
else
|
32
32
|
super
|
33
33
|
end
|
@@ -51,7 +51,7 @@ module ActiveRecord
|
|
51
51
|
index[:index_keys].split(",").each do |column|
|
52
52
|
column.strip!
|
53
53
|
|
54
|
-
if column.
|
54
|
+
if column.end_with?("(-)")
|
55
55
|
column.gsub! "(-)", ""
|
56
56
|
orders[column] = :desc
|
57
57
|
end
|
@@ -130,8 +130,9 @@ module ActiveRecord
|
|
130
130
|
rename_table_indexes(table_name, new_name)
|
131
131
|
end
|
132
132
|
|
133
|
-
def remove_column(table_name, column_name, type = nil, options
|
133
|
+
def remove_column(table_name, column_name, type = nil, **options)
|
134
134
|
raise ArgumentError.new("You must specify at least one column name. Example: remove_column(:people, :first_name)") if column_name.is_a? Array
|
135
|
+
return if options[:if_exists] == true && !column_exists?(table_name, column_name)
|
135
136
|
|
136
137
|
remove_check_constraints(table_name, column_name)
|
137
138
|
remove_default_constraint(table_name, column_name)
|
@@ -156,6 +157,7 @@ module ActiveRecord
|
|
156
157
|
end
|
157
158
|
sql_commands << "UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote_default_expression(options[:default], column_object)} WHERE #{quote_column_name(column_name)} IS NULL" if !options[:null].nil? && options[:null] == false && !options[:default].nil?
|
158
159
|
alter_command = "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} #{type_to_sql(type, limit: options[:limit], precision: options[:precision], scale: options[:scale])}"
|
160
|
+
alter_command += " COLLATE #{options[:collation]}" if options[:collation].present?
|
159
161
|
alter_command += " NOT NULL" if !options[:null].nil? && options[:null] == false
|
160
162
|
sql_commands << alter_command
|
161
163
|
if without_constraints
|
@@ -190,7 +192,7 @@ module ActiveRecord
|
|
190
192
|
end
|
191
193
|
|
192
194
|
def rename_index(table_name, old_name, new_name)
|
193
|
-
raise ArgumentError, "Index name '#{new_name}' on table '#{table_name}' is too long; the limit is #{
|
195
|
+
raise ArgumentError, "Index name '#{new_name}' on table '#{table_name}' is too long; the limit is #{index_name_length} characters" if new_name.length > index_name_length
|
194
196
|
|
195
197
|
identifier = SQLServer::Utils.extract_identifiers("#{table_name}.#{old_name}")
|
196
198
|
execute_procedure :sp_rename, identifier.quoted, new_name, "INDEX"
|
@@ -330,7 +332,7 @@ module ActiveRecord
|
|
330
332
|
def initialize_native_database_types
|
331
333
|
{
|
332
334
|
primary_key: "bigint NOT NULL IDENTITY(1,1) PRIMARY KEY",
|
333
|
-
primary_key_nonclustered: "
|
335
|
+
primary_key_nonclustered: "bigint NOT NULL IDENTITY(1,1) PRIMARY KEY NONCLUSTERED",
|
334
336
|
integer: { name: "int", limit: 4 },
|
335
337
|
bigint: { name: "bigint" },
|
336
338
|
boolean: { name: "bit" },
|