119 lines
4.0 KiB
C#
119 lines
4.0 KiB
C#
using System.Collections.Generic;
|
|
using Commerce.Contracts.DTOs;
|
|
using Microsoft.EntityFrameworkCore.Migrations;
|
|
|
|
#nullable disable
|
|
|
|
namespace Commerce.Domain.Migrations
|
|
{
|
|
/// <inheritdoc />
|
|
public partial class NestedTranslations : Migration
|
|
{
|
|
/// <inheritdoc />
|
|
protected override void Up(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.RenameColumn(
|
|
name: "Name",
|
|
table: "Products",
|
|
newName: "Translations");
|
|
|
|
migrationBuilder.RenameColumn(
|
|
name: "Name",
|
|
table: "Categories",
|
|
newName: "Translations");
|
|
|
|
// Data Transformation for Products
|
|
migrationBuilder.Sql(@"
|
|
UPDATE ""Products""
|
|
SET ""Translations"" = (
|
|
SELECT jsonb_agg(
|
|
jsonb_build_object(
|
|
'Language', key,
|
|
'Info', jsonb_build_object(
|
|
'Name', value,
|
|
'Description', COALESCE(""Description""->>key, ''),
|
|
'TechnicalDetails', COALESCE(""TechnicalDetails""->key, '[]'::jsonb)
|
|
)
|
|
)
|
|
)
|
|
FROM jsonb_each_text(""Translations"")
|
|
)
|
|
WHERE jsonb_typeof(""Translations"") = 'object';
|
|
");
|
|
|
|
// Data Transformation for Categories
|
|
migrationBuilder.Sql(@"
|
|
UPDATE ""Categories""
|
|
SET ""Translations"" = (
|
|
SELECT jsonb_agg(
|
|
jsonb_build_object(
|
|
'Language', key,
|
|
'Info', jsonb_build_object(
|
|
'Name', value,
|
|
'Description', COALESCE(""Description""->>key, ''),
|
|
'TechnicalDetails', COALESCE(""TechnicalDetails""->key, '[]'::jsonb)
|
|
)
|
|
)
|
|
)
|
|
FROM jsonb_each_text(""Translations"")
|
|
)
|
|
WHERE jsonb_typeof(""Translations"") = 'object';
|
|
");
|
|
|
|
migrationBuilder.DropColumn(
|
|
name: "Description",
|
|
table: "Products");
|
|
|
|
migrationBuilder.DropColumn(
|
|
name: "TechnicalDetails",
|
|
table: "Products");
|
|
|
|
migrationBuilder.DropColumn(
|
|
name: "Description",
|
|
table: "Categories");
|
|
|
|
migrationBuilder.DropColumn(
|
|
name: "TechnicalDetails",
|
|
table: "Categories");
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override void Down(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.RenameColumn(
|
|
name: "Translations",
|
|
table: "Products",
|
|
newName: "Name");
|
|
|
|
migrationBuilder.RenameColumn(
|
|
name: "Translations",
|
|
table: "Categories",
|
|
newName: "Name");
|
|
|
|
migrationBuilder.AddColumn<Dictionary<string, string>>(
|
|
name: "Description",
|
|
table: "Products",
|
|
type: "jsonb",
|
|
nullable: false);
|
|
|
|
migrationBuilder.AddColumn<Dictionary<string, List<TechnicalDetail>>>(
|
|
name: "TechnicalDetails",
|
|
table: "Products",
|
|
type: "jsonb",
|
|
nullable: true);
|
|
|
|
migrationBuilder.AddColumn<Dictionary<string, string>>(
|
|
name: "Description",
|
|
table: "Categories",
|
|
type: "jsonb",
|
|
nullable: false);
|
|
|
|
migrationBuilder.AddColumn<Dictionary<string, List<TechnicalDetail>>>(
|
|
name: "TechnicalDetails",
|
|
table: "Categories",
|
|
type: "jsonb",
|
|
nullable: true);
|
|
}
|
|
}
|
|
}
|