/// <summary>
        /// Sets the database to an empty state (Version 0).
        /// </summary>
        public void Reset()
        {
            var managementDatabase = new Database(this.connectionManager, this.fileSystemAccess, this.processManager)
            {
                Name = PostgresDefaultDatabaseName
            };

            try
            {
                // disconnect old database and delete it afterwards
                managementDatabase.ExecuteCommandNonQuery(SQLTemplates.DisconnectDatabase(this.Name));
                managementDatabase.ExecuteCommandNonQuery(SQLTemplates.DropDatabase(this.Name));
                managementDatabase.ExecuteCommandNonQuery(SQLTemplates.CreateDatabase(this.Name));

                // clear the old pool so no connection is reused!
                this.connectionManager.ClearPools();

                this.CreateTeamworkSchema();
                this.UpdateVersion();
            }
            catch (NpgsqlException ex)
            {
                Log.Error(ex.Message, ex);
                throw new TeamworkParserException($"Error while resetting database. {ex.Message}");
            }
        }
        private void TestSQLFiles(int progressStart, int progressEnd)
        {
            var databaseName       = $"APE.PostgreSQL.Teamwork.Test.{this.Name}";
            var progressDifference = progressEnd - progressStart;
            var progressStepSmall  = (double)progressDifference / 100 * 5;  // 5 %
            var progressStepBig    = (double)progressDifference / 100 * 40; // 40 %

            try
            {
                this.SetProgress(progressStart, "Testing the new files");

                // create temp database
                this.ExecuteCommandNonQuery(SQLTemplates.CreateDatabase(databaseName));
                this.SetProgress(this.Progress + progressStepSmall);
                var managementDatabase = new Database(databaseName, this.Path, this.IgnoredSchemas.ToList(), this.connectionManager, this.fileSystemAccess, this.processManager, this.differenceCreator, this.sqlFileTester, true);
                managementDatabase.UpdateData();

                var updateProgress = new Action <IEnumerable <SQLFile>, SQLFile>((files, file) =>
                {
                    // todo db file tester implement
                    ////this.sqlFileTester.CreateData(managementDatabase, file);
                    ////this.sqlFileTester.TestEmptyMethods(managementDatabase, file);

                    this.SetProgress(this.Progress + (progressStepBig / files.Count()), string.Format("Testing File {0}", file.FileName));
                });

                // execute all statements
                managementDatabase.UpdateToVersion(managementDatabase.LastApplicableVersion, updateProgress);

                if (managementDatabase.UndoDiffFiles.Count > 0)
                {
                    managementDatabase.UpdateToVersion(managementDatabase.UndoDiffFiles.Last().Version, updateProgress);
                }
            }
            finally
            {
                this.SetProgress(this.Progress + progressStepSmall, "Disconnecting Database");
                this.ExecuteCommandNonQuery(SQLTemplates.DisconnectDatabase(databaseName));
                this.SetProgress(this.Progress + progressStepSmall, "Dropping old Database");
                this.ExecuteCommandNonQuery(SQLTemplates.DropDatabase(databaseName));
                this.SetProgress(progressEnd);
            }
        }
예제 #3
0
        private void CreateDatabase()
        {
            new Task(() =>
            {
                try
                {
                    var name = DatabaseSetting.GetDatabaseSetting(this.Id).Name;
                    this.connectionManager.ExecuteCommandNonQuery(SQLTemplates.CreateDatabase(name));

                    // connect to database and update default data
                    this.ConnectDatabase();
                    this.UpdateData(); // do not remove this, else the edit mode will be finished when something else calls this

                    this.StartImport();
                }
                catch (Exception ex)
                {
                    Log.Error("Exception while executing Action in Task", ex);
                }
            }).Start();
        }
        public void ResetTest()
        {
            var name = "TestDB";
            var path = "testpfad";

            var fileMock = new Mock <IFileSystemAccess>();
            var connectionManagerMock = new Mock <IConnectionManager>();
            var processMock           = new Mock <IProcessManager>();
            var sqlFileMock           = new Mock <ISQLFile>();
            var diffCreatorMock       = new Mock <IDifferenceCreator>();
            var sqlFileTester         = new Mock <ISQLFileTester>();

            connectionManagerMock.Setup(c => c.ExecuteCommand <int>(It.IsAny <IDatabase>(), It.IsAny <string>()))
            .Returns(new List <int>()
            {
                0
            })
            .Verifiable();
            connectionManagerMock.Setup(c => c.ExecuteCommand <ExecutedFile>(It.IsAny <IDatabase>(), It.IsAny <string>()))
            .Returns(new List <ExecutedFile>())
            .Verifiable();

            // UpdateData is called internally when bool is set to true
            var database = new Database(name, path, new List <string>(), connectionManagerMock.Object, fileMock.Object, processMock.Object, diffCreatorMock.Object, sqlFileTester.Object, initializeData: false);

            connectionManagerMock.Setup(c => c.ExecuteCommandNonQuery(It.IsAny <IDatabase>(), SQLTemplates.CreateDatabase(name)));

            database.Reset();

            connectionManagerMock.VerifyAll();
        }