diff --git a/Respawn/Checkpoint.cs b/Respawn/Checkpoint.cs index 3b89c71..3b23ba0 100644 --- a/Respawn/Checkpoint.cs +++ b/Respawn/Checkpoint.cs @@ -8,23 +8,23 @@ namespace Respawn { - public class Checkpoint - { - private IList _temporalTables = new List(); - - public Table[] TablesToIgnore { get; init; } = Array.Empty(); - public Table[] TablesToInclude { get; init; } = Array.Empty
(); - public string[] SchemasToInclude { get; init; } = Array.Empty(); - public string[] SchemasToExclude { get; init; } = Array.Empty(); + public class Checkpoint + { + private IList _temporalTables = new List(); + + public Table[] TablesToIgnore { get; init; } = Array.Empty
(); + public Table[] TablesToInclude { get; init; } = Array.Empty
(); + public string[] SchemasToInclude { get; init; } = Array.Empty(); + public string[] SchemasToExclude { get; init; } = Array.Empty(); public string? DeleteSql { get; private set; } - public string? ReseedSql { get; private set; } - public bool CheckTemporalTables { get; init; } - public bool WithReseed { get; init; } - public IDbAdapter DbAdapter { get; init; } = Respawn.DbAdapter.SqlServer; + public string? ReseedSql { get; private set; } + public bool CheckTemporalTables { get; init; } + public bool WithReseed { get; init; } + public IDbAdapter DbAdapter { get; init; } = Respawn.DbAdapter.SqlServer; - public int? CommandTimeout { get; init; } + public int? CommandTimeout { get; init; } - public virtual async Task Reset(string nameOrConnectionString) + public virtual async Task Reset(string nameOrConnectionString) { await using var connection = new SqlConnection(nameOrConnectionString); @@ -33,18 +33,18 @@ public virtual async Task Reset(string nameOrConnectionString) await Reset(connection); } - public virtual async Task Reset(DbConnection connection) - { - if (string.IsNullOrWhiteSpace(DeleteSql)) - { - await BuildDeleteTables(connection); - } + public virtual async Task Reset(DbConnection connection) + { + if (string.IsNullOrWhiteSpace(DeleteSql)) + { + await BuildDeleteTables(connection); + } - if (_temporalTables.Any()) - { - var turnOffVersioningCommandText = DbAdapter.BuildTurnOffSystemVersioningCommandText(_temporalTables); - await ExecuteAlterSystemVersioningAsync(connection, turnOffVersioningCommandText); - } + if (_temporalTables.Any()) + { + var turnOffVersioningCommandText = DbAdapter.BuildTurnOffSystemVersioningCommandText(_temporalTables); + await ExecuteAlterSystemVersioningAsync(connection, turnOffVersioningCommandText); + } try { @@ -60,7 +60,7 @@ public virtual async Task Reset(DbConnection connection) } } - private async Task ExecuteAlterSystemVersioningAsync(DbConnection connection, string commandText) + private async Task ExecuteAlterSystemVersioningAsync(DbConnection connection, string commandText) { await using var tx = await connection.BeginTransactionAsync(); await using var cmd = connection.CreateCommand(); @@ -74,7 +74,7 @@ private async Task ExecuteAlterSystemVersioningAsync(DbConnection connection, st await tx.CommitAsync(); } - private async Task ExecuteDeleteSqlAsync(DbConnection connection) + private async Task ExecuteDeleteSqlAsync(DbConnection connection) { await using var tx = await connection.BeginTransactionAsync(); await using var cmd = connection.CreateCommand(); @@ -94,30 +94,30 @@ private async Task ExecuteDeleteSqlAsync(DbConnection connection) await tx.CommitAsync(); } - private async Task BuildDeleteTables(DbConnection connection) - { - var allTables = await GetAllTables(connection); + private async Task BuildDeleteTables(DbConnection connection) + { + var allTables = await GetAllTables(connection); - if (CheckTemporalTables && await DbAdapter.CheckSupportsTemporalTables(connection)) - { - _temporalTables = await GetAllTemporalTables(connection); - } + if (CheckTemporalTables && await DbAdapter.CheckSupportsTemporalTables(connection)) + { + _temporalTables = await GetAllTemporalTables(connection); + } - var allRelationships = await GetRelationships(connection); + var allRelationships = await GetRelationships(connection); - var graphBuilder = new GraphBuilder(allTables, allRelationships); + var graphBuilder = new GraphBuilder(allTables, allRelationships); - DeleteSql = DbAdapter.BuildDeleteCommandText(graphBuilder); - ReseedSql = WithReseed ? DbAdapter.BuildReseedSql(graphBuilder.ToDelete) : null; - } + DeleteSql = DbAdapter.BuildDeleteCommandText(graphBuilder); + ReseedSql = WithReseed ? DbAdapter.BuildReseedSql(graphBuilder.ToDelete) : null; + } - private async Task> GetRelationships(DbConnection connection) - { - var relationships = new HashSet(); - var commandText = DbAdapter.BuildRelationshipCommandText(this); + private async Task> GetRelationships(DbConnection connection) + { + var relationships = new HashSet(); + var commandText = DbAdapter.BuildRelationshipCommandText(this); await using var cmd = connection.CreateCommand(); - + cmd.CommandText = commandText; await using var reader = await cmd.ExecuteReaderAsync(); @@ -131,13 +131,13 @@ private async Task> GetRelationships(DbConnection connecti } return relationships; - } + } - private async Task> GetAllTables(DbConnection connection) - { - var tables = new HashSet
(); + private async Task> GetAllTables(DbConnection connection) + { + var tables = new HashSet
(); - var commandText = DbAdapter.BuildTableCommandText(this); + var commandText = DbAdapter.BuildTableCommandText(this); await using var cmd = connection.CreateCommand(); @@ -151,13 +151,13 @@ private async Task> GetAllTables(DbConnection connection) } return tables; - } + } - private async Task> GetAllTemporalTables(DbConnection connection) - { - var tables = new List(); + private async Task> GetAllTemporalTables(DbConnection connection) + { + var tables = new List(); - var commandText = DbAdapter.BuildTemporalTableCommandText(this); + var commandText = DbAdapter.BuildTemporalTableCommandText(this); await using var cmd = connection.CreateCommand(); @@ -171,6 +171,6 @@ private async Task> GetAllTemporalTables(DbConnection conne } return tables; - } - } + } + } }