13
13
using System . Reflection ;
14
14
using NHibernate . Cfg ;
15
15
using NHibernate . Driver ;
16
+ using NHibernate . Engine ;
17
+ using NHibernate . Exceptions ;
16
18
using NHibernate . Tool . hbm2ddl ;
17
19
using NHibernate . Util ;
18
20
using NUnit . Framework ;
@@ -25,18 +27,36 @@ namespace NHibernate.Test.Tools.hbm2ddl.SchemaUpdate
25
27
[ TestFixture ]
26
28
public class MigrationFixtureAsync
27
29
{
28
- private async Task MigrateSchemaAsync ( string resource1 , string resource2 , CancellationToken cancellationToken = default ( CancellationToken ) )
30
+ private Configuration _configurationToDrop ;
31
+ private FirebirdClientDriver _fireBirdDriver ;
32
+
33
+ [ OneTimeSetUp ]
34
+ public void OneTimeSetup ( )
29
35
{
30
- Configuration v1cfg = TestConfigurationHelper . GetDefaultConfiguration ( ) ;
31
- var driverClass = ReflectHelper . ClassForName ( v1cfg . GetProperty ( Environment . ConnectionDriver ) ) ;
36
+ var cfg = TestConfigurationHelper . GetDefaultConfiguration ( ) ;
37
+ var driverClass = ReflectHelper . ClassForName ( cfg . GetProperty ( Environment . ConnectionDriver ) ) ;
32
38
// Odbc is not supported by schema update: System.Data.Odbc.OdbcConnection.GetSchema("ForeignKeys") fails with an ArgumentException: ForeignKeys is undefined.
33
39
// It seems it would require its own DataBaseSchema, but this is bound to the dialect, not the driver.
34
40
if ( typeof ( OdbcDriver ) . IsAssignableFrom ( driverClass ) )
35
41
Assert . Ignore ( "Test is not compatible with ODBC" ) ;
36
42
37
- using ( Stream stream = Assembly . GetExecutingAssembly ( ) . GetManifestResourceStream ( resource1 ) )
38
- v1cfg . AddInputStream ( stream ) ;
39
- await ( new SchemaExport ( v1cfg ) . ExecuteAsync ( false , true , true , cancellationToken ) ) ;
43
+ if ( typeof ( FirebirdClientDriver ) . IsAssignableFrom ( driverClass ) )
44
+ _fireBirdDriver = new FirebirdClientDriver ( ) ;
45
+ }
46
+
47
+ [ TearDown ]
48
+ public void TearDown ( )
49
+ {
50
+ if ( _configurationToDrop != null )
51
+ DropSchema ( _configurationToDrop ) ;
52
+ _configurationToDrop = null ;
53
+ }
54
+
55
+ private async Task MigrateSchemaAsync ( string resource1 , string resource2 , CancellationToken cancellationToken = default ( CancellationToken ) )
56
+ {
57
+ var v1cfg = GetConfigurationForMapping ( resource1 ) ;
58
+ await ( DropSchemaAsync ( v1cfg , cancellationToken ) ) ;
59
+ _configurationToDrop = v1cfg ;
40
60
41
61
Tool . hbm2ddl . SchemaUpdate v1schemaUpdate = new Tool . hbm2ddl . SchemaUpdate ( v1cfg ) ;
42
62
await ( v1schemaUpdate . ExecuteAsync ( true , true , cancellationToken ) ) ;
@@ -46,9 +66,7 @@ public class MigrationFixtureAsync
46
66
47
67
Assert . AreEqual ( 0 , v1schemaUpdate . Exceptions . Count ) ;
48
68
49
- Configuration v2cfg = TestConfigurationHelper . GetDefaultConfiguration ( ) ;
50
- using ( Stream stream = Assembly . GetExecutingAssembly ( ) . GetManifestResourceStream ( resource2 ) )
51
- v2cfg . AddInputStream ( stream ) ;
69
+ var v2cfg = GetConfigurationForMapping ( resource2 ) ;
52
70
53
71
Tool . hbm2ddl . SchemaUpdate v2schemaUpdate = new Tool . hbm2ddl . SchemaUpdate ( v2cfg ) ;
54
72
await ( v2schemaUpdate . ExecuteAsync ( true , true , cancellationToken ) ) ;
@@ -76,5 +94,90 @@ public async Task SimpleColumnReplaceAsync()
76
94
77
95
await ( MigrateSchemaAsync ( resource1 , resource2 ) ) ;
78
96
}
97
+
98
+ [ Test ]
99
+ public async Task AutoUpdateFailuresAreThrownAsync ( )
100
+ {
101
+ var cfg1 = GetConfigurationForMapping ( "NHibernate.Test.Tools.hbm2ddl.SchemaUpdate.1_Person.hbm.xml" ) ;
102
+ var sf1 = cfg1 . BuildSessionFactory ( ) ;
103
+ var dialect = Dialect . Dialect . GetDialect ( cfg1 . Properties ) ;
104
+ if ( ! dialect . SupportsUnique )
105
+ Assert . Ignore ( "This test requires a dialect supporting unique constraints" ) ;
106
+
107
+ _configurationToDrop = cfg1 ;
108
+ await ( CreateSchemaAsync ( cfg1 ) ) ;
109
+
110
+ using ( var s = sf1 . OpenSession ( ) )
111
+ using ( var t = s . BeginTransaction ( ) )
112
+ {
113
+ await ( s . SaveAsync ( new Person ( ) ) ) ;
114
+ await ( s . SaveAsync ( new Person ( ) ) ) ;
115
+ await ( t . CommitAsync ( ) ) ;
116
+ }
117
+
118
+ // This schema switches to not-nullable the person properties, which should fail due to an existing person with null properties.
119
+ var cfg2 = GetConfigurationForMapping ( "NHibernate.Test.Tools.hbm2ddl.SchemaUpdate.3_Person.hbm.xml" ) ;
120
+ cfg2 . Properties [ Environment . Hbm2ddlAuto ] = SchemaAutoAction . Update . ToString ( ) ;
121
+ cfg2 . Properties [ Environment . Hbm2ddlThrowOnUpdate ] = "true" ;
122
+ Assert . That ( ( ) => cfg2 . BuildSessionFactory ( ) , Throws . InstanceOf < AggregateHibernateException > ( ) ) ;
123
+ }
124
+
125
+ private Configuration GetConfigurationForMapping ( string resourcePath )
126
+ {
127
+ var cfg = TestConfigurationHelper . GetDefaultConfiguration ( ) ;
128
+ using ( var stream = Assembly . GetExecutingAssembly ( ) . GetManifestResourceStream ( resourcePath ) )
129
+ cfg . AddInputStream ( stream ) ;
130
+ return cfg ;
131
+ }
132
+
133
+ private Task CreateSchemaAsync ( Configuration cfg , CancellationToken cancellationToken = default ( CancellationToken ) )
134
+ {
135
+ try
136
+ {
137
+ // Firebird will pool each connection created during the test and will marked as used any table
138
+ // referenced by queries. It will at best delays those tables drop until connections are actually
139
+ // closed, or immediately fail dropping them.
140
+ // This results in other tests failing when they try to create tables with the same name.
141
+ // By clearing the connection pool the tables will get dropped.
142
+ _fireBirdDriver ? . ClearPool ( null ) ;
143
+
144
+ return new SchemaExport ( cfg ) . CreateAsync ( false , true , cancellationToken ) ;
145
+ }
146
+ catch ( Exception ex )
147
+ {
148
+ return Task . FromException < object > ( ex ) ;
149
+ }
150
+ }
151
+
152
+ private Task DropSchemaAsync ( Configuration cfg , CancellationToken cancellationToken = default ( CancellationToken ) )
153
+ {
154
+ try
155
+ {
156
+ // Firebird will pool each connection created during the test and will marked as used any table
157
+ // referenced by queries. It will at best delays those tables drop until connections are actually
158
+ // closed, or immediately fail dropping them.
159
+ // This results in other tests failing when they try to create tables with the same name.
160
+ // By clearing the connection pool the tables will get dropped.
161
+ _fireBirdDriver ? . ClearPool ( null ) ;
162
+
163
+ return new SchemaExport ( cfg ) . DropAsync ( false , true , cancellationToken ) ;
164
+ }
165
+ catch ( Exception ex )
166
+ {
167
+ return Task . FromException < object > ( ex ) ;
168
+ }
169
+ }
170
+
171
+ private void DropSchema ( Configuration cfg )
172
+ {
173
+ // Firebird will pool each connection created during the test and will marked as used any table
174
+ // referenced by queries. It will at best delays those tables drop until connections are actually
175
+ // closed, or immediately fail dropping them.
176
+ // This results in other tests failing when they try to create tables with the same name.
177
+ // By clearing the connection pool the tables will get dropped.
178
+ _fireBirdDriver ? . ClearPool ( null ) ;
179
+
180
+ new SchemaExport ( cfg ) . Drop ( false , true ) ;
181
+ }
79
182
}
80
- }
183
+ }
0 commit comments