private ProjectMetadata GetDbProjectMetadata(DbProjectInfo dbProjectInfo, EnvironmentInfo environmentInfo) { var projectVersions = new List <MachineSpecificProjectVersion>(); DbProjectConfiguration dbProjectConfiguration = environmentInfo.GetDbProjectConfiguration(dbProjectInfo); DatabaseServer databaseServer = environmentInfo.GetDatabaseServer(dbProjectConfiguration.DatabaseServerId); var dbVersions = _dbVersionProvider.GetVersions( dbProjectInfo.DbName, databaseServer.MachineName); DbVersion latestDbVersion = dbVersions .Select(s => DbVersion.FromString(s.Version)) .OrderByDescending(v => v) .FirstOrDefault(); if (latestDbVersion != null) { projectVersions.Add(new MachineSpecificProjectVersion(databaseServer.MachineName, latestDbVersion.ToString())); } return(new ProjectMetadata(dbProjectInfo.Name, environmentInfo.Name, projectVersions)); }
protected override void DoPrepare() { EnvironmentInfo environmentInfo = GetEnvironmentInfo(); DbProjectInfo projectInfo = GetProjectInfo <DbProjectInfo>(); DbProjectConfiguration dbProjectConfiguration = environmentInfo.GetDbProjectConfiguration(projectInfo); DatabaseServer databaseServer = environmentInfo.GetDatabaseServer(dbProjectConfiguration.DatabaseServerId); string databaseServerMachineName = databaseServer.MachineName; bool databaseExists = _dbVersionProvider.CheckIfDatabaseExists( projectInfo.DbName, databaseServerMachineName); // create a step for downloading the artifacts var downloadArtifactsDeploymentStep = new DownloadArtifactsDeploymentStep( projectInfo, DeploymentInfo, GetTempDirPath(), _artifactsRepository); AddSubTask(downloadArtifactsDeploymentStep); // create a step for extracting the artifacts var extractArtifactsDeploymentStep = new ExtractArtifactsDeploymentStep( projectInfo, environmentInfo, DeploymentInfo, downloadArtifactsDeploymentStep.ArtifactsFilePath, GetTempDirPath(), _fileAdapter, _directoryAdapter, _zipFileAdapter); AddSubTask(extractArtifactsDeploymentStep); if (databaseExists) { // create a step for gathering scripts to run var gatherDbScriptsToRunDeploymentStep = new GatherDbScriptsToRunDeploymentStep( projectInfo.DbName, new Lazy <string>(() => extractArtifactsDeploymentStep.BinariesDirPath), databaseServerMachineName, environmentInfo.Name, DeploymentInfo, _dbVersionProvider, _createScriptsToRunSelector ); AddSubTask(gatherDbScriptsToRunDeploymentStep); // create a step for running scripts var runDbScriptsDeploymentStep = new RunDbScriptsDeploymentStep( GetScriptRunner(projectInfo.IsTransactional, databaseServerMachineName, projectInfo.DbName, environmentInfo.DatabaseServers.FirstOrDefault(e => e.Id == projectInfo.DatabaseServerId)), databaseServerMachineName, new DeferredEnumerable <DbScriptToRun>(() => gatherDbScriptsToRunDeploymentStep.ScriptsToRun)); AddSubTask(runDbScriptsDeploymentStep); } else { // create step for dropping database var createDatabaseDeploymentStep = new CreateDatabaseDeploymentStep(projectInfo, databaseServer, _dbManagerFactory); AddSubTask(createDatabaseDeploymentStep); // create step for deploying dacpac var publishDatabaseDeploymentStep = new PublishDatabaseDeploymentStep( projectInfo, databaseServer, GetTempDirPath(), _databasePublisher); AddSubTask(publishDatabaseDeploymentStep); } foreach (string userId in projectInfo.Users) { var environmentUser = environmentInfo.EnvironmentUsers.SingleOrDefault(x => x.Id == userId); if (environmentUser == null) { throw new DeploymentTaskException(string.Format("User [{0}] doesn't exist in enviroment configuration [{1}] in project [{2}]", userId, environmentInfo.Name, projectInfo.Name)); } string user = _userNameNormalizer.ConvertToPreWin2000UserName(environmentUser.UserName, environmentInfo.DomainName); IDbManager manager = _dbManagerFactory.CreateDbManager(databaseServerMachineName); if (databaseExists && manager.UserExists(projectInfo.DbName, user)) { foreach (string dbUserRole in _dbUserRoles) { if (!manager.CheckIfUserIsInRole(projectInfo.DbName, user, dbUserRole)) { AddSubTask(new AddRoleToUserStep(manager, projectInfo.DbName, user, dbUserRole)); } } } else { AddSubTask(new AddUserToDatabaseStep(manager, projectInfo.DbName, user)); foreach (string dbUserRole in _dbUserRoles) { AddSubTask(new AddRoleToUserStep(manager, projectInfo.DbName, user, dbUserRole)); } } } }