82 lines
4.1 KiB
C#
82 lines
4.1 KiB
C#
using Microsoft.EntityFrameworkCore.Migrations;
|
|
using NetTopologySuite.Geometries;
|
|
using System;
|
|
|
|
#nullable disable
|
|
|
|
namespace gehGassi.Persistence.Migrations
|
|
{
|
|
public partial class ListingLocation : Migration
|
|
{
|
|
protected override void Up(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.DropColumn(
|
|
name: "Lat",
|
|
table: "Listings");
|
|
|
|
migrationBuilder.DropColumn(
|
|
name: "Lng",
|
|
table: "Listings");
|
|
|
|
migrationBuilder.AddColumn<Point>(
|
|
name: "Location",
|
|
table: "Listings",
|
|
type: "geography",
|
|
nullable: true);
|
|
|
|
var viewName = "tv_ListingsWithNames";
|
|
var sql = @"CREATE FUNCTION tv_ListingsWithNames (@language nvarchar(2), @fallback nvarchar(2)) RETURNS TABLE AS RETURN
|
|
SELECT dbo.Listings.*,
|
|
C.Name AS CustomerName,
|
|
BR.Name AS BranchName,
|
|
Isnull(JSON_VALUE(dbo.Listings.NameLocalized, '$.' + @language), JSON_VALUE(dbo.Listings.NameLocalized, '$.' + @fallback)) AS Name,
|
|
Isnull(JSON_VALUE(dbo.Listings.DescriptionLocalized, '$.' + @language), JSON_VALUE(dbo.Listings.DescriptionLocalized, '$.' + @fallback)) AS Description,
|
|
JSON_VALUE(dbo.Listings.ImageLanguageLocalized, '$.' + @language) AS ImageLanguage,
|
|
JSON_VALUE(dbo.Listings.UrlLanguageLocalized, '$.' + @language) AS UrlLanguage
|
|
FROM dbo.Listings
|
|
LEFT JOIN dbo.tv_CustomersWithNames(@language, @fallback) C ON dbo.Listings.CustomerId = C.Id
|
|
LEFT JOIN dbo.tv_BranchesWithNames(@language, @fallback) BR ON dbo.Listings.BranchId = BR.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");
|
|
}
|
|
|
|
protected override void Down(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.DropColumn(
|
|
name: "Location",
|
|
table: "Listings");
|
|
|
|
migrationBuilder.AddColumn<double>(
|
|
name: "Lat",
|
|
table: "Listings",
|
|
type: "float",
|
|
nullable: false,
|
|
defaultValue: 0.0);
|
|
|
|
migrationBuilder.AddColumn<double>(
|
|
name: "Lng",
|
|
table: "Listings",
|
|
type: "float",
|
|
nullable: false,
|
|
defaultValue: 0.0);
|
|
|
|
var viewName = "tv_ListingsWithNames";
|
|
var sql = @"CREATE FUNCTION tv_ListingsWithNames (@language nvarchar(2), @fallback nvarchar(2)) RETURNS TABLE AS RETURN
|
|
SELECT dbo.Listings.*,
|
|
C.Name AS CustomerName,
|
|
BR.Name AS BranchName,
|
|
Isnull(JSON_VALUE(dbo.Listings.NameLocalized, '$.' + @language), JSON_VALUE(dbo.Listings.NameLocalized, '$.' + @fallback)) AS Name,
|
|
Isnull(JSON_VALUE(dbo.Listings.DescriptionLocalized, '$.' + @language), JSON_VALUE(dbo.Listings.DescriptionLocalized, '$.' + @fallback)) AS Description,
|
|
JSON_VALUE(dbo.Listings.ImageLanguageLocalized, '$.' + @language) AS ImageLanguage,
|
|
JSON_VALUE(dbo.Listings.UrlLanguageLocalized, '$.' + @language) AS UrlLanguage
|
|
FROM dbo.Listings
|
|
LEFT JOIN dbo.tv_CustomersWithNames(@language, @fallback) C ON dbo.Listings.CustomerId = C.Id
|
|
LEFT JOIN dbo.tv_BranchesWithNames(@language, @fallback) BR ON dbo.Listings.BranchId = BR.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");
|
|
}
|
|
}
|
|
}
|