gehgassi_backend/gehGassi.Persistence/Migrations/20240221163820_CustomerMangopayAdded.cs

170 lines
6.2 KiB
C#

using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace gehGassi.Persistence.Migrations
{
/// <inheritdoc />
public partial class CustomerMangopayAdded : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "MangopayAddress",
table: "Customers",
type: "nvarchar(max)",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "MangopayCompanyName",
table: "Customers",
type: "nvarchar(256)",
maxLength: 256,
nullable: true);
migrationBuilder.AddColumn<DateOnly>(
name: "MangopayContactBirthdate",
table: "Customers",
type: "date",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "MangopayContactEmail",
table: "Customers",
type: "nvarchar(255)",
maxLength: 255,
nullable: true);
migrationBuilder.AddColumn<string>(
name: "MangopayContactFirstName",
table: "Customers",
type: "nvarchar(100)",
maxLength: 100,
nullable: true);
migrationBuilder.AddColumn<string>(
name: "MangopayContactLastName",
table: "Customers",
type: "nvarchar(100)",
maxLength: 100,
nullable: true);
migrationBuilder.AddColumn<string>(
name: "MangopayContactMainResidenceCode",
table: "Customers",
type: "nvarchar(2)",
maxLength: 2,
nullable: true);
migrationBuilder.AddColumn<string>(
name: "MangopayContactNationality",
table: "Customers",
type: "nvarchar(2)",
maxLength: 2,
nullable: true);
migrationBuilder.AddColumn<string>(
name: "MangopayEmail",
table: "Customers",
type: "nvarchar(255)",
maxLength: 255,
nullable: true);
migrationBuilder.AddColumn<bool>(
name: "MangopayIdCreated",
table: "Customers",
type: "bit",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<string>(
name: "MangopayPaymentId",
table: "Customers",
type: "nvarchar(255)",
maxLength: 255,
nullable: true);
migrationBuilder.AddColumn<string>(
name: "MangopayRegistrationNumber",
table: "Customers",
type: "nvarchar(50)",
maxLength: 50,
nullable: true);
var viewName = "tv_CustomersWithNames";
var sql = @"CREATE FUNCTION tv_CustomersWithNames (@language nvarchar(2), @fallback nvarchar(2)) RETURNS TABLE AS RETURN
SELECT dbo.Customers.*, dbo.CustomerTypes.Name AS TypeName, BN.Name AS BranchName
FROM dbo.Customers
LEFT JOIN dbo.CustomerTypes ON dbo.Customers.TypeId = dbo.CustomerTypes.Id
LEFT JOIN dbo.tv_BranchesWithNames(@language, @fallback) BN ON dbo.Customers.BranchId = BN.Id";
migrationBuilder.Sql($"IF(exists (SELECT 1 FROM sys.objects WHERE Name = '{viewName}')) BEGIN DROP FUNCTION [dbo].[{viewName}] END {Environment.NewLine} GO");
migrationBuilder.Sql($"{sql} {Environment.NewLine} GO");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "MangopayAddress",
table: "Customers");
migrationBuilder.DropColumn(
name: "MangopayCompanyName",
table: "Customers");
migrationBuilder.DropColumn(
name: "MangopayContactBirthdate",
table: "Customers");
migrationBuilder.DropColumn(
name: "MangopayContactEmail",
table: "Customers");
migrationBuilder.DropColumn(
name: "MangopayContactFirstName",
table: "Customers");
migrationBuilder.DropColumn(
name: "MangopayContactLastName",
table: "Customers");
migrationBuilder.DropColumn(
name: "MangopayContactMainResidenceCode",
table: "Customers");
migrationBuilder.DropColumn(
name: "MangopayContactNationality",
table: "Customers");
migrationBuilder.DropColumn(
name: "MangopayEmail",
table: "Customers");
migrationBuilder.DropColumn(
name: "MangopayIdCreated",
table: "Customers");
migrationBuilder.DropColumn(
name: "MangopayPaymentId",
table: "Customers");
migrationBuilder.DropColumn(
name: "MangopayRegistrationNumber",
table: "Customers");
var viewName = "tv_CustomersWithNames";
var sql = @"CREATE FUNCTION tv_CustomersWithNames (@language nvarchar(2), @fallback nvarchar(2)) RETURNS TABLE AS RETURN
SELECT dbo.Customers.*, dbo.CustomerTypes.Name AS TypeName, BN.Name AS BranchName
FROM dbo.Customers
LEFT JOIN dbo.CustomerTypes ON dbo.Customers.TypeId = dbo.CustomerTypes.Id
LEFT JOIN dbo.tv_BranchesWithNames(@language, @fallback) BN ON dbo.Customers.BranchId = BN.Id";
migrationBuilder.Sql($"IF(exists (SELECT 1 FROM sys.objects WHERE Name = '{viewName}')) BEGIN DROP FUNCTION [dbo].[{viewName}] END {Environment.NewLine} GO");
migrationBuilder.Sql($"{sql} {Environment.NewLine} GO");
}
}
}