public void Upgrade(IRepository repository, int version) { int dbVersion = GetDbVersion(repository); if (dbVersion >= version) { // Already up higher than this so do nothing. return; } int highestAvailable = repository.GetVersion(); if (version > highestAvailable) { throw new SectorException("Version requested higher than latest available"); } if (!repository.HasVersion(version)) { throw new SectorException("Version requested not available in the repository"); } int steps = version - dbVersion; foreach (var upVersion in Enumerable.Range(dbVersion + 1, steps)) { using (var transaction = sectorDb.Connection.BeginTransaction()) { using (var sqlCommand = sectorDb.Connection.CreateCommand()) { // Run the SQL for the next version. sqlCommand.CommandText = repository.GetUpgradeSql(upVersion); sqlCommand.ExecuteNonQuery(); } // Now update the version table. using (var sqlCommand = sectorDb.Connection.CreateCommand()) { // Upgrade the version info. const string templ = "UPDATE {0} SET version = @RepoVer WHERE repository_id = @RepoId"; sqlCommand.CommandText = string.Format(templ, SectorDb.TableName); { var param = sqlCommand.CreateParameter(); param.DbType = DbType.String; param.ParameterName = "@RepoId"; param.Value = repository.RepositoryId; sqlCommand.Parameters.Add(param); } { var param = sqlCommand.CreateParameter(); param.DbType = DbType.Int32; param.ParameterName = "@RepoVer"; param.Value = upVersion; sqlCommand.Parameters.Add(param); } sqlCommand.ExecuteNonQuery(); } transaction.Commit(); } } }
public void Upgrade(IRepository repository) { int version = repository.GetVersion(); Upgrade(repository, version); }