Skip to content

Commit

Permalink
Add IRelationalDatabaseCreator.HasTables and make the implementation …
Browse files Browse the repository at this point in the history
…public

Fixes #15997
  • Loading branch information
ajcvickers committed Jun 8, 2019
1 parent 244cdc1 commit 3f7b929
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 5 deletions.
18 changes: 18 additions & 0 deletions src/EFCore.Relational/Storage/IRelationalDatabaseCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ public interface IRelationalDatabaseCreator : IDatabaseCreator
/// </returns>
Task<bool> ExistsAsync(CancellationToken cancellationToken = default);

/// <summary>
/// Determines whether the database contains any tables. No attempt is made to determine if
/// tables belong to the current model or not.
/// </summary>
/// <returns> A value indicating whether any tables are present in the database. </returns>
bool HasTables();

/// <summary>
/// Asynchronously determines whether the database contains any tables. No attempt is made to determine if
/// tables belong to the current model or not.
/// </summary>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>
/// A task that represents the asynchronous operation. The task result contains
/// a value indicating whether any tables are present in the database.
/// </returns>
Task<bool> HasTablesAsync(CancellationToken cancellationToken = default);

/// <summary>
/// Creates the physical database. Does not attempt to populate it with any schema.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Relational/Storage/RelationalDatabaseCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ protected virtual IReadOnlyList<MigrationCommand> GetCreateTablesCommands()
/// tables belong to the current model or not.
/// </summary>
/// <returns> A value indicating whether any tables are present in the database. </returns>
protected abstract bool HasTables();
public abstract bool HasTables();

/// <summary>
/// Asynchronously determines whether the database contains any tables. No attempt is made to determine if
Expand All @@ -156,7 +156,7 @@ protected virtual IReadOnlyList<MigrationCommand> GetCreateTablesCommands()
/// A task that represents the asynchronous operation. The task result contains
/// a value indicating whether any tables are present in the database.
/// </returns>
protected virtual Task<bool> HasTablesAsync(CancellationToken cancellationToken = default)
public virtual Task<bool> HasTablesAsync(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ await Dependencies.MigrationCommandExecutor
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override bool HasTables()
public override bool HasTables()
=> Dependencies.ExecutionStrategyFactory
.Create()
.Execute(
Expand All @@ -128,7 +128,7 @@ protected override bool HasTables()
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Task<bool> HasTablesAsync(CancellationToken cancellationToken = default)
public override Task<bool> HasTablesAsync(CancellationToken cancellationToken = default)
=> Dependencies.ExecutionStrategyFactory.Create().ExecuteAsync(
_connection,
async (connection, ct) => (int)await CreateHasTablesCommand()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public override bool Exists()
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override bool HasTables()
public override bool HasTables()
{
var count = (long)_rawSqlCommandBuilder
.Build("SELECT COUNT(*) FROM \"sqlite_master\" WHERE \"type\" = 'table' AND \"rootpage\" IS NOT NULL;")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class FakeRelationalDatabaseCreator : IRelationalDatabaseCreator
public Task<bool> CanConnectAsync(CancellationToken cancellationToken = default) => throw new NotImplementedException();
public bool Exists() => throw new NotImplementedException();
public Task<bool> ExistsAsync(CancellationToken cancellationToken = new CancellationToken()) => throw new NotImplementedException();
public bool HasTables() => throw new NotImplementedException();
public Task<bool> HasTablesAsync(CancellationToken cancellationToken = default) => throw new NotImplementedException();
public void Create() => throw new NotImplementedException();
public Task CreateAsync(CancellationToken cancellationToken = new CancellationToken()) => throw new NotImplementedException();
public void Delete() => throw new NotImplementedException();
Expand Down
29 changes: 29 additions & 0 deletions test/EFCore.Sqlite.FunctionalTests/SqliteDatabaseCreatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,35 @@ public async Task Exists_returns_false_when_database_doesnt_exist(bool async, bo
}
}

[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public async Task HasTables_returns_false_when_database_is_empty(bool async)
{
using (var testStore = SqliteTestStore.GetOrCreateInitialized("Empty"))
{
var context = CreateContext(testStore.ConnectionString);

var creator = context.GetService<IRelationalDatabaseCreator>();
Assert.False(async ? await creator.HasTablesAsync() : creator.HasTables());
}
}

[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public async Task HasTables_returns_true_when_database_is_not_empty(bool async)
{
using (var testStore = SqliteTestStore.GetOrCreateInitialized($"HasATable{(async ? 'A' : 'S')}"))
{
var context = CreateContext(testStore.ConnectionString);
context.Database.ExecuteSqlRaw("CREATE TABLE Dummy (Foo INTEGER)");

var creator = context.GetService<IRelationalDatabaseCreator>();
Assert.True(async ? await creator.HasTablesAsync() : creator.HasTables());
}
}

[ConditionalTheory]
[InlineData(false, false)]
[InlineData(true, false)]
Expand Down

0 comments on commit 3f7b929

Please sign in to comment.