diff --git a/CHANGELOG.md b/CHANGELOG.md index 773a3ea..204efa1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ Represents the **NuGet** versions. +## v1.0.15 +- *Fixed:* `Reset` command updated to load embedded SQL resource correctly. + ## v1.0.14 - *Enhancement:* Removed `DbUp` package dependencies and implemented equivalent (basics) that is compatible with `[dbo].[SchemaVersion]` journal management. Primary reason is related to the slow uptake of pull requests by the maintainers of `DbUp` that imposes limitations on `DbEx`. - *Fixed:* `DbTypeMapper` updated to support `SMALLDATETIME` and `IMAGE` Microsoft Sql Server types. diff --git a/src/DbEx/DbEx.csproj b/src/DbEx/DbEx.csproj index eed5297..d1c6137 100644 --- a/src/DbEx/DbEx.csproj +++ b/src/DbEx/DbEx.csproj @@ -3,7 +3,7 @@ netstandard2.1 DbEx - 1.0.14 + 1.0.15 true DbEx Developers Avanade diff --git a/src/DbEx/Migration/DatabaseMigrationScript.cs b/src/DbEx/Migration/DatabaseMigrationScript.cs index 901ede7..e703c0f 100644 --- a/src/DbEx/Migration/DatabaseMigrationScript.cs +++ b/src/DbEx/Migration/DatabaseMigrationScript.cs @@ -74,6 +74,8 @@ public DatabaseMigrationScript(string sql, string name) /// /// Gets the resource or file . /// - public StreamReader GetStreamReader() =>_assembly is not null ? new StreamReader(_assembly!.GetManifestResourceStream(Name)!) : (_file is not null ? _file!.OpenText() : new StreamReader(new MemoryStream(Encoding.Default.GetBytes(_sql)))); + public StreamReader GetStreamReader() => _assembly is not null + ? new StreamReader(_assembly!.GetManifestResourceStream(Name)!) + : (_file is not null ? _file!.OpenText() : new StreamReader(new MemoryStream(Encoding.Default.GetBytes(_sql)))); } } \ No newline at end of file diff --git a/src/DbEx/Migration/SqlServer/SqlServerMigrator.cs b/src/DbEx/Migration/SqlServer/SqlServerMigrator.cs index d42b8a1..8bc2635 100644 --- a/src/DbEx/Migration/SqlServer/SqlServerMigrator.cs +++ b/src/DbEx/Migration/SqlServer/SqlServerMigrator.cs @@ -142,9 +142,13 @@ protected override async Task DatabaseSchemaAsync(List protected override async Task DatabaseResetAsync() { - Logger.LogInformation(" Deleting data from all tables (excludes schema 'dbo' and 'cdc')."); - var ss = new DatabaseMigrationScript(typeof(DatabaseExtensions).Assembly, $"{typeof(IDatabase).Namespace}.SqlServer.DeleteAllAndReset.sql") { RunAlways = true }; - return await ExecuteScriptsAsync(new DatabaseMigrationScript[] { ss }, false).ConfigureAwait(false); + Logger.LogInformation(" Deleting data from all tables (excludes schema 'dbo' and 'cdc')..."); + using var sr = StreamLocator.GetResourcesStreamReader("SqlServer.DeleteAllAndReset.sql", new Assembly[] { typeof(DatabaseExtensions).Assembly }).StreamReader!; + var tables = await Database.SqlStatement(sr.ReadToEnd()).SelectQueryAsync(dr => { Logger.LogInformation("{Content}", $" [{dr.GetValue("Schema")}].[{dr.GetValue("Table")}]"); return 0; }).ConfigureAwait(false); + if (!tables.Any()) + Logger.LogInformation(" None."); + + return true; } /// @@ -196,7 +200,8 @@ public override async Task ExecuteScriptsAsync(IEnumerable 'dbo' AND i.TABLE_SCHEMA <> 'cdc' ) AS [t] WHERE t.TABLE_HISTORY IN (0,2) + ORDER BY t.TABLE_SCHEMA, t.TABLE_NAME DECLARE @TableSchema VARCHAR(256) DECLARE @TableName VARCHAR(256) @@ -31,6 +34,7 @@ BEGIN SET @SqlCommand = 'SET QUOTED_IDENTIFIER ON; DELETE FROM [' + @TableSchema + '].[' + @TableName + ']' PRINT @SqlCommand EXECUTE (@SqlCommand) + INSERT INTO #temp ([Schema], [Table]) VALUES (@TableSchema, @TableName) FETCH NEXT FROM t_cursor into @TableSchema, @TableName END @@ -45,4 +49,7 @@ BEGIN END CLOSE t_cursor -DEALLOCATE t_cursor \ No newline at end of file +DEALLOCATE t_cursor + +SELECT [Schema], [Table] FROM #temp +DROP TABLE #temp \ No newline at end of file diff --git a/tests/DbEx.Test/SqlServerMigratorTest.cs b/tests/DbEx.Test/SqlServerMigratorTest.cs index 98a37a2..392c50e 100644 --- a/tests/DbEx.Test/SqlServerMigratorTest.cs +++ b/tests/DbEx.Test/SqlServerMigratorTest.cs @@ -1,4 +1,7 @@ -using CoreEx.Database; +using System; +using System.Linq; +using System.Threading.Tasks; +using CoreEx.Database; using CoreEx.Database.SqlServer; using DbEx.Migration.Data; using DbEx.Migration.SqlServer; @@ -6,9 +9,6 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using NUnit.Framework; -using System; -using System.Linq; -using System.Threading.Tasks; namespace DbEx.Test { @@ -174,5 +174,40 @@ public async Task B100_Execute_Console_Error() var r = await m.ExecuteSqlStatementsAsync("SELECT * FROM Test.Contact", "SELECT BANANAS").ConfigureAwait(false); Assert.IsFalse(r); } + + [Test] + public async Task A140_Reset_None() + { + var cs = UnitTest.GetConfig("DbEx_").GetConnectionString("NoneDb"); + var l = UnitTest.GetLogger(); + var m = new SqlServerMigrator(cs, Migration.MigrationCommand.Reset, l); + var r = await m.MigrateAsync().ConfigureAwait(false); + + Assert.IsTrue(r); + } + + [Test] + public async Task A150_Reset_Console() + { + var (cs, l, m) = await CreateConsoleDb().ConfigureAwait(false); + using var db = new SqlServerDatabase(() => new SqlConnection(cs)); + + // There should be data loaded in Test.Contact. + var c = await db.SqlStatement("SELECT COUNT(*) FROM Test.Contact").ScalarAsync().ConfigureAwait(false); + Assert.That(c, Is.GreaterThanOrEqualTo(1)); + + // Execute Reset. + m = new SqlServerMigrator(cs, Migration.MigrationCommand.Reset, l); + var r = await m.MigrateAsync().ConfigureAwait(false); + Assert.IsTrue(r); + + // There should now be no data in Test.Contact. + c = await db.SqlStatement("SELECT COUNT(*) FROM Test.Contact").ScalarAsync().ConfigureAwait(false); + Assert.That(c, Is.EqualTo(0)); + + // Tables in dbo schema should not be touched. + c = await db.SqlStatement("SELECT COUNT(*) FROM [dbo].[SchemaVersions]").ScalarAsync().ConfigureAwait(false); + Assert.That(c, Is.GreaterThanOrEqualTo(1)); + } } } \ No newline at end of file