/// <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}");
            }
        }
Пример #2
0
 private void Disconnect()
 {
     this.ExecuteInTask(() =>
     {
         try
         {
             var name = DatabaseSetting.GetDatabaseSetting(this.Id).Name;
             this.connectionManager.ExecuteCommandNonQuery(SQLTemplates.DisconnectDatabase(name));
         }
         catch (Exception ex)
         {
             Log.Error("Exception while executing Action in Task", ex);
         }
     });
 }
        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);
            }
        }