Skip to content

Commit

Permalink
#7377 Marked Order.OrderGuid field as unique
Browse files Browse the repository at this point in the history
  • Loading branch information
skoshelev committed Oct 29, 2024
1 parent 7ed9afa commit 112994d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public override void MapEntity(CreateTableExpressionBuilder table)
.WithColumn(nameof(Order.CustomerId)).AsInt32().ForeignKey<Customer>(onDelete: Rule.None)
.WithColumn(nameof(Order.PickupAddressId)).AsInt32().Nullable().ForeignKey<Address>(onDelete: Rule.None)
.WithColumn(nameof(Order.ShippingAddressId)).AsInt32().Nullable().ForeignKey<Address>(onDelete: Rule.None)
.WithColumn(nameof(Order.CustomerIp)).AsString(100).Nullable();
.WithColumn(nameof(Order.CustomerIp)).AsString(100).Nullable()
.WithColumn(nameof(Order.OrderGuid)).AsGuid().Unique("AK_Order_OrderGuid");
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
using FluentMigrator;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Orders;

namespace Nop.Data.Migrations.UpgradeTo480;

[NopSchemaMigration("2024-06-21 19:53:00", "AddIndexesMigration for 4.80.0")]
[NopSchemaMigration("2024-10-29 20:02:00", "AddIndexesMigration for 4.80.0")]
public class AddIndexesMigration : ForwardOnlyMigration
{
private readonly INopDataProvider _dataProvider;

public AddIndexesMigration(INopDataProvider dataProvider)
{
_dataProvider = dataProvider;
}

/// <summary>
/// Collect the UP migration expressions
/// </summary>
Expand All @@ -16,5 +24,26 @@ public override void Up()
.OnTable(nameof(Customer))
.OnColumn(nameof(Customer.Deleted)).Ascending()
.WithOptions().NonClustered();

//#7377
if (!Schema.Table(nameof(Order)).Constraint("AK_Order_OrderGuid").Exists())
{
var orders = _dataProvider.GetTable<Order>().GroupBy(p => p.OrderGuid, p => p)
.Where(p => p.Count() > 1)
.SelectMany(p => p)
.ToList();

if (orders.Any())
{
foreach (var order in orders)
order.OrderGuid = Guid.NewGuid();

_dataProvider.UpdateEntities(orders);
}

Create.UniqueConstraint("AK_Order_OrderGuid")
.OnTable(nameof(Order))
.Column(nameof(Order.OrderGuid));
}
}
}

0 comments on commit 112994d

Please sign in to comment.