Galen.Ci.EntityFramework.DeploymentHistory.ExtractCurrent C# (CSharp) Method

ExtractCurrent() public static method

public static ExtractCurrent ( string contextKey, DbConnection connection, string schemaName, string extractDirectoryPath ) : string
contextKey string
connection System.Data.Common.DbConnection
schemaName string
extractDirectoryPath string
return string
        public static string ExtractCurrent(
            string contextKey, 
            DbConnection connection, 
            string schemaName, 
            string extractDirectoryPath)
        {
            const string sql =
                "SELECT TOP 1 " +
                    "   DeploymentId " +
                    " , ContextKey " +
                    " , AssemblyFileName " +
                    " , Binaries " +
                    " , Hashes " +
                    " , DeployerVersion " +
                "FROM [{0}].[__DeploymentHistory] " +
                "WHERE ContextKey = @ContextKey " +
                "ORDER BY DeploymentId DESC ";

            var hasDeploymentHistoryTable = GetIsDeploymentHistoryTableExists(connection, schemaName);
            if (!hasDeploymentHistoryTable)
            {
                return null;
            }

            DbCommand command = null;
            DbDataReader reader = null;
            try
            {
                command = connection.CreateCommand();
                command.CommandText = string.Format(sql, schemaName);

                var contextKeyParam = command.CreateParameter();
                contextKeyParam.DbType = DbType.String;
                contextKeyParam.Direction = ParameterDirection.Input;
                contextKeyParam.ParameterName = "@ContextKey";
                contextKeyParam.Value = contextKey;
                command.Parameters.Add(contextKeyParam);

                reader = command.ExecuteReader();
                if (!reader.HasRows)
                {
                    return null;
                }

                if (!reader.Read())
                {
                    throw new Exception($"Couldn't read {schemaName} deployment history; contextKey: {contextKey}");
                }

                // extract to a working sub-directory using the deploymentId
                var deploymentId = reader.GetString(0);
                var targetDirectoryPath = Path.Combine(extractDirectoryPath, deploymentId);

                using (var zipArchive = new ZipArchive(reader.GetStream(3), ZipArchiveMode.Read))
                {
                    ZipUtility.Unzip(targetDirectoryPath, zipArchive);
                }

                using (var hashesReader = new StreamReader(reader.GetStream(4)))
                {
                    HashUtility.Verify(targetDirectoryPath, hashesReader, m_HashName);
                }

                var assemblyFilePath = Path.Combine(targetDirectoryPath, reader.GetString(2));

                reader.Close();

                return assemblyFilePath;
            }
            finally
            {
                reader.SafeDispose();
                command.SafeDispose();
            }
        }
    }

Usage Example

示例#1
0
        private Assembly LazyLoadDeployedAssembly()
        {
            var isOverrideDeploymentHistory = !string.IsNullOrEmpty(m_Config.DeployedAssemblyOverridePath);

            if (isOverrideDeploymentHistory)
            {
                Log.Information(
                    "Override deployed assembly path {overridePath} specified.  Deployment History will not be used!",
                    m_Config.DeployedAssemblyOverridePath);
                return(m_AssemblyLoader.Load(MigrationsSource.Deployed, m_Config.DeployedAssemblyOverridePath));
            }

            if (m_Config.Mode == DeploymentMode.InitializeOnly || m_Config.Mode == DeploymentMode.SeedOnly)
            {
                return(null);
            }

            string deploymentHistoryAssemblyPath = null;
            var    targetContextKeySchema        = GetContextKeySchema(TargetAssembly, m_Config.MigrationConfig.Type);

            if (targetContextKeySchema == null)
            {
                Log.Warning(
                    "Failed to determine context key and schema name for config type {configType} in target assembly {assemblyPath}.  " +
                    "The cause is most likely an attempt to migrate downward to a version in which {configType} does not exist.  " +
                    "A deployed assembly override must be specified in these cases.  Downward migrations will either not occur or will fail.",
                    m_Config.MigrationConfig.Type,
                    TargetAssembly.CodeBase);
                return(null);
            }

            if (!GetIsTargetDatabaseExists())
            {
                Log.Debug(
                    "Database {endpointDatabase} does not exist on {endPointServer}.  Deployment History will not be used.",
                    m_Config.Database.DatabaseName,
                    m_Config.Database.ServerName);
                return(null);
            }

            var factory = DbProviderFactories.GetFactory(m_ConnectionInfoBuilder.ProviderName);

            using (var connection = factory.CreateConnection())
            {
                connection.ConnectionString = m_ConnectionInfoBuilder.BuildConnectionString(
                    m_Config.Database,
                    m_Config.AuthMode,
                    m_Config.SqlLogin,
                    m_Config.SqlPassword);

                connection.Open();

                Log.Debug(
                    "Extracting current deployment history assemblies " +
                    "for {contextKey} in {schemaName} schema on {endPointServer}\\{endpointDatabase} to {extractPath}.",
                    targetContextKeySchema.ContextKey,
                    targetContextKeySchema.SchemaName,
                    m_Config.Database.ServerName,
                    m_Config.Database.DatabaseName,
                    m_Config.DeploymentHistoryExtractPath);

                deploymentHistoryAssemblyPath = DeploymentHistory.ExtractCurrent(
                    targetContextKeySchema.ContextKey,
                    connection,
                    targetContextKeySchema.SchemaName,
                    m_Config.DeploymentHistoryExtractPath);
                connection.Close();
            }

            if (string.IsNullOrEmpty(deploymentHistoryAssemblyPath))
            {
                Log.Warning("No {targetSchemaName} Deployment History available for {targetContextKey} on {endPointServer}\\{endpointDatabase}.",
                            targetContextKeySchema.SchemaName,
                            targetContextKeySchema.ContextKey,
                            m_Config.Database.ServerName,
                            m_Config.Database.DatabaseName);
                return(null);
            }

            return(m_AssemblyLoader.Load(MigrationsSource.Deployed, deploymentHistoryAssemblyPath));
        }