Skip to content

Instantly share code, notes, and snippets.

@wwwlicious
Last active August 29, 2015 14:02
Show Gist options
  • Save wwwlicious/15bd32691b026fe1ea98 to your computer and use it in GitHub Desktop.
Save wwwlicious/15bd32691b026fe1ea98 to your computer and use it in GitHub Desktop.
Truncate NCover 3.x coverage
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TruncateNCoverTrend.cs" company="wwwlicious">
// Copyright (c) 2011 All Rights Reserved
// </copyright>
// <summary>
// Defines the TruncateNCoverTrend Build Task. This task truncates the trend file to a specific time period
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace wwwlicious.MSBuild
{
using System;
using System.Data.SQLite;
using System.IO;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
public class TruncateNCoverTrend : Task
{
[Required]
public string TrendFile { get; set; }
[Required]
public int PreserveMonths { get; set; }
/// <summary>
/// When overridden in a derived class, executes the task.
/// </summary>
/// <returns>
/// true if the task successfully executed; otherwise, false.
/// </returns>
/// <exception cref="FileNotFoundException">TrendFile not found</exception>
public override bool Execute()
{
Log.LogMessage(MessageImportance.High, "Starting nCover Trend Coverage Truncate");
if (!File.Exists(TrendFile)) throw new FileNotFoundException("TrendFile not found", TrendFile);
Truncate();
Log.LogMessage(MessageImportance.High, "Finished nCover Trend Coverage Truncate");
return true;
}
private void Truncate()
{
var connectionString = string.Format("data source={0}", TrendFile);
using (var connection = new SQLiteConnection(connectionString))
using (var command = connection.CreateCommand())
{
command.CommandText = this.CreateCommandText();
this.Log.LogMessage(command.CommandText);
try
{
connection.Open();
command.ExecuteNonQuery();
}
catch (Exception ex)
{
this.Log.LogErrorFromException(ex, true);
}
}
}
private string CreateCommandText()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("BEGIN TRANSACTION;");
sb.AppendLine(string.Format("DELETE from Execution where StartTime < '{0}';", DateTime.Today.AddMonths(-this.PreserveMonths).ToString("yyyy-MM-dd")));
sb.AppendLine("DELETE FROM ViewExecutionStat WHERE (ExecutionId NOT IN (SELECT ExecutionId FROM Execution));");
sb.AppendLine("DELETE FROM ClassExecutionStat WHERE (ExecutionId NOT IN (SELECT ExecutionId FROM Execution));");
sb.AppendLine("DELETE FROM DocumentExecutionStat WHERE (ExecutionId NOT IN (SELECT ExecutionId FROM Execution));");
sb.AppendLine("DELETE FROM MethodExecutionStat WHERE (ExecutionId NOT IN (SELECT ExecutionId FROM Execution));");
sb.AppendLine("DELETE FROM ModuleExecutionStat WHERE (ExecutionId NOT IN (SELECT ExecutionId FROM Execution));");
sb.AppendLine("DELETE FROM NamespaceExecutionStat WHERE (ExecutionId NOT IN (SELECT ExecutionId FROM Execution));");
sb.AppendLine("DELETE FROM ModuleClass WHERE (ModuleClassId NOT IN (SELECT ModuleClassId FROM ClassExecutionStat));");
sb.AppendLine("DELETE FROM Module WHERE (ModuleId NOT IN (SELECT ModuleId FROM ModuleClass));");
sb.AppendLine("DELETE FROM ModuleDocument WHERE (ModuleDocumentId NOT IN (SELECT ModuleDocumentId FROM DocumentExecutionStat));");
sb.AppendLine("DELETE FROM Document WHERE (DocumentId NOT IN (SELECT DocumentId FROM ModuleDocument));");
sb.AppendLine("DELETE FROM ModuleNamespace WHERE (ModuleNamespaceId NOT IN (SELECT ModuleNamespaceId FROM NamespaceExecutionStat));");
sb.AppendLine("DELETE FROM Namespace WHERE (NamespaceId NOT IN (SELECT NamespaceId FROM ModuleNamespace));");
sb.AppendLine("DELETE FROM Class WHERE (NamespaceId NOT IN (SELECT NamespaceId FROM Namespace));");
sb.AppendLine("DELETE FROM Method WHERE (ClassId NOT IN (SELECT ClassId FROM Class));");
sb.AppendLine("DELETE FROM ModuleClassMethod WHERE (MethodId NOT IN (SELECT MethodId FROM Method));");
sb.AppendLine("COMMIT TRANSACTION;");
sb.AppendLine("VACUUM;");
return sb.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment