예제 #1
0
        private void _ApplyChangeScripts(
            DatabaseVersion currentDatabaseVersion,
            string scriptsDirectoryPath,
            IDbConnection dbConnection,
            IDbTransaction transaction
            )
        {
            var changeScriptSqlFiles        = Directory.GetFiles(scriptsDirectoryPath, _sqlScriptFileSearchPattern);
            var orderedChangeScriptSqlFiles = changeScriptSqlFiles
                                              .OrderBy(changeScriptFileFullName =>
            {
                var version = _GetChangeScriptVersionFromFullFileName(changeScriptFileFullName);
                return(new DatabaseVersion(version));
            })
                                              .ToList();

            var changeScriptSqlFilesGreaterThanCurrentDatabaseVersion = orderedChangeScriptSqlFiles.Where(
                changeScriptFileFullName =>
            {
                var version = _GetChangeScriptVersionFromFullFileName(changeScriptFileFullName);
                return(new DatabaseVersion(version).CompareTo(currentDatabaseVersion) > 0);
            }).ToList();

            if (!changeScriptSqlFilesGreaterThanCurrentDatabaseVersion.Any())
            {
                return;
            }

            _logAction("Change scripts applied:");
            foreach (var changeScript in changeScriptSqlFilesGreaterThanCurrentDatabaseVersion)
            {
                _logAction(_GetChangeScriptVersionFromFullFileName(changeScript));
            }

            foreach (var changeScriptSqlFile in changeScriptSqlFilesGreaterThanCurrentDatabaseVersion)
            {
                _ApplyOneSqlScript(changeScriptSqlFile, dbConnection, transaction);
            }

            _UpdateDatabaseVersion(
                dbConnection,
                transaction,
                changeScriptSqlFilesGreaterThanCurrentDatabaseVersion.Last(),
                currentDatabaseVersion
                );
        }
예제 #2
0
        private void _UpdateDatabaseVersion(
            IDbConnection dbConnection,
            IDbTransaction transaction,
            string lastChangeScriptSqlFile,
            DatabaseVersion currentDatabaseVersion
            )
        {
            var lastChangeScriptVersion           = _GetChangeScriptVersionFromFullFileName(lastChangeScriptSqlFile);
            var databaseVersionOfLastChangeScript = new DatabaseVersion(lastChangeScriptVersion);

            try
            {
                using (var command = dbConnection.CreateCommand())
                {
                    command.CommandText = $"update \"{_versionTableName}\" set " +
                                          $"    \"Major\" = {databaseVersionOfLastChangeScript.Major}, " +
                                          $"    \"Minor\" = {databaseVersionOfLastChangeScript.Minor}, " +
                                          $"    \"Revision\" = {databaseVersionOfLastChangeScript.Revision}, " +
                                          $"    \"ScriptNumber\" = {databaseVersionOfLastChangeScript.ScriptNumber} " +
                                          $"where \"Major\" = {currentDatabaseVersion.Major} " +
                                          $"and \"Minor\" = {currentDatabaseVersion.Minor} " +
                                          $"and \"Revision\" = {currentDatabaseVersion.Revision} " +
                                          $"and \"ScriptNumber\" = {currentDatabaseVersion.ScriptNumber} "
                    ;
                    command.Transaction = transaction;
                    var numberOfRowsAffected = command.ExecuteNonQuery();
                    if (numberOfRowsAffected != 1)
                    {
                        throw new Exception(
                                  "Database version has been changed (by another process concurrently?). " +
                                  "Please make sure the first change script creating the version table updates the database version to 0.0.0.0.");
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"Cannot update database version in table {_versionTableName}", ex);
            }

            _logAction($"Database version updated to {lastChangeScriptVersion}");
        }