Ejemplo n.º 1
0
        private void ExecuteOperation(DacFxOperation operation, DacFxParams parameters, string taskName, RequestContext <DacFxResult> requestContext)
        {
            Task.Run(async() =>
            {
                try
                {
                    TaskMetadata metadata = TaskMetadata.Create(parameters, taskName, operation, ConnectionServiceInstance);

                    // put appropriate database name since connection passed was to master
                    metadata.DatabaseName = parameters.DatabaseName;
                    SqlTask sqlTask       = SqlTaskManagerInstance.CreateTask <SqlTask>(metadata);

                    await sqlTask.RunAsync();
                    await requestContext.SendResult(new DacFxResult()
                    {
                        OperationId  = operation.OperationId,
                        Success      = sqlTask.TaskStatus == SqlTaskStatus.Succeeded,
                        ErrorMessage = string.Empty
                    });
                }
                catch (Exception e)
                {
                    await requestContext.SendResult(new DacFxResult()
                    {
                        OperationId  = operation.OperationId,
                        Success      = false,
                        ErrorMessage = e.Message
                    });
                }
            });
        }
        public async Task VerifyTaskWithExecutionMode(TaskExecutionMode executionMode, bool makeTaskFail = false)
        {
            serviceHostMock.AddEventHandling(TaskStatusChangedNotification.Type, null);

            using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
            {
                //To make the task fail don't create the schema so create table fails
                string query = string.Empty;
                if (!makeTaskFail)
                {
                    query = $"CREATE SCHEMA [test]";
                }
                SqlTestDb testDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, query, "TaskService");

                try
                {
                    TestConnectionResult connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync(testDb.DatabaseName, queryTempFile.FilePath);

                    string          taskName    = "task name";
                    Server          server      = CreateServerObject(connectionResult.ConnectionInfo);
                    RequstParamStub requstParam = new RequstParamStub
                    {
                        TaskExecutionMode = executionMode,
                        OwnerUri          = queryTempFile.FilePath
                    };
                    SmoScriptableTaskOperationStub taskOperation = new SmoScriptableTaskOperationStub(server);
                    taskOperation.DatabaseName = testDb.DatabaseName;
                    taskOperation.TableName    = "newTable";
                    TaskMetadata taskMetadata = TaskMetadata.Create(requstParam, taskName, taskOperation, ConnectionService.Instance);
                    SqlTask      sqlTask      = service.TaskManager.CreateTask <SqlTask>(taskMetadata);
                    Task         taskToVerify = sqlTask.RunAsync().ContinueWith(task =>
                    {
                        if (!makeTaskFail)
                        {
                            if (executionMode == TaskExecutionMode.Script || executionMode == TaskExecutionMode.ExecuteAndScript)
                            {
                                serviceHostMock.Verify(x => x.SendEvent(TaskStatusChangedNotification.Type,
                                                                        It.Is <TaskProgressInfo>(t => !string.IsNullOrEmpty(t.Script))), Times.AtLeastOnce());
                            }

                            //Verify if the table created if execution mode includes execute
                            bool expected         = executionMode == TaskExecutionMode.Execute || executionMode == TaskExecutionMode.ExecuteAndScript;
                            Server serverToverfiy = CreateServerObject(connectionResult.ConnectionInfo);
                            bool actual           = serverToverfiy.Databases[testDb.DatabaseName].Tables.Contains(taskOperation.TableName, "test");
                            Assert.Equal(expected, actual);
                        }
                        else
                        {
                            serviceHostMock.Verify(x => x.SendEvent(TaskStatusChangedNotification.Type,
                                                                    It.Is <TaskProgressInfo>(t => t.Status == SqlTaskStatus.Failed)), Times.AtLeastOnce());
                        }
                    });
                    await taskToVerify;
                }
                finally
                {
                    testDb.Cleanup();
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Handles request to generate deploy script
        /// </summary>
        /// <returns></returns>
        public async Task HandleGenerateDeployScriptRequest(GenerateDeployScriptParams parameters, RequestContext <DacFxResult> requestContext)
        {
            try
            {
                ConnectionInfo connInfo;
                ConnectionServiceInstance.TryFindConnection(
                    parameters.OwnerUri,
                    out connInfo);
                if (connInfo != null)
                {
                    GenerateDeployScriptOperation operation = new GenerateDeployScriptOperation(parameters, connInfo);
                    SqlTask      sqlTask  = null;
                    TaskMetadata metadata = TaskMetadata.Create(parameters, SR.GenerateScriptTaskName, operation, ConnectionServiceInstance);

                    // want to show filepath in task history instead of server and database
                    metadata.ServerName   = parameters.ScriptFilePath;
                    metadata.DatabaseName = string.Empty;

                    sqlTask = SqlTaskManagerInstance.CreateAndRun <SqlTask>(metadata);

                    await requestContext.SendResult(new DacFxResult()
                    {
                        OperationId  = operation.OperationId,
                        Success      = true,
                        ErrorMessage = string.Empty
                    });
                }
            }
            catch (Exception e)
            {
                await requestContext.SendError(e);
            }
        }
        /// <summary>
        /// Handles a restore request
        /// </summary>
        internal async Task HandleRestoreRequest(
            RestoreParams restoreParams,
            RequestContext <RestoreResponse> requestContext)
        {
            RestoreResponse response = new RestoreResponse();

            try
            {
                ConnectionInfo connInfo;
                bool           supported = IsBackupRestoreOperationSupported(restoreParams.OwnerUri, out connInfo);

                if (supported && connInfo != null)
                {
                    try
                    {
                        RestoreDatabaseTaskDataObject restoreDataObject = this.restoreDatabaseService.CreateRestoreDatabaseTaskDataObject(restoreParams, connInfo);

                        if (restoreDataObject != null)
                        {
                            restoreDataObject.LockedDatabaseManager = ConnectionServiceInstance.LockedDatabaseManager;
                            // create task metadata
                            TaskMetadata metadata = TaskMetadata.Create(restoreParams, SR.RestoreTaskName, restoreDataObject, ConnectionServiceInstance);
                            metadata.DatabaseName = restoreParams.TargetDatabaseName;

                            // create restore task and perform
                            SqlTask sqlTask = SqlTaskManagerInstance.CreateAndRun <SqlTask>(metadata);
                            response.TaskId = sqlTask.TaskId.ToString();
                        }
                        else
                        {
                            response.ErrorMessage = SR.RestorePlanFailed;
                        }
                    }
                    catch (Exception ex)
                    {
                        response.ErrorMessage = ex.Message;
                    }
                }
                else
                {
                    response.ErrorMessage = SR.RestoreNotSupported;
                }

                await requestContext.SendResult(response);
            }
            catch (Exception ex)
            {
                response.Result       = false;
                response.ErrorMessage = ex.Message;
                await requestContext.SendResult(response);
            }
        }
Ejemplo n.º 5
0
        private async Task ExecuteOperation(DacFxOperation operation, DacFxParams parameters, string taskName, RequestContext <DacFxResult> requestContext)
        {
            SqlTask      sqlTask  = null;
            TaskMetadata metadata = TaskMetadata.Create(parameters, taskName, operation, ConnectionServiceInstance);

            // put appropriate database name since connection passed was to master
            metadata.DatabaseName = parameters.DatabaseName;

            sqlTask = SqlTaskManagerInstance.CreateAndRun <SqlTask>(metadata);

            await requestContext.SendResult(new DacFxResult()
            {
                OperationId  = operation.OperationId,
                Success      = true,
                ErrorMessage = string.Empty
            });
        }
        /// <summary>
        /// Handles a backup request
        /// </summary>
        internal async Task HandleBackupRequest(
            BackupParams backupParams,
            RequestContext <BackupResponse> requestContext)
        {
            try
            {
                BackupResponse response = new BackupResponse();
                ConnectionInfo connInfo;
                bool           supported = IsBackupRestoreOperationSupported(backupParams.OwnerUri, out connInfo);

                if (supported && connInfo != null)
                {
                    DatabaseTaskHelper helper  = AdminService.CreateDatabaseTaskHelper(connInfo, databaseExists: true);
                    SqlConnection      sqlConn = ConnectionService.OpenSqlConnection(connInfo, "Backup");
                    // Connection gets discounnected when backup is done

                    BackupOperation backupOperation = CreateBackupOperation(helper.DataContainer, sqlConn, backupParams.BackupInfo);
                    SqlTask         sqlTask         = null;

                    // create task metadata
                    TaskMetadata metadata = TaskMetadata.Create(backupParams, SR.BackupTaskName, backupOperation, ConnectionServiceInstance);

                    sqlTask = SqlTaskManagerInstance.CreateAndRun <SqlTask>(metadata);
                    sqlTask.StatusChanged += CloseConnection;
                }
                else
                {
                    response.Result = false;
                }

                await requestContext.SendResult(response);
            }
            catch (Exception ex)
            {
                await requestContext.SendError(ex.ToString());
            }
        }