66using System . Reflection ;
77using log4net ;
88
9- namespace Coderr . Server . SqlServer
9+ namespace Coderr . Server . SqlServer . Migrations
1010{
1111 public class SchemaManager
1212 {
1313 private readonly Func < IDbConnection > _connectionFactory ;
14- private const string SchemaNamespace = "Coderr.Server.SqlServer.Schema" ;
1514 private ILog _logger = LogManager . GetLogger ( typeof ( SchemaManager ) ) ;
15+ private readonly MigrationScripts _migrationScripts = new MigrationScripts ( ) ;
16+ private string [ ] _scriptOrder = { "Update" } ;
17+
18+ private bool invoked ;
1619
1720 public SchemaManager ( Func < IDbConnection > connectionFactory )
1821 {
1922 _connectionFactory = connectionFactory ;
2023 }
2124
2225 /// <summary>
23- /// Check if the current DB schema is out of date compared to the embedded schema resources.
26+ /// Check if the current DB schema is out of date compared to the embedded schema resources.
2427 /// </summary>
2528 public bool CanSchemaBeUpgraded ( )
2629 {
@@ -29,16 +32,14 @@ public bool CanSchemaBeUpgraded()
2932 return embeddedSchema > version ;
3033 }
3134
32- private bool invoked = false ;
33-
3435 public void CreateInitialStructure ( )
3536 {
3637 if ( invoked )
3738 throw new InvalidOperationException ( "Invoked" ) ;
3839 invoked = true ;
3940 using ( var con = _connectionFactory ( ) )
4041 {
41- var resourceName = $ "{ SchemaNamespace } .Database.sql";
42+ var resourceName = $ "{ MigrationScripts . SchemaNamespace } .Database.sql";
4243 var res = Assembly . GetExecutingAssembly ( ) . GetManifestResourceStream ( resourceName ) ;
4344 var sql = new StreamReader ( res ) . ReadToEnd ( ) ;
4445 using ( var transaction = con . BeginTransaction ( ) )
@@ -65,7 +66,7 @@ public int GetCurrentSchemaVersion()
6566 var sql = "SELECT Version FROM DatabaseSchema" ;
6667 cmd . CommandText = sql ;
6768 var result = cmd . ExecuteScalar ( ) ;
68- version = ( int ) result ;
69+ version = ( int ) result ;
6970 }
7071 catch ( SqlException ex )
7172 {
@@ -77,6 +78,7 @@ public int GetCurrentSchemaVersion()
7778 }
7879 }
7980 }
81+
8082 return version ;
8183 }
8284
@@ -86,21 +88,25 @@ public int GetLatestSchemaVersion()
8688 var names =
8789 Assembly . GetExecutingAssembly ( )
8890 . GetManifestResourceNames ( )
89- . Where ( x => x . StartsWith ( SchemaNamespace ) && x . Contains ( ".Update. " ) ) ;
91+ . Where ( x => x . StartsWith ( MigrationScripts . SchemaNamespace ) && x . Contains ( ".v " ) ) ;
9092 foreach ( var name in names )
9193 {
92- var pos = name . IndexOf ( "Update " ) + 8 ; //2 extra for ".v"
94+ var pos = name . IndexOf ( ".v " ) + 2 ; //2 extra for ".v"
9395 var endPos = name . IndexOf ( "." , pos ) ;
9496 var versionStr = name . Substring ( pos , endPos - pos ) ;
9597 var version = int . Parse ( versionStr ) ;
98+
9699 if ( version > highestVersion )
97100 highestVersion = version ;
101+
102+ _migrationScripts . AddScript ( version , name ) ;
98103 }
104+
99105 return highestVersion ;
100106 }
101107
102108 /// <summary>
103- /// Upgrade schema
109+ /// Upgrade schema
104110 /// </summary>
105111 /// <param name="toVersion">-1 = latest version</param>
106112 public void UpgradeDatabaseSchema ( int toVersion = - 1 )
@@ -113,29 +119,22 @@ public void UpgradeDatabaseSchema(int toVersion = -1)
113119
114120 for ( var version = currentSchema + 1 ; version <= toVersion ; version ++ )
115121 {
116- var schema = GetSchema ( version ) ;
122+ var migrationScripts = _migrationScripts . GetScripts ( version ) ;
117123 using ( var con = _connectionFactory ( ) )
118124 {
119- using ( var transaction = con . BeginTransaction ( ) )
120- using ( var cmd = con . CreateCommand ( ) )
125+ foreach ( var versionScript in migrationScripts )
121126 {
122- cmd . Transaction = transaction ;
123- cmd . CommandText = schema ;
124- cmd . ExecuteNonQuery ( ) ;
125- transaction . Commit ( ) ;
127+ using ( var transaction = con . BeginTransaction ( ) )
128+ using ( var cmd = con . CreateCommand ( ) )
129+ {
130+ cmd . Transaction = transaction ;
131+ cmd . CommandText = versionScript ;
132+ cmd . ExecuteNonQuery ( ) ;
133+ transaction . Commit ( ) ;
134+ }
126135 }
127136 }
128137 }
129138 }
130-
131- private string GetSchema ( int version )
132- {
133- var resourceName = $ "{ SchemaNamespace } .Update.v{ version } .sql";
134- var res = Assembly . GetExecutingAssembly ( ) . GetManifestResourceStream ( resourceName ) ;
135- if ( res == null )
136- throw new InvalidOperationException ( "Failed to find schema " + resourceName ) ;
137-
138- return new StreamReader ( res ) . ReadToEnd ( ) ;
139- }
140139 }
141140}
0 commit comments