-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[rc2] SQLite: Make AUTOINCREMENT first-class and fix false pending model changes warning for value converters #36717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
32293b1
Initial plan
Copilot fa6fbfe
Implement first-class SQLite AUTOINCREMENT support
Copilot a9b7973
Add comprehensive tests for SQLite AUTOINCREMENT first-class support
Copilot 738d27f
Finalize SQLite AUTOINCREMENT first-class implementation
Copilot a1ab3b0
Fix SQLite AUTOINCREMENT to work with value converters for issues #30…
Copilot 21c588b
Add SqliteStoreGenerationConvention and SqliteAnnotationCodeGenerator…
Copilot 2698227
Address review feedback: move tests, fix value generation convention …
Copilot e84ae21
Add test for migration consistency between string API and convention …
Copilot e0087b5
Add tests for SQLite annotation code generator in model snapshots
Copilot da1ebe3
Address review feedback: fix annotation code generator logic, remove …
Copilot ca03cda
Address review feedback: rename test class, make GetDefaultValueGener…
Copilot fdb1536
Address review feedback: remove redundant test, make GetDefaultValueG…
Copilot 665a925
Address review feedback: rename test class, remove comments and empty…
Copilot 219a91c
Address review feedback: fix ambiguous GetValueGenerationStrategy cal…
Copilot e533351
Fix TryGetAndRemove method in SqliteAnnotationCodeGenerator
Copilot 3769a12
Address review feedback: remove empty lines and add convention-based …
Copilot 28412a4
Address review feedback: rewrite PR description, confirm issue fixes,…
Copilot 2b52a6b
Fix TryGetAndRemove method in SqliteAnnotationCodeGenerator
Copilot 0754086
Address PR review feedback: remove comments, empty lines, move and re…
Copilot 37f6338
Fix compilation issues in moved CSharpMigrationsGeneratorSqliteTest.c…
Copilot 9685d43
Add UseAutoincrement extension method for TestPropertyBuilder and mov…
Copilot 050c685
Extract common beginning and end fragments using AddBoilerPlate metho…
Copilot 1de78ab
Change assertion to expect SqliteValueGenerationStrategy.None for non…
Copilot b371842
Extract common code to CSharpMigrationsGeneratorTestBase and refactor…
Copilot a34ef8b
Update baselines
AndriySvyryd a0a0eb1
Add baselines
AndriySvyryd 744c2c3
Address PR feedback: Add CanSetValueGenerationStrategy, improve valid…
Copilot eaf05a2
Fix SqliteValueGenerationConvention to inherit from RelationalValueGe…
Copilot f48a50f
Make autoincrement convention more selective - only apply by conventi…
Copilot 782762c
Make convention fully conservative - no autoincrement by default, onl…
Copilot a4919ca
Revert last two commits and fix annotation code generator logic for e…
Copilot 388c332
Update annotation code generator to always include autoincrement anno…
Copilot 85b9a3d
Add ConflictingValueGenerationStrategiesWarning for SQLite with strin…
Copilot f2e7f0f
Address PR feedback: use expression-body syntax and improve property …
Copilot e337cac
Add TPC mapping strategy check and refactor GetDefaultValueGeneration…
Copilot b2e736e
Update baselines
AndriySvyryd c90161c
Address PR feedback: remove unused method, update entity names, fix c…
Copilot 786542b
Fix tests
AndriySvyryd 5e04e23
Add missing methods and cleanup
AndriySvyryd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/EFCore.Sqlite.Core/Metadata/Conventions/SqliteValueGenerationConvention.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| // ReSharper disable once CheckNamespace | ||
|
|
||
| namespace Microsoft.EntityFrameworkCore.Metadata.Conventions; | ||
|
|
||
| /// <summary> | ||
| /// A convention that configures the SQLite value generation strategy for properties. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See <see href="https://aka.ms/efcore-docs-conventions">Model building conventions</see>, and | ||
| /// <see href="https://aka.ms/efcore-docs-sqlite">Accessing SQLite databases with EF Core</see> | ||
| /// for more information and examples. | ||
| /// </remarks> | ||
| public class SqliteValueGenerationConvention : ValueGenerationConvention | ||
AndriySvyryd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| /// <summary> | ||
| /// Creates a new instance of <see cref="SqliteValueGenerationConvention" />. | ||
| /// </summary> | ||
| /// <param name="dependencies">Parameter object containing dependencies for this convention.</param> | ||
| /// <param name="relationalDependencies"> Parameter object containing relational dependencies for this convention.</param> | ||
| public SqliteValueGenerationConvention( | ||
| ProviderConventionSetBuilderDependencies dependencies, | ||
| RelationalConventionSetBuilderDependencies relationalDependencies) | ||
| : base(dependencies) | ||
| { | ||
| RelationalDependencies = relationalDependencies; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Relational provider-specific dependencies for this service. | ||
| /// </summary> | ||
| protected virtual RelationalConventionSetBuilderDependencies RelationalDependencies { get; } | ||
|
|
||
| /// <summary> | ||
| /// Returns the store value generation strategy to set for the given property. | ||
| /// </summary> | ||
| /// <param name="property">The property.</param> | ||
| /// <returns>The strategy to set for the property.</returns> | ||
| protected override ValueGenerated? GetValueGenerated(IConventionProperty property) | ||
| { | ||
| var declaringType = property.DeclaringType; | ||
AndriySvyryd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| var strategy = GetValueGenerationStrategy(property); | ||
| if (strategy == SqliteValueGenerationStrategy.Autoincrement) | ||
| { | ||
| return ValueGenerated.OnAdd; | ||
| } | ||
|
|
||
| return base.GetValueGenerated(property); | ||
| } | ||
|
|
||
| private static SqliteValueGenerationStrategy GetValueGenerationStrategy(IConventionProperty property) | ||
| { | ||
| var entityType = (IConventionEntityType)property.DeclaringType; | ||
| var primaryKey = entityType.FindPrimaryKey(); | ||
| if (primaryKey is { Properties.Count: 1 } | ||
| && primaryKey.Properties[0] == property | ||
| && property.ClrType.UnwrapNullableType().IsInteger()) | ||
| { | ||
| return SqliteValueGenerationStrategy.Autoincrement; | ||
| } | ||
|
|
||
| return SqliteValueGenerationStrategy.None; | ||
AndriySvyryd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/EFCore.Sqlite.Core/Metadata/SqliteValueGenerationStrategy.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| // ReSharper disable once CheckNamespace | ||
|
|
||
| namespace Microsoft.EntityFrameworkCore.Metadata; | ||
|
|
||
| /// <summary> | ||
| /// Defines strategies to use across the EF Core stack when generating key values | ||
| /// from SQLite database columns. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See <see href="https://aka.ms/efcore-docs-conventions">Model building conventions</see>, and | ||
| /// <see href="https://aka.ms/efcore-docs-sqlite">Accessing SQLite databases with EF Core</see> | ||
| /// for more information and examples. | ||
| /// </remarks> | ||
| public enum SqliteValueGenerationStrategy | ||
| { | ||
| /// <summary> | ||
| /// No SQLite-specific strategy | ||
| /// </summary> | ||
| None, | ||
|
|
||
| /// <summary> | ||
| /// A pattern that uses SQLite's AUTOINCREMENT feature to generate values for new entities. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// AUTOINCREMENT can only be used on integer primary key columns in SQLite. | ||
| /// </remarks> | ||
| Autoincrement | ||
| } |
46 changes: 46 additions & 0 deletions
46
src/EFCore.Sqlite.Core/Migrations/Internal/SqliteMigrationsAnnotationProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.EntityFrameworkCore.Sqlite.Metadata.Internal; | ||
|
|
||
| namespace Microsoft.EntityFrameworkCore.Sqlite.Migrations.Internal; | ||
|
|
||
| /// <summary> | ||
| /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
| /// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
| /// 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> | ||
| public class SqliteMigrationsAnnotationProvider : MigrationsAnnotationProvider | ||
AndriySvyryd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of this class. | ||
| /// </summary> | ||
| /// <param name="dependencies">Parameter object containing dependencies for this service.</param> | ||
| #pragma warning disable EF1001 // Internal EF Core API usage. | ||
| public SqliteMigrationsAnnotationProvider(MigrationsAnnotationProviderDependencies dependencies) | ||
| #pragma warning restore EF1001 // Internal EF Core API usage. | ||
| : base(dependencies) | ||
| { | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public override IEnumerable<IAnnotation> ForRemove(IColumn column) | ||
| { | ||
| // Preserve the autoincrement annotation when removing columns for SQLite migrations | ||
| if (column[SqliteAnnotationNames.Autoincrement] as bool? == true) | ||
| { | ||
| yield return new Annotation(SqliteAnnotationNames.Autoincrement, true); | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public override IEnumerable<IAnnotation> ForRename(IColumn column) | ||
| { | ||
| // Preserve the autoincrement annotation when renaming columns for SQLite migrations | ||
| if (column[SqliteAnnotationNames.Autoincrement] as bool? == true) | ||
| { | ||
| yield return new Annotation(SqliteAnnotationNames.Autoincrement, true); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.