Skip to content

Commit 02c9f07

Browse files
authored
NH-4022 - Fix dropping sequences in MsSql2012Dialect (nhibernate#647)
1 parent 40b5f39 commit 02c9f07

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

src/NHibernate.Test/DialectTest/MsSql2012DialectFixture.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ SELECT DISTINCT TOP (?) PERCENT WITH TIES @id = id FROM persons WHERE name LIKE
155155
EXEC get_person_summary @id");
156156
}
157157

158+
[Test]
159+
public void GetDropSequenceString()
160+
{
161+
var d = new MsSql2012Dialect();
162+
163+
Assert.That(d.GetDropSequenceString("[foo].[bar_seq]"), Is.EqualTo("IF EXISTS (SELECT * FROM sys.sequences WHERE object_id = OBJECT_ID(N'[foo].[bar_seq]')) DROP SEQUENCE [foo].[bar_seq]"));
164+
}
165+
158166
private static void VerifyLimitStringForStoredProcedureCalls(string sql)
159167
{
160168
var d = new MsSql2012Dialect();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace NHibernate.Test.NHSpecificTest.NH4004
2+
{
3+
class Entity
4+
{
5+
public virtual int Id { get; set; }
6+
public virtual string Name { get; set; }
7+
}
8+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System.Data;
2+
using NHibernate.Cfg.MappingSchema;
3+
using NHibernate.Dialect;
4+
using NHibernate.Mapping.ByCode;
5+
using NHibernate.Tool.hbm2ddl;
6+
using NUnit.Framework;
7+
8+
namespace NHibernate.Test.NHSpecificTest.NH4004
9+
{
10+
public class Fixture : TestCaseMappingByCode
11+
{
12+
protected override bool AppliesTo(Dialect.Dialect dialect)
13+
{
14+
return dialect is MsSql2012Dialect;
15+
}
16+
17+
protected override HbmMapping GetMappings()
18+
{
19+
var mapper = new ModelMapper();
20+
mapper.Class<Entity>(
21+
rc =>
22+
{
23+
rc.Id(
24+
x => x.Id,
25+
m => m.Generator(
26+
Generators.Sequence,
27+
gm => gm.Params(
28+
new
29+
{
30+
sequence = "`entity_seq`"
31+
}
32+
)));
33+
rc.Property(x => x.Name);
34+
});
35+
36+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
37+
}
38+
39+
protected override bool CheckDatabaseWasCleaned()
40+
{
41+
//NO OP
42+
return true;
43+
}
44+
45+
[Test]
46+
public void SequenceShallBeDropped()
47+
{
48+
new SchemaExport(cfg).Drop(true, true);
49+
50+
using (var connection = Sfi.ConnectionProvider.GetConnection())
51+
{
52+
var command = connection.CreateCommand();
53+
command.CommandText = "SELECT COUNT(*) FROM sys.sequences WHERE NAME = 'entity_seq'";
54+
command.CommandType = CommandType.Text;
55+
var count = (int) command.ExecuteScalar();
56+
57+
Assert.That(count, Is.EqualTo(0));
58+
}
59+
}
60+
}
61+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,8 @@
751751
<Compile Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\FooExample.cs" />
752752
<Compile Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\IExample.cs" />
753753
<Compile Include="Insertordering\NH3931Entities.cs" />
754+
<Compile Include="NHSpecificTest\NH4004\Entity.cs" />
755+
<Compile Include="NHSpecificTest\NH4004\Fixture.cs" />
754756
<Compile Include="NHSpecificTest\NH1904\StructFixture.cs" />
755757
<Compile Include="NHSpecificTest\NH3985\Entity.cs" />
756758
<Compile Include="NHSpecificTest\NH3985\Fixture.cs" />

src/NHibernate/Dialect/MsSql2012Dialect.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected override string GetCreateSequenceString(string sequenceName, int initi
3030

3131
public override string GetDropSequenceString(string sequenceName)
3232
{
33-
string dropSequence = "IF EXISTS (select * from sys.sequences where name = N'{0}') DROP SEQUENCE {0}";
33+
string dropSequence = "IF EXISTS (SELECT * FROM sys.sequences WHERE object_id = OBJECT_ID(N'{0}')) DROP SEQUENCE {0}";
3434

3535
return string.Format(dropSequence, sequenceName);
3636
}

0 commit comments

Comments
 (0)