Skip to content

Commit 7ce79ed

Browse files
Fix SQLite/SqlCE validation failure on tables with a schema (#1790)
1 parent af9021d commit 7ce79ed

File tree

8 files changed

+371
-19
lines changed

8 files changed

+371
-19
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System;
12+
using NHibernate.Dialect;
13+
using NHibernate.Util;
14+
using NUnit.Framework;
15+
16+
namespace NHibernate.Test.Tools.hbm2ddl.SchemaValidator
17+
{
18+
using System.Threading.Tasks;
19+
[TestFixture]
20+
public class SchemaValidateTableWithSchemaFixtureAsync : TestCase
21+
{
22+
protected override string MappingsAssembly => "NHibernate.Test";
23+
24+
protected override string[] Mappings => new[] { "Tools.hbm2ddl.SchemaValidator.VersionWithSchema.hbm.xml" };
25+
26+
protected override bool AppliesTo(Dialect.Dialect dialect)
27+
{
28+
switch (Dialect)
29+
{
30+
case MsSql2000Dialect _:
31+
case PostgreSQLDialect _:
32+
case SQLiteDialect _:
33+
case MsSqlCeDialect _:
34+
return true;
35+
default:
36+
// Firebird does not support schema. Its current dialect leave table name being schema prefixed,
37+
// which causes SQL parse errors. It has a "create schema" command but instead creates a new
38+
// database.
39+
// MySql does not truly support schema. Its current dialect leave table name being schema prefixed,
40+
// which is interpreted as a database name. It has a "create schema" command but instead creates
41+
// a new database.
42+
// Oracle tightly bounds schema to users.
43+
return false;
44+
}
45+
}
46+
47+
protected override void CreateSchema()
48+
{
49+
switch (Dialect)
50+
{
51+
case MsSql2000Dialect _:
52+
case PostgreSQLDialect _:
53+
// Must handle the schema manually: mapped database-objects are handled too late.
54+
var cnx = Sfi.ConnectionProvider.GetConnection();
55+
try
56+
{
57+
using (var cmd = cnx.CreateCommand())
58+
{
59+
cmd.CommandText = "create schema Test";
60+
cmd.ExecuteNonQuery();
61+
}
62+
}
63+
catch (Exception ex)
64+
{
65+
// Unfortunateley Assert.Warn and Console.WriteLine at this place seems to be ignored in Rider
66+
// viewer.
67+
Assert.Warn("Creating the schema failed, assuming it already exists. {0}", ex);
68+
Console.WriteLine("Creating the schema failed, assuming it already exists.");
69+
Console.WriteLine(ex);
70+
}
71+
finally
72+
{
73+
Sfi.ConnectionProvider.CloseConnection(cnx);
74+
}
75+
break;
76+
}
77+
base.CreateSchema();
78+
}
79+
80+
protected override void DropSchema()
81+
{
82+
// SQL-Server does not need this call, but Postgres does not accept dropping a schema carrying objects.
83+
base.DropSchema();
84+
85+
switch (Dialect)
86+
{
87+
case MsSql2000Dialect _:
88+
case PostgreSQLDialect _:
89+
var cnx = Sfi.ConnectionProvider.GetConnection();
90+
try
91+
{
92+
using (var cmd = cnx.CreateCommand())
93+
{
94+
cmd.CommandText = "drop schema Test";
95+
cmd.ExecuteNonQuery();
96+
}
97+
}
98+
finally
99+
{
100+
Sfi.ConnectionProvider.CloseConnection(cnx);
101+
}
102+
break;
103+
}
104+
}
105+
106+
[Test]
107+
public async Task ShouldVerifyAsync()
108+
{
109+
var validator = new Tool.hbm2ddl.SchemaValidator(cfg);
110+
try
111+
{
112+
await (validator.ValidateAsync());
113+
}
114+
catch (SchemaValidationException sve)
115+
{
116+
Assert.Fail("Validation failed: {0}.\n{1}", StringHelper.CollectionToString(sve.ValidationErrors), sve);
117+
}
118+
}
119+
}
120+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System;
2+
using NHibernate.Dialect;
3+
using NHibernate.Util;
4+
using NUnit.Framework;
5+
6+
namespace NHibernate.Test.Tools.hbm2ddl.SchemaValidator
7+
{
8+
[TestFixture]
9+
public class SchemaValidateTableWithSchemaFixture : TestCase
10+
{
11+
protected override string MappingsAssembly => "NHibernate.Test";
12+
13+
protected override string[] Mappings => new[] { "Tools.hbm2ddl.SchemaValidator.VersionWithSchema.hbm.xml" };
14+
15+
protected override bool AppliesTo(Dialect.Dialect dialect)
16+
{
17+
switch (Dialect)
18+
{
19+
case MsSql2000Dialect _:
20+
case PostgreSQLDialect _:
21+
case SQLiteDialect _:
22+
case MsSqlCeDialect _:
23+
return true;
24+
default:
25+
// Firebird does not support schema. Its current dialect leave table name being schema prefixed,
26+
// which causes SQL parse errors. It has a "create schema" command but instead creates a new
27+
// database.
28+
// MySql does not truly support schema. Its current dialect leave table name being schema prefixed,
29+
// which is interpreted as a database name. It has a "create schema" command but instead creates
30+
// a new database.
31+
// Oracle tightly bounds schema to users.
32+
return false;
33+
}
34+
}
35+
36+
protected override void CreateSchema()
37+
{
38+
switch (Dialect)
39+
{
40+
case MsSql2000Dialect _:
41+
case PostgreSQLDialect _:
42+
// Must handle the schema manually: mapped database-objects are handled too late.
43+
var cnx = Sfi.ConnectionProvider.GetConnection();
44+
try
45+
{
46+
using (var cmd = cnx.CreateCommand())
47+
{
48+
cmd.CommandText = "create schema Test";
49+
cmd.ExecuteNonQuery();
50+
}
51+
}
52+
catch (Exception ex)
53+
{
54+
// Unfortunateley Assert.Warn and Console.WriteLine at this place seems to be ignored in Rider
55+
// viewer.
56+
Assert.Warn("Creating the schema failed, assuming it already exists. {0}", ex);
57+
Console.WriteLine("Creating the schema failed, assuming it already exists.");
58+
Console.WriteLine(ex);
59+
}
60+
finally
61+
{
62+
Sfi.ConnectionProvider.CloseConnection(cnx);
63+
}
64+
break;
65+
}
66+
base.CreateSchema();
67+
}
68+
69+
protected override void DropSchema()
70+
{
71+
// SQL-Server does not need this call, but Postgres does not accept dropping a schema carrying objects.
72+
base.DropSchema();
73+
74+
switch (Dialect)
75+
{
76+
case MsSql2000Dialect _:
77+
case PostgreSQLDialect _:
78+
var cnx = Sfi.ConnectionProvider.GetConnection();
79+
try
80+
{
81+
using (var cmd = cnx.CreateCommand())
82+
{
83+
cmd.CommandText = "drop schema Test";
84+
cmd.ExecuteNonQuery();
85+
}
86+
}
87+
finally
88+
{
89+
Sfi.ConnectionProvider.CloseConnection(cnx);
90+
}
91+
break;
92+
}
93+
}
94+
95+
[Test]
96+
public void ShouldVerify()
97+
{
98+
var validator = new Tool.hbm2ddl.SchemaValidator(cfg);
99+
try
100+
{
101+
validator.Validate();
102+
}
103+
catch (SchemaValidationException sve)
104+
{
105+
Assert.Fail("Validation failed: {0}.\n{1}", StringHelper.CollectionToString(sve.ValidationErrors), sve);
106+
}
107+
}
108+
}
109+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
namespace="NHibernate.Test.Tools.hbm2ddl.SchemaValidator"
4+
assembly="NHibernate.Test">
5+
6+
<class name="Version" schema="Test">
7+
<id name="Id">
8+
<generator class="NHibernate.Id.TableHiLoGenerator">
9+
<param name="table">uid_table</param>
10+
<param name="column">next_hi_value_column</param>
11+
</generator>
12+
</id>
13+
<property name="Description"/>
14+
<many-to-one name="Previous"/>
15+
</class>
16+
</hibernate-mapping>
17+

src/NHibernate/Dialect/MsSqlCeDialect.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public override bool SupportsLimitOffset
263263

264264
public override IDataBaseSchema GetDataBaseSchema(DbConnection connection)
265265
{
266-
return new MsSqlCeDataBaseSchema(connection);
266+
return new MsSqlCeDataBaseSchema(connection, this);
267267
}
268268

269269
public override SqlString GetLimitString(SqlString querySqlString, SqlString offset, SqlString limit)

src/NHibernate/Dialect/SQLiteDialect.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ protected virtual void RegisterDefaultProperties()
190190

191191
public override Schema.IDataBaseSchema GetDataBaseSchema(DbConnection connection)
192192
{
193-
return new Schema.SQLiteDataBaseMetaData(connection);
193+
return new Schema.SQLiteDataBaseMetaData(connection, this);
194194
}
195195

196196
public override string AddColumnString

0 commit comments

Comments
 (0)