Skip to content

Commit 9bbf466

Browse files
committed
Unique constraint in Migrations
1 parent 54dc8a2 commit 9bbf466

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,60 @@ public void DropForeignKey()
725725
Assert.AreEqual(NewLineEnd(@"ALTER TABLE ""People"" DROP CONSTRAINT ""FK_People_Principal"";"), batch[0].CommandText);
726726
}
727727

728+
[Test]
729+
public void AddUniqueConstraintOneColumn()
730+
{
731+
var operation = new AddUniqueConstraintOperation()
732+
{
733+
Table = "People",
734+
Name = "UNQ_People_Foo",
735+
Columns = new[] { "Foo" },
736+
};
737+
var batch = Generate(new[] { operation });
738+
Assert.AreEqual(1, batch.Count());
739+
Assert.AreEqual(NewLineEnd(@"ALTER TABLE ""People"" ADD CONSTRAINT ""UNQ_People_Foo"" UNIQUE (""Foo"");"), batch[0].CommandText);
740+
}
741+
742+
[Test]
743+
public void AddUniqueConstraintTwoColumns()
744+
{
745+
var operation = new AddUniqueConstraintOperation()
746+
{
747+
Table = "People",
748+
Name = "UNQ_People_Foo_Bar",
749+
Columns = new[] { "Foo", "Bar" },
750+
};
751+
var batch = Generate(new[] { operation });
752+
Assert.AreEqual(1, batch.Count());
753+
Assert.AreEqual(NewLineEnd(@"ALTER TABLE ""People"" ADD CONSTRAINT ""UNQ_People_Foo_Bar"" UNIQUE (""Foo"", ""Bar"");"), batch[0].CommandText);
754+
}
755+
756+
[Test]
757+
public void AddUniqueConstraintNoName()
758+
{
759+
var operation = new AddUniqueConstraintOperation()
760+
{
761+
Table = "People",
762+
Columns = new[] { "Foo" },
763+
};
764+
var batch = Generate(new[] { operation });
765+
Assert.AreEqual(1, batch.Count());
766+
Assert.AreEqual(NewLineEnd(@"ALTER TABLE ""People"" ADD UNIQUE (""Foo"");"), batch[0].CommandText);
767+
}
768+
769+
[Test]
770+
public void DropUniqueConstraint()
771+
{
772+
var operation = new DropUniqueConstraintOperation()
773+
{
774+
Table = "People",
775+
Name = "UNQ_People_Foo",
776+
};
777+
var batch = Generate(new[] { operation });
778+
Assert.AreEqual(1, batch.Count());
779+
Assert.AreEqual(NewLineEnd(@"ALTER TABLE ""People"" DROP CONSTRAINT ""UNQ_People_Foo"";"), batch[0].CommandText);
780+
}
781+
728782
IReadOnlyList<MigrationCommand> Generate(IReadOnlyList<MigrationOperation> operations)
729783
{
730784
using (var db = GetDbContext<FbTestDbContext>())

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,14 @@ protected override void Generate(AddForeignKeyOperation operation, IModel model,
237237
=> base.Generate(operation, model, builder);
238238

239239
protected override void Generate(DropForeignKeyOperation operation, IModel model, MigrationCommandListBuilder builder)
240-
{
241-
base.Generate(operation, model, builder);
242-
}
240+
=> base.Generate(operation, model, builder);
241+
242+
243+
protected override void Generate(AddUniqueConstraintOperation operation, IModel model, MigrationCommandListBuilder builder)
244+
=> base.Generate(operation, model, builder);
245+
246+
protected override void Generate(DropUniqueConstraintOperation operation, IModel model, MigrationCommandListBuilder builder)
247+
=> base.Generate(operation, model, builder);
243248

244249

245250
protected override void ColumnDefinition(string schema, string table, string name, Type clrType, string type, bool? unicode, int? maxLength, bool? fixedLength, bool rowVersion, bool nullable, object defaultValue, string defaultValueSql, string computedColumnSql, IAnnotatable annotatable, IModel model, MigrationCommandListBuilder builder)

0 commit comments

Comments
 (0)