From 347ac0a78a51773dc33e1476385808993ac6374b Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Fri, 9 Sep 2022 12:52:06 +0200 Subject: [PATCH] Fix post-seeding sequence bumping logic Fixes #2501 (cherry picked from commit 6cd895e713ff4df6db8df9176c39bc30369c680c) --- .../NpgsqlMigrationsSqlGenerator.cs | 45 ++++++------------- 1 file changed, 14 insertions(+), 31 deletions(-) diff --git a/src/EFCore.PG/Migrations/NpgsqlMigrationsSqlGenerator.cs b/src/EFCore.PG/Migrations/NpgsqlMigrationsSqlGenerator.cs index 6d193830a..ca1ad9c99 100644 --- a/src/EFCore.PG/Migrations/NpgsqlMigrationsSqlGenerator.cs +++ b/src/EFCore.PG/Migrations/NpgsqlMigrationsSqlGenerator.cs @@ -61,39 +61,22 @@ public override IReadOnlyList Generate( void AddSequenceBumpingForSeeding() { - // For all tables where we had data seeding insertions, get any identity/serial columns for those tables. + // For all tables where we had data seeding insertions, find all columns mapped to properties with identity/serial value + // generation strategy. We'll bump the sequences for those columns. var seededGeneratedColumns = operations .OfType() .Select(o => new { o.Schema, o.Table }) .Distinct() - .Select( - t => new - { - t.Schema, - t.Table, - Columns = (model?.GetRelationalModel().FindTable(t.Table, t.Schema) - ?.EntityTypeMappings.Select(m => m.EntityType) - ?? Enumerable.Empty()) - .SelectMany( - e => e.GetDeclaredProperties() - .Where( - p => p.GetValueGenerationStrategy() switch - { - NpgsqlValueGenerationStrategy.IdentityByDefaultColumn => true, - NpgsqlValueGenerationStrategy.IdentityAlwaysColumn => true, - NpgsqlValueGenerationStrategy.SerialColumn => true, - _ => false - }) - .Select(p => p.GetColumnName(StoreObjectIdentifier.Table(t.Table, t.Schema)))) - }) .SelectMany( - t => t.Columns.Select( - p => new - { - t.Schema, - t.Table, - Column = p!, - })) + t => model?.GetRelationalModel().FindTable(t.Table, t.Schema)?.Columns + .Where( + c => c.PropertyMappings.Any( + p => p.Property.GetValueGenerationStrategy() is + NpgsqlValueGenerationStrategy.IdentityByDefaultColumn + or NpgsqlValueGenerationStrategy.IdentityAlwaysColumn + or NpgsqlValueGenerationStrategy.SerialColumn)) + ?? Enumerable.Empty()) + .Distinct() .ToArray(); if (!seededGeneratedColumns.Any()) @@ -108,9 +91,9 @@ void AddSequenceBumpingForSeeding() // Weirdly, pg_get_serial_sequence accepts a standard quoted "schema"."table" inside its first // parameter string literal, but the second one is a column name that shouldn't be double-quoted... - var table = Dependencies.SqlGenerationHelper.DelimitIdentifier(c.Table, c.Schema); - var column = Dependencies.SqlGenerationHelper.DelimitIdentifier(c.Column!); - var unquotedColumn = c.Column.Replace("'", "''"); + var table = Dependencies.SqlGenerationHelper.DelimitIdentifier(c.Table.Name, c.Table.Schema); + var column = Dependencies.SqlGenerationHelper.DelimitIdentifier(c.Name); + var unquotedColumn = c.Name.Replace("'", "''"); // When generating idempotent scripts, migration DDL is enclosed in anonymous DO blocks, // where PERFORM must be used instead of SELECT