Skip to content
This repository was archived by the owner on Jul 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Microsoft.Research/Clousot.Cache/Clousot.Cache.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Data.Entity" />
<Reference Include="System.Data.Linq" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down
35 changes: 35 additions & 0 deletions Microsoft.Research/Clousot.Cache/SQLCacheModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Linq;
using System.Globalization;
using System.Linq;
using System.Text;
using Microsoft.Research.CodeAnalysis.Caching.Models;
Expand Down Expand Up @@ -558,6 +561,34 @@ public string CacheName
{
get { return this.cachename; }
}

protected static void DetachDeletedDb(string connection)
{
// If anyone has manually deleted the cache file, it might be still registered with LocalDB.
// If this is the case, we can't create a new database and the user is stuck unless he knows the tricky internals of LocalDB and how to get rid of the registration.
// => If we use LocalDB and the file does not exist, we try to detach it.
try
{
var connectionString = new DbConnectionStringBuilder { ConnectionString = connection };
var fileName = (string)connectionString["AttachDbFileName"];
if (File.Exists(fileName))
return;

var catalog = (string)connectionString["Initial Catalog"];
var dataSource = (string)connectionString["Data Source"];

using (var master = new DataContext(string.Format(CultureInfo.InvariantCulture, @"Data Source={0};Initial Catalog=master;Integrated Security=True", dataSource)))
{
master.ExecuteCommand(@"exec sp_detach_db {0}", catalog);
}
}
catch
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 I prefer to see this list the actual exception types which are known to be thrown for the cases you describe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fully agree, but here the exceptions may come from the DB providers and are neither documented nor consistent between DB providers - you can only get them by trial and error using all known providers, but then the code may break with any unknown provider.

{
// Ignore any errors here, could be
// - not using LocalDB at all (any of the connection string parameters did not exist)
// - file was never registered (first time used) or already detached.
}
}
}

public class SqlCacheModelNoCreate : SQLCacheModel
Expand Down Expand Up @@ -614,6 +645,8 @@ public SqlCacheModelClearExisting(string connection, string cachename, bool trac
: base(connection, cachename, trace)
{
Contract.Requires(cachename != null);

DetachDeletedDb(connection);
}
}

Expand All @@ -637,6 +670,8 @@ public SqlCacheModelDropOnModelChange(string connection, string cachename, bool
: base(connection, cachename, trace)
{
Contract.Requires(cachename != null);

DetachDeletedDb(connection);
}
}

Expand Down