Skip to content

Commit

Permalink
Fix post-seeding sequence bumping logic
Browse files Browse the repository at this point in the history
Fixes #2501

(cherry picked from commit 6cd895e)
  • Loading branch information
roji committed Sep 9, 2022
1 parent 395f737 commit 347ac0a
Showing 1 changed file with 14 additions and 31 deletions.
45 changes: 14 additions & 31 deletions src/EFCore.PG/Migrations/NpgsqlMigrationsSqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,39 +61,22 @@ public override IReadOnlyList<MigrationCommand> 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<InsertDataOperation>()
.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<IEntityType>())
.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<IColumn>())
.Distinct()
.ToArray();

if (!seededGeneratedColumns.Any())
Expand All @@ -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
Expand Down

0 comments on commit 347ac0a

Please sign in to comment.