Skip to content

Commit 7e07687

Browse files
authored
Ensure correct order of COLLATE and NOT NULL in CREATE TABLE statements (#1145)
1 parent c7d2eab commit 7e07687

File tree

3 files changed

+46
-29
lines changed

3 files changed

+46
-29
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Unreleased
2+
3+
#### Fixed
4+
5+
- [#1145](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1145) Ensure correct order of COLLATE and NOT NULL in CREATE TABLE statements
6+
17
## v7.1.0
28

39
#### Added

lib/active_record/connection_adapters/sqlserver/schema_creation.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ def visit_CreateIndexDefinition(o)
5151

5252
def add_column_options!(sql, options)
5353
sql << " DEFAULT #{quote_default_expression(options[:default], options[:column])}" if options_include_default?(options)
54-
if options[:null] == false
55-
sql << " NOT NULL"
56-
end
5754
if options[:collation].present?
5855
sql << " COLLATE #{options[:collation]}"
5956
end
57+
if options[:null] == false
58+
sql << " NOT NULL"
59+
end
6060
if options[:is_identity] == true
6161
sql << " IDENTITY(1,1)"
6262
end

test/cases/active_schema_test_sqlserver.rb

+37-26
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,44 @@
33
require "cases/helper_sqlserver"
44

55
class ActiveSchemaTestSQLServer < ActiveRecord::TestCase
6-
before do
7-
connection.create_table :schema_test_table, force: true, id: false do |t|
8-
t.column :foo, :string, limit: 100
9-
t.column :state, :string
6+
describe "indexes" do
7+
8+
before do
9+
connection.create_table :schema_test_table, force: true, id: false do |t|
10+
t.column :foo, :string, limit: 100
11+
t.column :state, :string
12+
end
1013
end
11-
end
1214

13-
after do
14-
connection.drop_table :schema_test_table rescue nil
15-
end
15+
after do
16+
connection.drop_table :schema_test_table rescue nil
17+
end
1618

17-
it 'default index' do
18-
assert_sql('CREATE INDEX [index_schema_test_table_on_foo] ON [schema_test_table] ([foo])') do
19-
connection.add_index :schema_test_table, "foo"
19+
it 'default index' do
20+
assert_sql('CREATE INDEX [index_schema_test_table_on_foo] ON [schema_test_table] ([foo])') do
21+
connection.add_index :schema_test_table, "foo"
22+
end
2023
end
21-
end
2224

23-
it 'unique index' do
24-
assert_sql('CREATE UNIQUE INDEX [index_schema_test_table_on_foo] ON [schema_test_table] ([foo])') do
25-
connection.add_index :schema_test_table, "foo", unique: true
25+
it 'unique index' do
26+
assert_sql('CREATE UNIQUE INDEX [index_schema_test_table_on_foo] ON [schema_test_table] ([foo])') do
27+
connection.add_index :schema_test_table, "foo", unique: true
28+
end
2629
end
27-
end
2830

29-
it 'where condition on index' do
30-
assert_sql("CREATE INDEX [index_schema_test_table_on_foo] ON [schema_test_table] ([foo]) WHERE state = 'active'") do
31-
connection.add_index :schema_test_table, "foo", where: "state = 'active'"
31+
it 'where condition on index' do
32+
assert_sql("CREATE INDEX [index_schema_test_table_on_foo] ON [schema_test_table] ([foo]) WHERE state = 'active'") do
33+
connection.add_index :schema_test_table, "foo", where: "state = 'active'"
34+
end
3235
end
33-
end
3436

35-
it 'if index does not exist' do
36-
assert_sql("IF NOT EXISTS (SELECT name FROM sysindexes WHERE name = 'index_schema_test_table_on_foo') " \
37-
"CREATE INDEX [index_schema_test_table_on_foo] ON [schema_test_table] ([foo])") do
38-
connection.add_index :schema_test_table, "foo", if_not_exists: true
37+
it 'if index does not exist' do
38+
assert_sql("IF NOT EXISTS (SELECT name FROM sysindexes WHERE name = 'index_schema_test_table_on_foo') " \
39+
"CREATE INDEX [index_schema_test_table_on_foo] ON [schema_test_table] ([foo])") do
40+
connection.add_index :schema_test_table, "foo", if_not_exists: true
41+
end
3942
end
40-
end
4143

42-
describe "index types" do
4344
it 'clustered index' do
4445
assert_sql('CREATE CLUSTERED INDEX [index_schema_test_table_on_foo] ON [schema_test_table] ([foo])') do
4546
connection.add_index :schema_test_table, "foo", type: :clustered
@@ -52,4 +53,14 @@ class ActiveSchemaTestSQLServer < ActiveRecord::TestCase
5253
end
5354
end
5455
end
56+
57+
it "create column with NOT NULL and COLLATE" do
58+
assert_nothing_raised do
59+
connection.create_table :not_null_with_collation_table, force: true, id: false do |t|
60+
t.text :not_null_text_with_collation, null: false, collation: "Latin1_General_CS_AS"
61+
end
62+
end
63+
ensure
64+
connection.drop_table :not_null_with_collation_table rescue nil
65+
end
5566
end

0 commit comments

Comments
 (0)