Skip to content

Commit 9e9d3d2

Browse files
committed
Support for collation on columns (part of #1050).
1 parent f171d2d commit 9e9d3d2

2 files changed

Lines changed: 74 additions & 3 deletions

File tree

src/FirebirdSql.EntityFrameworkCore.Firebird.Tests/Migrations/MigrationsTests.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ public async Task CreateTable()
9595
DefaultValueSql = "'x'",
9696
IsNullable = true,
9797
},
98+
new AddColumnOperation
99+
{
100+
Name = "COLLA",
101+
Table = "People",
102+
ClrType = typeof(string),
103+
MaxLength = 20,
104+
IsNullable = true,
105+
Collation = "UNICODE_CI_AI"
106+
},
98107
},
99108
PrimaryKey = new AddPrimaryKeyOperation
100109
{
@@ -125,6 +134,7 @@ public async Task CreateTable()
125134
""SSN"" char(11),
126135
""DEF_O"" VARCHAR(20) DEFAULT _UTF8'test',
127136
""DEF_S"" VARCHAR(20) DEFAULT 'x',
137+
""COLLA"" VARCHAR(20) COLLATE UNICODE_CI_AI,
128138
PRIMARY KEY (""Id""),
129139
UNIQUE (""SSN""),
130140
FOREIGN KEY (""EmployerId"") REFERENCES ""Companies"" (""Id"") ON UPDATE NO ACTION ON DELETE NO ACTION
@@ -201,6 +211,15 @@ public async Task CreateTableScript()
201211
DefaultValueSql = "'x'",
202212
IsNullable = true,
203213
},
214+
new AddColumnOperation
215+
{
216+
Name = "COLLA",
217+
Table = "People",
218+
ClrType = typeof(string),
219+
MaxLength = 20,
220+
IsNullable = true,
221+
Collation = "UNICODE_CI_AI"
222+
},
204223
},
205224
PrimaryKey = new AddPrimaryKeyOperation
206225
{
@@ -231,6 +250,7 @@ public async Task CreateTableScript()
231250
""SSN"" char(11),
232251
""DEF_O"" VARCHAR(20) DEFAULT _UTF8'test',
233252
""DEF_S"" VARCHAR(20) DEFAULT 'x',
253+
""COLLA"" VARCHAR(20) COLLATE UNICODE_CI_AI,
234254
PRIMARY KEY (""Id""),
235255
UNIQUE (""SSN""),
236256
FOREIGN KEY (""EmployerId"") REFERENCES ""Companies"" (""Id"") ON UPDATE NO ACTION ON DELETE NO ACTION
@@ -272,6 +292,23 @@ public async Task AddColumn()
272292
Assert.AreEqual(NewLineEnd(@"ALTER TABLE ""schema"".""People"" ADD ""NewColumn"" DECIMAL(18,2) NOT NULL;"), batch[0].CommandText);
273293
}
274294

295+
[Test]
296+
public async Task AddColumnWithCollation()
297+
{
298+
var operation = new AddColumnOperation()
299+
{
300+
Table = "People",
301+
Name = "NewColumn",
302+
ClrType = typeof(string),
303+
MaxLength = 10,
304+
IsNullable = false,
305+
Collation = "UNICODE_CI_AI",
306+
};
307+
var batch = await Generate(new[] { operation });
308+
Assert.AreEqual(1, batch.Count());
309+
Assert.AreEqual(NewLineEnd(@"ALTER TABLE ""People"" ADD ""NewColumn"" VARCHAR(10) COLLATE UNICODE_CI_AI NOT NULL;"), batch[0].CommandText);
310+
}
311+
275312
[Test]
276313
public async Task DropColumn()
277314
{
@@ -456,6 +493,29 @@ public async Task AlterColumnRemoveSequenceTrigger()
456493
Assert.AreEqual(NewLineEnd(@"ALTER TABLE ""People"" ALTER COLUMN ""Col"" TYPE INTEGER;"), batch[2].CommandText);
457494
}
458495

496+
[Test]
497+
public async Task AlterColumnCollation()
498+
{
499+
var operation = new AlterColumnOperation()
500+
{
501+
Table = "People",
502+
Name = "Col",
503+
ClrType = typeof(string),
504+
MaxLength = 10,
505+
IsNullable = false,
506+
Collation = "UNICODE_CI_AI",
507+
OldColumn = new AddColumnOperation()
508+
{
509+
ClrType = typeof(string),
510+
MaxLength = 10,
511+
IsNullable = false,
512+
},
513+
};
514+
var batch = await Generate(new[] { operation });
515+
Assert.AreEqual(3, batch.Count());
516+
Assert.AreEqual(NewLineEnd(@"ALTER TABLE ""People"" ALTER COLUMN ""Col"" TYPE VARCHAR(10) COLLATE UNICODE_CI_AI;"), batch[1].CommandText);
517+
}
518+
459519
[Test]
460520
public async Task RenameColumn()
461521
{

src/FirebirdSql.EntityFrameworkCore.Firebird/Migrations/FbMigrationsSqlGenerator.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ protected override void Generate(AlterColumnOperation operation, IModel model, M
127127
var type = GetColumnType(operation.Schema, operation.Table, operation.Name, operation, model);
128128
builder.Append(type);
129129
}
130+
if (operation.Collation != null)
131+
{
132+
builder.Append(" COLLATE ");
133+
builder.Append(operation.Collation);
134+
}
130135
if (valueGenerationStrategy == FbValueGenerationStrategy.IdentityColumn)
131136
{
132137
builder.Append(" GENERATED BY DEFAULT AS IDENTITY");
@@ -323,9 +328,15 @@ public virtual void Generate(FbDropDatabaseOperation operation, IModel model, Mi
323328

324329
protected override void ColumnDefinition(string schema, string table, string name, ColumnOperation operation, IModel model, MigrationCommandListBuilder builder)
325330
{
326-
builder.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(name))
327-
.Append(" ")
328-
.Append(operation.ColumnType ?? GetColumnType(schema, table, name, operation, model));
331+
builder.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(name));
332+
builder.Append(" ");
333+
builder.Append(operation.ColumnType ?? GetColumnType(schema, table, name, operation, model));
334+
335+
if (operation.Collation != null)
336+
{
337+
builder.Append(" COLLATE ");
338+
builder.Append(operation.Collation);
339+
}
329340

330341
var valueGenerationStrategy = operation[FbAnnotationNames.ValueGenerationStrategy] as FbValueGenerationStrategy?;
331342
if (valueGenerationStrategy == FbValueGenerationStrategy.IdentityColumn)

0 commit comments

Comments
 (0)