提交 6cd895e7 编写于 作者: S Shay Rojansky

Fix post-seeding sequence bumping logic

Fixes #2501
上级 f1a87fa6
......@@ -70,39 +70,22 @@ public class NpgsqlMigrationsSqlGenerator : MigrationsSqlGenerator
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())
......@@ -117,9 +100,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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册