58 lines
2.7 KiB
C#
58 lines
2.7 KiB
C#
using System;
|
|
using Microsoft.EntityFrameworkCore.Migrations;
|
|
|
|
#nullable disable
|
|
|
|
namespace gehGassi.Persistence.Migrations
|
|
{
|
|
/// <inheritdoc />
|
|
public partial class CustomerDateOnlyChanges : Migration
|
|
{
|
|
/// <inheritdoc />
|
|
protected override void Up(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.AlterColumn<DateTimeOffset>(
|
|
name: "MangopayContactBirthdate",
|
|
table: "Customers",
|
|
type: "datetimeoffset",
|
|
nullable: true,
|
|
oldClrType: typeof(DateOnly),
|
|
oldType: "date",
|
|
oldNullable: 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.AlterColumn<DateOnly>(
|
|
name: "MangopayContactBirthdate",
|
|
table: "Customers",
|
|
type: "date",
|
|
nullable: true,
|
|
oldClrType: typeof(DateTimeOffset),
|
|
oldType: "datetimeoffset",
|
|
oldNullable: 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");
|
|
}
|
|
}
|
|
}
|