From f1449d9739e13ee91141f74dfdd296108bcc106e Mon Sep 17 00:00:00 2001 From: Marc Sallin Date: Tue, 16 Jun 2026 02:47:19 +0200 Subject: [PATCH] feat: expose default collation through the public initializers SqliteInitializerBase already accepts a default Collation and feeds it into SQL generation, but none of the three concrete initializers forwarded it, so the documented "default collation" feature was unreachable without subclassing. Add constructor overloads that pass a Collation to the base class. --- .../InitializerDefaultCollationTest.cs | 64 +++++++++++++++++++ .../SqliteCreateDatabaseIfNotExists.cs | 23 ++++++- .../SqliteDropCreateDatabaseAlways.cs | 11 +++- ...qliteDropCreateDatabaseWhenModelChanges.cs | 28 ++++++-- 4 files changed, 117 insertions(+), 9 deletions(-) create mode 100644 SQLite.CodeFirst.Test/UnitTests/DbInitializers/InitializerDefaultCollationTest.cs diff --git a/SQLite.CodeFirst.Test/UnitTests/DbInitializers/InitializerDefaultCollationTest.cs b/SQLite.CodeFirst.Test/UnitTests/DbInitializers/InitializerDefaultCollationTest.cs new file mode 100644 index 0000000..256b323 --- /dev/null +++ b/SQLite.CodeFirst.Test/UnitTests/DbInitializers/InitializerDefaultCollationTest.cs @@ -0,0 +1,64 @@ +using System.Data.Entity; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SQLite.CodeFirst.NetCore.Console; + +namespace SQLite.CodeFirst.Test.UnitTests.DbInitializers +{ + /// + /// Verifies that the public initializers forward an explicitly supplied default + /// to the base class, which is what feeds it into the SQL generation. + /// Without this the documented "default collation" feature is unreachable through the shipped initializers. + /// + [TestClass] + public class InitializerDefaultCollationTest + { + private static readonly Collation Collation = new Collation(CollationFunction.RTrim); + + private static DbModelBuilder NewModelBuilder() + { + return new DbModelBuilder(); + } + + [TestMethod] + public void SqliteCreateDatabaseIfNotExists_ForwardsDefaultCollation() + { + var initializer = new SqliteCreateDatabaseIfNotExists(NewModelBuilder(), Collation); + Assert.AreSame(Collation, initializer.DefaultCollation); + } + + [TestMethod] + public void SqliteCreateDatabaseIfNotExists_WithNullByteFlag_ForwardsDefaultCollation() + { + var initializer = new SqliteCreateDatabaseIfNotExists(NewModelBuilder(), true, Collation); + Assert.AreSame(Collation, initializer.DefaultCollation); + } + + [TestMethod] + public void SqliteDropCreateDatabaseAlways_ForwardsDefaultCollation() + { + var initializer = new SqliteDropCreateDatabaseAlways(NewModelBuilder(), Collation); + Assert.AreSame(Collation, initializer.DefaultCollation); + } + + [TestMethod] + public void SqliteDropCreateDatabaseWhenModelChanges_ForwardsDefaultCollation() + { + var initializer = new SqliteDropCreateDatabaseWhenModelChanges(NewModelBuilder(), Collation); + Assert.AreSame(Collation, initializer.DefaultCollation); + } + + [TestMethod] + public void SqliteDropCreateDatabaseWhenModelChanges_WithHistoryType_ForwardsDefaultCollation() + { + var initializer = new SqliteDropCreateDatabaseWhenModelChanges(NewModelBuilder(), typeof(History), Collation); + Assert.AreSame(Collation, initializer.DefaultCollation); + } + + [TestMethod] + public void Initializer_WithoutCollation_HasNullDefaultCollation() + { + var initializer = new SqliteDropCreateDatabaseAlways(NewModelBuilder()); + Assert.IsNull(initializer.DefaultCollation); + } + } +} diff --git a/SQLite.CodeFirst/Public/DbInitializers/SqliteCreateDatabaseIfNotExists.cs b/SQLite.CodeFirst/Public/DbInitializers/SqliteCreateDatabaseIfNotExists.cs index c572be6..6bfd7dd 100644 --- a/SQLite.CodeFirst/Public/DbInitializers/SqliteCreateDatabaseIfNotExists.cs +++ b/SQLite.CodeFirst/Public/DbInitializers/SqliteCreateDatabaseIfNotExists.cs @@ -19,7 +19,16 @@ public class SqliteCreateDatabaseIfNotExists : SqliteInitializerBase /// The model builder. public SqliteCreateDatabaseIfNotExists(DbModelBuilder modelBuilder) - : this(modelBuilder, false) + : this(modelBuilder, false, null) + { } + + /// + /// Initializes a new instance of the class. + /// + /// The model builder. + /// The default collation applied to all string columns. Explicit s take precedence. + public SqliteCreateDatabaseIfNotExists(DbModelBuilder modelBuilder, Collation defaultCollation) + : this(modelBuilder, false, defaultCollation) { } /// @@ -28,7 +37,17 @@ public SqliteCreateDatabaseIfNotExists(DbModelBuilder modelBuilder) /// The model builder. /// if set to true a null byte database file is treated like the database does not exist. public SqliteCreateDatabaseIfNotExists(DbModelBuilder modelBuilder, bool nullByteFileMeansNotExisting) - : base(modelBuilder) + : this(modelBuilder, nullByteFileMeansNotExisting, null) + { } + + /// + /// Initializes a new instance of the class. + /// + /// The model builder. + /// if set to true a null byte database file is treated like the database does not exist. + /// The default collation applied to all string columns. Explicit s take precedence. + public SqliteCreateDatabaseIfNotExists(DbModelBuilder modelBuilder, bool nullByteFileMeansNotExisting, Collation defaultCollation) + : base(modelBuilder, defaultCollation) { this.nullByteFileMeansNotExisting = nullByteFileMeansNotExisting; } diff --git a/SQLite.CodeFirst/Public/DbInitializers/SqliteDropCreateDatabaseAlways.cs b/SQLite.CodeFirst/Public/DbInitializers/SqliteDropCreateDatabaseAlways.cs index 3154835..fb73aee 100644 --- a/SQLite.CodeFirst/Public/DbInitializers/SqliteDropCreateDatabaseAlways.cs +++ b/SQLite.CodeFirst/Public/DbInitializers/SqliteDropCreateDatabaseAlways.cs @@ -17,7 +17,16 @@ public class SqliteDropCreateDatabaseAlways : SqliteInitializerBase /// The model builder. public SqliteDropCreateDatabaseAlways(DbModelBuilder modelBuilder) - : base(modelBuilder) + : this(modelBuilder, null) + { } + + /// + /// Initializes a new instance of the class. + /// + /// The model builder. + /// The default collation applied to all string columns. Explicit s take precedence. + public SqliteDropCreateDatabaseAlways(DbModelBuilder modelBuilder, Collation defaultCollation) + : base(modelBuilder, defaultCollation) { } /// diff --git a/SQLite.CodeFirst/Public/DbInitializers/SqliteDropCreateDatabaseWhenModelChanges.cs b/SQLite.CodeFirst/Public/DbInitializers/SqliteDropCreateDatabaseWhenModelChanges.cs index f8029fc..7e6d834 100644 --- a/SQLite.CodeFirst/Public/DbInitializers/SqliteDropCreateDatabaseWhenModelChanges.cs +++ b/SQLite.CodeFirst/Public/DbInitializers/SqliteDropCreateDatabaseWhenModelChanges.cs @@ -34,11 +34,17 @@ public class SqliteDropCreateDatabaseWhenModelChanges : SqliteInitiali /// /// The model builder. public SqliteDropCreateDatabaseWhenModelChanges(DbModelBuilder modelBuilder) - : base(modelBuilder) - { - historyEntityType = typeof(History); - ConfigureHistoryEntity(); - } + : this(modelBuilder, typeof(History), null) + { } + + /// + /// Initializes a new instance of the class. + /// + /// The model builder. + /// The default collation applied to all string columns. Explicit s take precedence. + public SqliteDropCreateDatabaseWhenModelChanges(DbModelBuilder modelBuilder, Collation defaultCollation) + : this(modelBuilder, typeof(History), defaultCollation) + { } /// /// Initializes a new instance of the class. @@ -46,7 +52,17 @@ public SqliteDropCreateDatabaseWhenModelChanges(DbModelBuilder modelBuilder) /// The model builder. /// Type of the history entity (must implement and provide an parameterless constructor). public SqliteDropCreateDatabaseWhenModelChanges(DbModelBuilder modelBuilder, Type historyEntityType) - : base(modelBuilder) + : this(modelBuilder, historyEntityType, null) + { } + + /// + /// Initializes a new instance of the class. + /// + /// The model builder. + /// Type of the history entity (must implement and provide an parameterless constructor). + /// The default collation applied to all string columns. Explicit s take precedence. + public SqliteDropCreateDatabaseWhenModelChanges(DbModelBuilder modelBuilder, Type historyEntityType, Collation defaultCollation) + : base(modelBuilder, defaultCollation) { this.historyEntityType = historyEntityType; ConfigureHistoryEntity();