24 using System.Collections.Generic;
26 using System.Diagnostics.CodeAnalysis;
31 namespace Ntp.Data.Schema
39 changes =
new List<IVersionChanges>();
42 private readonly List<IVersionChanges>
changes;
46 IEnumerator IEnumerable.GetEnumerator()
48 return changes.GetEnumerator();
53 return changes.GetEnumerator();
61 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
65 int databaseVersion = controller.FindCurrentVersion();
67 if (databaseVersion <= 0)
69 var last = changes.OrderBy(
c =>
c.VersionNumber).LastOrDefault();
71 controller.SetCurrentVersion(last);
77 var versionChanges = changes.
78 OrderBy(
c =>
c.VersionNumber).
79 Where(
c =>
c.VersionNumber > databaseVersion);
81 var changeList = versionChanges.ToList();
82 if (changeList.Count == 0)
88 log.ApplySchemaChanges();
90 var connection = factory.CreateConnection();
93 foreach (var
version in changeList)
95 var transaction = connection.BeginTransaction();
98 foreach (var change
in version)
100 var command = factory.CreateCommand();
101 command.Connection = connection;
102 command.CommandText = change.Sql;
103 command.Transaction = transaction;
105 log.SqlExecute(command.CommandText);
106 command.ExecuteNonQuery();
108 transaction.Commit();
112 log.SchemaUpdateError(e);
113 transaction.Rollback();
118 log.SchemaChangesApplied(lastVersion.VersionText);
121 if (connection.State == ConnectionState.Open)
124 controller.SetCurrentVersion(lastVersion);
130 int databaseVersion = controller.FindCurrentVersion();
131 var latest = changes.OrderBy(
c =>
c.VersionNumber).Last();
133 if (databaseVersion == -1)
135 controller.SetCurrentVersion(latest);
139 if (databaseVersion == latest.VersionNumber)
142 log.SchemaVersionError(databaseVersion, latest.VersionNumber);
void Add(IVersionChanges item)
IEnumerator< IVersionChanges > GetEnumerator()
DatabaseUpdater(ISqlFactory factory, LogBase log)
bool CheckLatestVersion()
readonly ISqlFactory factory
readonly List< IVersionChanges > changes