Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
bricelam committed Dec 10, 2015
1 parent cd792c8 commit 5fc5e8c
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Migrations;
using Microsoft.Data.Entity.Utilities;
using Microsoft.Data.Sqlite;

namespace Microsoft.Data.Entity.Storage.Internal
{
public class SqliteDatabaseCreator : RelationalDatabaseCreator
{
private const int SQLITE_CANTOPEN = 14;

private readonly SqliteRelationalConnection _connection;
private readonly IRawSqlCommandBuilder _rawSqlCommandBuilder;

public SqliteDatabaseCreator(
[NotNull] IRelationalConnection connection,
[NotNull] SqliteRelationalConnection connection,
[NotNull] IMigrationsModelDiffer modelDiffer,
[NotNull] IMigrationsSqlGenerator migrationsSqlGenerator,
[NotNull] IModel model,
Expand All @@ -23,6 +27,7 @@ public SqliteDatabaseCreator(
{
Check.NotNull(rawSqlCommandBuilder, nameof(rawSqlCommandBuilder));

_connection = connection;
_rawSqlCommandBuilder = rawSqlCommandBuilder;
}

Expand All @@ -32,7 +37,22 @@ public override void Create()
Connection.Close();
}

public override bool Exists() => true;
public override bool Exists()
{
using (var readOnlyConnection = _connection.CreateReadOnlyConnection())
{
try
{
readOnlyConnection.Open();
}
catch (SqliteException ex) when (ex.SqliteErrorCode == SQLITE_CANTOPEN)
{
return false;
}
}

return true;
}

protected override bool HasTables()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,21 @@ private void EnableForeignKeys()
_rawSqlCommandBuilder.Build("PRAGMA foreign_keys=OFF;").ExecuteNonQuery(this);
}
}

public virtual SqliteRelationalConnection CreateReadOnlyConnection()
{
var builder = new SqliteConnectionStringBuilder(ConnectionString)
{
Mode = SqliteOpenMode.ReadOnly
};

var options = new DbContextOptionsBuilder();
options.UseSqlite(builder.ToString());

return new SqliteRelationalConnection(
_rawSqlCommandBuilder,
options.Options,
(ILogger<SqliteRelationalConnection>)Logger);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<Compile Include="DefaultValuesTest.cs" />
<Compile Include="F1SqliteFixture.cs" />
<Compile Include="GearsOfWarFromSqlQuerySqliteTest.cs" />
<Compile Include="SqliteDatabaseCreatorTest.cs" />
<Compile Include="SqliteForeignKeyTest.cs" />
<Compile Include="FromSqlQuerySqliteTest.cs" />
<Compile Include="GearsOfWarQuerySqliteFixture.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.Data.Entity.Infrastructure;
using Microsoft.Data.Entity.Storage;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace Microsoft.Data.Entity.Sqlite.FunctionalTests
{
public class SqliteDatabaseCreatorTest
{
[Fact]
public void Exists_returns_false_when_database_doesnt_exist()
{
var creator = GetDatabaseCreator("Data Source=doesnt-exist.db");

Assert.False(creator.Exists());
}

[Fact]
public void Exists_returns_true_when_database_exists()
{
using (var testStore = SqliteTestStore.CreateScratch())
{
var creator = GetDatabaseCreator(testStore.Connection.ConnectionString);

Assert.True(creator.Exists());
}
}

private IRelationalDatabaseCreator GetDatabaseCreator(string connectionString)
{
var services = new ServiceCollection();
services
.AddEntityFramework()
.AddSqlite();

var options = new DbContextOptionsBuilder();
options.UseSqlite(connectionString);

return new DbContext(services.BuildServiceProvider(), options.Options)
.GetService<IRelationalDatabaseCreator>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public static string CreateConnectionString(string name, bool sharedCache = fals
new SqliteConnectionStringBuilder
{
DataSource = name + ".db",
Cache = sharedCache ? SqliteConnectionCacheMode.Shared : SqliteConnectionCacheMode.Private
Cache = sharedCache ? SqliteCacheMode.Shared : SqliteCacheMode.Private
}.ToString();
}
}

0 comments on commit 5fc5e8c

Please sign in to comment.