From 56d5c464e3d8155a0311103f6ee1729d4cc3c7a6 Mon Sep 17 00:00:00 2001 From: Jason Finch Date: Tue, 30 Jan 2024 20:57:14 +1000 Subject: [PATCH 1/2] fix(driverResolve Exception during PerformUpgrade using LogScriptOutput #577 - Drop non supported frames. (Netstandard 1.3/Mono) - Replace System.Data.SQLite with Microsoft.Data.SqLite - Breaking change `Create` removed from TemporarySQLiteDatabase, it is now implicit --- src/Sample/Program.cs | 5 +- .../NoPublicApiChanges.Run.DotNet.verified.cs | 1 - .../NoPublicApiChanges.Run.Net.verified.cs | 1 - src/Tests/SQLiteSupportTests.cs | 37 +++++++++----- src/Tests/SQLiteTableJournalTests.cs | 8 ++- .../Helpers/InMemorySQLiteDatabase.cs | 30 +++-------- .../Helpers/TemporarySQLiteDatabase.cs | 50 ++----------------- src/dbup-sqlite/SQLiteConnectionManager.cs | 12 ++--- src/dbup-sqlite/SQLiteScriptExecutor.cs | 15 +----- src/dbup-sqlite/dbup-sqlite.csproj | 13 +---- 10 files changed, 48 insertions(+), 124 deletions(-) diff --git a/src/Sample/Program.cs b/src/Sample/Program.cs index 8957155..3ff2c46 100644 --- a/src/Sample/Program.cs +++ b/src/Sample/Program.cs @@ -1,4 +1,5 @@ using System; +using Microsoft.Data.Sqlite; namespace SQLiteSampleApplication { @@ -55,7 +56,7 @@ static void TemporaryFileDb() static void PermanentFileDb() { - Microsoft.Data.Sqlite.SqliteConnection connection = new("Data Source=dbup.db"); + SqliteConnection connection = new("Data Source=dbup.db"); using (var database = new DbUp.SQLite.Helpers.SharedConnection(connection)) { @@ -98,4 +99,4 @@ static void Display(string dbType, DbUp.Engine.DatabaseUpgradeResult result, Tim } } } -} \ No newline at end of file +} diff --git a/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.DotNet.verified.cs b/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.DotNet.verified.cs index 15ac3ed..b691b30 100644 --- a/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.DotNet.verified.cs +++ b/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.DotNet.verified.cs @@ -71,7 +71,6 @@ public class TemporarySQLiteDatabase : System.IDisposable public TemporarySQLiteDatabase(string name) { } public DbUp.SQLite.Helpers.SharedConnection SharedConnection { get; } public DbUp.Helpers.AdHocSqlRunner SqlRunner { get; } - public void Create() { } public void Dispose() { } } } diff --git a/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.Net.verified.cs b/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.Net.verified.cs index 15ac3ed..b691b30 100644 --- a/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.Net.verified.cs +++ b/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.Net.verified.cs @@ -71,7 +71,6 @@ public class TemporarySQLiteDatabase : System.IDisposable public TemporarySQLiteDatabase(string name) { } public DbUp.SQLite.Helpers.SharedConnection SharedConnection { get; } public DbUp.Helpers.AdHocSqlRunner SqlRunner { get; } - public void Create() { } public void Dispose() { } } } diff --git a/src/Tests/SQLiteSupportTests.cs b/src/Tests/SQLiteSupportTests.cs index bd0d553..b6cafe4 100644 --- a/src/Tests/SQLiteSupportTests.cs +++ b/src/Tests/SQLiteSupportTests.cs @@ -1,30 +1,43 @@ -#if !NETCORE -using System; -using System.Data.SQLite; +using System; using System.IO; +using Shouldly; using Xunit; namespace DbUp.SQLite.Tests { public class SQLiteSupportTests { - static readonly string dbFilePath = Path.Combine(Environment.CurrentDirectory, "test.db"); + static readonly string DbFilePath = Path.Combine(Environment.CurrentDirectory, "test.db"); [Fact] public void CanUseSQLite() { - var connectionString = string.Format("Data Source={0}; Version=3;", dbFilePath); + var connectionString = $"Data Source={DbFilePath}; Version=3;"; - if (!File.Exists(dbFilePath)) - { - SQLiteConnection.CreateFile(dbFilePath); - } - - var upgrader = DeployChanges.To + DeployChanges.To .SQLiteDatabase(connectionString) .WithScript("Script0001", "CREATE TABLE IF NOT EXISTS Foo (Id int)") .Build(); } + + [Fact] + public void DoesNotExhibitSafeHandleError_Issue577() + { + var connectionString = "Data source=:memory:"; + + var upgrader = + DeployChanges.To + .SQLiteDatabase(connectionString) + .WithScript("Script001", @" +create table test ( + contact_id INTEGER PRIMARY KEY +); +") + .LogScriptOutput() + .LogToConsole() + .Build(); + var result = upgrader.PerformUpgrade(); + result.Successful.ShouldBeTrue(); + } } } -#endif diff --git a/src/Tests/SQLiteTableJournalTests.cs b/src/Tests/SQLiteTableJournalTests.cs index ce0171a..2b02260 100644 --- a/src/Tests/SQLiteTableJournalTests.cs +++ b/src/Tests/SQLiteTableJournalTests.cs @@ -1,11 +1,10 @@ -#if !NETCORE -using System.Collections.Generic; +using System.Collections.Generic; using System.Data; -using System.Data.SQLite; using DbUp.Engine; using DbUp.Engine.Output; using DbUp.Engine.Transactions; using DbUp.Tests.Common; +using Microsoft.Data.Sqlite; using NSubstitute; using Shouldly; using Xunit; @@ -22,7 +21,7 @@ public void dbversion_is_zero_when_journal_table_not_exist() var command = Substitute.For(); dbConnection.CreateCommand().Returns(command); var connectionManager = Substitute.For(); - command.ExecuteScalar().Returns(x => { throw new SQLiteException("table not found"); }); + command.ExecuteScalar().Returns(x => throw new SqliteException("table not found",1)); var consoleUpgradeLog = new ConsoleUpgradeLog(); var journal = new SQLiteTableJournal(() => connectionManager, () => consoleUpgradeLog, "SchemaVersions"); @@ -62,4 +61,3 @@ public void creates_a_new_journal_table_when_not_exist() } } } -#endif diff --git a/src/dbup-sqlite/Helpers/InMemorySQLiteDatabase.cs b/src/dbup-sqlite/Helpers/InMemorySQLiteDatabase.cs index 16bec7b..86bd9f1 100644 --- a/src/dbup-sqlite/Helpers/InMemorySQLiteDatabase.cs +++ b/src/dbup-sqlite/Helpers/InMemorySQLiteDatabase.cs @@ -1,17 +1,7 @@ using System; using DbUp.Engine.Transactions; using DbUp.Helpers; - -#if MONO -using SQLiteConnection = Mono.Data.Sqlite.SqliteConnection; -using SQLiteConnectionStringBuilder = Mono.Data.Sqlite.SqliteConnectionStringBuilder; -using SQLiteJournalModeEnum = Mono.Data.Sqlite.SQLiteJournalModeEnum; -#elif NETCORE -using SQLiteConnection = Microsoft.Data.Sqlite.SqliteConnection; -using SQLiteConnectionStringBuilder = Microsoft.Data.Sqlite.SqliteConnectionStringBuilder; -#else -using System.Data.SQLite; -#endif +using Microsoft.Data.Sqlite; namespace DbUp.SQLite.Helpers { @@ -21,31 +11,23 @@ namespace DbUp.SQLite.Helpers public class InMemorySQLiteDatabase : IDisposable { readonly SQLiteConnectionManager connectionManager; - readonly SQLiteConnection sharedConnection; + readonly SqliteConnection sharedConnection; /// /// Initializes a new instance of the class. /// public InMemorySQLiteDatabase() { - var connectionStringBuilder = new SQLiteConnectionStringBuilder + var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = ":memory:", -#if !NETCORE - Version = 3, - DefaultTimeout = 5, -#if MONO - JournalMode = SQLiteJournalModeEnum.Off, -#else - JournalMode = SQLiteJournalModeEnum.Memory, -#endif - UseUTF16Encoding = true -#endif + Mode = SqliteOpenMode.Memory, + ConnectionString = "PRAGMA encoding='UTF-16'; PRAGMA journal_mode='MEMORY';" }; ConnectionString = connectionStringBuilder.ToString(); connectionManager = new SQLiteConnectionManager(connectionStringBuilder.ConnectionString); - sharedConnection = new SQLiteConnection(connectionStringBuilder.ConnectionString); + sharedConnection = new SqliteConnection(connectionStringBuilder.ConnectionString); sharedConnection.Open(); SqlRunner = new AdHocSqlRunner(() => sharedConnection.CreateCommand(), new SQLiteObjectParser(), null, () => true); } diff --git a/src/dbup-sqlite/Helpers/TemporarySQLiteDatabase.cs b/src/dbup-sqlite/Helpers/TemporarySQLiteDatabase.cs index e1d1911..c69d69b 100644 --- a/src/dbup-sqlite/Helpers/TemporarySQLiteDatabase.cs +++ b/src/dbup-sqlite/Helpers/TemporarySQLiteDatabase.cs @@ -1,17 +1,7 @@ using System; using System.IO; using DbUp.Helpers; - -#if MONO -using SQLiteConnection = Mono.Data.Sqlite.SqliteConnection; -using SQLiteConnectionStringBuilder = Mono.Data.Sqlite.SqliteConnectionStringBuilder; -using SQLiteJournalModeEnum = Mono.Data.Sqlite.SQLiteJournalModeEnum; -#elif NETCORE -using SQLiteConnection = Microsoft.Data.Sqlite.SqliteConnection; -using SQLiteConnectionStringBuilder = Microsoft.Data.Sqlite.SqliteConnectionStringBuilder; -#else -using System.Data.SQLite; -#endif +using Microsoft.Data.Sqlite; namespace DbUp.SQLite.Helpers { @@ -21,7 +11,7 @@ namespace DbUp.SQLite.Helpers public class TemporarySQLiteDatabase : IDisposable { readonly string dataSourcePath; - readonly SQLiteConnection sqLiteConnection; + readonly SqliteConnection sqLiteConnection; /// /// Initializes a new instance of the class. @@ -31,22 +21,12 @@ public TemporarySQLiteDatabase(string name) { dataSourcePath = Path.Combine(Directory.GetCurrentDirectory(), name); - var connectionStringBuilder = new SQLiteConnectionStringBuilder + var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = name, -#if !NETCORE - Version = 3, - DefaultTimeout = 5, -#if MONO - JournalMode = SQLiteJournalModeEnum.Off, -#else - JournalMode = SQLiteJournalModeEnum.Memory, -#endif - UseUTF16Encoding = true -#endif }; - sqLiteConnection = new SQLiteConnection(connectionStringBuilder.ConnectionString); + sqLiteConnection = new SqliteConnection(connectionStringBuilder.ConnectionString); sqLiteConnection.Open(); SharedConnection = new SharedConnection(sqLiteConnection); SqlRunner = new AdHocSqlRunner(() => sqLiteConnection.CreateCommand(), new SQLiteObjectParser(), null, () => true); @@ -59,20 +39,6 @@ public TemporarySQLiteDatabase(string name) public SharedConnection SharedConnection { get; } - /// - /// Creates the database. - /// - public void Create() - { -#if !NETCORE - var filePath = new FileInfo(dataSourcePath); - if (!filePath.Exists) - { - SQLiteConnection.CreateFile(dataSourcePath); - } -#endif - } - /// /// Deletes the database. /// @@ -82,14 +48,6 @@ public void Dispose() if (!filePath.Exists) return; SharedConnection.Dispose(); sqLiteConnection.Dispose(); -#if !NETCORE - SQLiteConnection.ClearAllPools(); - - // SQLite requires all created sql connection/command objects to be disposed - // in order to delete the database file - GC.Collect(2, GCCollectionMode.Forced); - System.Threading.Thread.Sleep(100); -#endif File.Delete(dataSourcePath); } } diff --git a/src/dbup-sqlite/SQLiteConnectionManager.cs b/src/dbup-sqlite/SQLiteConnectionManager.cs index 29f6edc..133ec6e 100644 --- a/src/dbup-sqlite/SQLiteConnectionManager.cs +++ b/src/dbup-sqlite/SQLiteConnectionManager.cs @@ -3,13 +3,7 @@ using System.Text.RegularExpressions; using DbUp.Engine.Transactions; using DbUp.SQLite.Helpers; -#if MONO -using SQLiteConnection = Mono.Data.Sqlite.SqliteConnection; -#elif NETCORE -using SQLiteConnection = Microsoft.Data.Sqlite.SqliteConnection; -#else -using System.Data.SQLite; -#endif +using Microsoft.Data.Sqlite; namespace DbUp.SQLite { @@ -21,7 +15,7 @@ public class SQLiteConnectionManager : DatabaseConnectionManager /// /// Creates new SQLite Connection Manager /// - public SQLiteConnectionManager(string connectionString) : base(l => new SQLiteConnection(connectionString)) + public SQLiteConnectionManager(string connectionString) : base(l => new SqliteConnection(connectionString)) { } @@ -46,4 +40,4 @@ public override IEnumerable SplitScriptIntoCommands(string scriptContent return scriptStatements; } } -} \ No newline at end of file +} diff --git a/src/dbup-sqlite/SQLiteScriptExecutor.cs b/src/dbup-sqlite/SQLiteScriptExecutor.cs index 741b52f..2476ac5 100644 --- a/src/dbup-sqlite/SQLiteScriptExecutor.cs +++ b/src/dbup-sqlite/SQLiteScriptExecutor.cs @@ -4,14 +4,7 @@ using DbUp.Engine.Output; using DbUp.Engine.Transactions; using DbUp.Support; - -#if MONO -using SQLiteException = Mono.Data.Sqlite.SqliteException; -#elif NETCORE -using SQLiteException = Microsoft.Data.Sqlite.SqliteException; -#else -using System.Data.SQLite; -#endif +using Microsoft.Data.Sqlite; namespace DbUp.SQLite { @@ -46,14 +39,10 @@ protected override void ExecuteCommandsWithinExceptionHandler(int index, SqlScri { executeCommand(); } - catch (SQLiteException exception) + catch (SqliteException exception) { Log().WriteInformation("SQLite exception has occurred in script: '{0}'", script.Name); -#if NETCORE - Log().WriteError("Script block number: {0}; Error Code: {1}; Message: {2}", index, exception.SqliteErrorCode, exception.Message); -#else Log().WriteError("Script block number: {0}; Error Code: {1}; Message: {2}", index, exception.ErrorCode, exception.Message); -#endif Log().WriteError(exception.ToString()); throw; } diff --git a/src/dbup-sqlite/dbup-sqlite.csproj b/src/dbup-sqlite/dbup-sqlite.csproj index da35aad..36f8585 100644 --- a/src/dbup-sqlite/dbup-sqlite.csproj +++ b/src/dbup-sqlite/dbup-sqlite.csproj @@ -6,7 +6,7 @@ DbUp Contributors DbUp Copyright © DbUp Contributors 2015 - netstandard1.3;net462 + netstandard2.0;net462 dbup-sqlite DbUp.SQLite dbup-sqlite @@ -22,16 +22,7 @@ - - - - - - - - - - + From 0cd4c56f13969a91e80597f2df02ca3fee560f36 Mon Sep 17 00:00:00 2001 From: Robert Wagner Date: Mon, 29 Jul 2024 16:37:11 +1000 Subject: [PATCH 2/2] Added link to the Issue --- src/Tests/SQLiteSupportTests.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Tests/SQLiteSupportTests.cs b/src/Tests/SQLiteSupportTests.cs index 5174ffb..c861ec1 100644 --- a/src/Tests/SQLiteSupportTests.cs +++ b/src/Tests/SQLiteSupportTests.cs @@ -23,8 +23,11 @@ public void CanUseSQLite() result.Successful.ShouldBe(true); } + /// + /// Test for https://github.com/DbUp/dbup-sqlite/issues/2 + /// [Fact] - public void DoesNotExhibitSafeHandleError_Issue577() + public void DoesNotExhibitSafeHandleError() { var connectionString = "Data source=:memory:";