2019-11-15 15:50:20 -05:00
|
|
|
|
using Microsoft.EntityFrameworkCore.Migrations;
|
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
namespace Data.Migrations.Postgresql
|
2019-11-15 15:50:20 -05:00
|
|
|
|
{
|
|
|
|
|
public partial class EnforceUniqueIndexForEFAliasIPName : Migration
|
|
|
|
|
{
|
|
|
|
|
protected override void Up(MigrationBuilder migrationBuilder)
|
|
|
|
|
{
|
|
|
|
|
if (migrationBuilder.ActiveProvider == "Microsoft.EntityFrameworkCore.Sqlite")
|
|
|
|
|
{
|
2019-11-25 13:05:12 -05:00
|
|
|
|
migrationBuilder.Sql(@"DROP TABLE IF EXISTS DUPLICATE_ALIASES;
|
|
|
|
|
CREATE TABLE DUPLICATE_ALIASES AS
|
|
|
|
|
SELECT
|
|
|
|
|
MIN(AliasId) MIN,
|
|
|
|
|
MAX(AliasId) MAX,
|
|
|
|
|
LinkId
|
|
|
|
|
FROM
|
|
|
|
|
EFAlias
|
|
|
|
|
WHERE
|
|
|
|
|
(IPAddress, NAME) IN(
|
|
|
|
|
SELECT DISTINCT
|
|
|
|
|
IPAddress,
|
|
|
|
|
NAME
|
|
|
|
|
FROM
|
|
|
|
|
EFAlias
|
|
|
|
|
GROUP BY
|
|
|
|
|
EFAlias.IPAddress,
|
|
|
|
|
NAME
|
|
|
|
|
HAVING
|
|
|
|
|
COUNT(IPAddress) > 1 AND COUNT(NAME) > 1
|
|
|
|
|
)
|
|
|
|
|
GROUP BY
|
|
|
|
|
IPAddress
|
|
|
|
|
ORDER BY
|
|
|
|
|
IPAddress;
|
|
|
|
|
|
|
|
|
|
UPDATE
|
|
|
|
|
EFClients
|
|
|
|
|
SET CurrentAliasId = (SELECT MAX FROM DUPLICATE_ALIASES WHERE CurrentAliasId = MIN)
|
|
|
|
|
WHERE
|
|
|
|
|
CurrentAliasId IN(
|
|
|
|
|
SELECT
|
|
|
|
|
MIN
|
|
|
|
|
FROM
|
|
|
|
|
DUPLICATE_ALIASES
|
|
|
|
|
);
|
|
|
|
|
DELETE
|
|
|
|
|
FROM
|
|
|
|
|
EFAlias
|
|
|
|
|
WHERE
|
|
|
|
|
AliasId IN(
|
|
|
|
|
SELECT
|
|
|
|
|
MIN
|
|
|
|
|
FROM
|
|
|
|
|
DUPLICATE_ALIASES
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
DROP TABLE
|
|
|
|
|
DUPLICATE_ALIASES;");
|
2019-11-15 15:50:20 -05:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (migrationBuilder.ActiveProvider == "Pomelo.EntityFrameworkCore.MySql")
|
|
|
|
|
{
|
2019-12-03 16:56:00 -05:00
|
|
|
|
migrationBuilder.Sql(@"CREATE TABLE DUPLICATE_ALIASES
|
2019-11-15 15:50:20 -05:00
|
|
|
|
SELECT
|
|
|
|
|
MIN(`AliasId`) `MIN`,
|
|
|
|
|
MAX(`AliasId`) `MAX`,
|
|
|
|
|
`LinkId`
|
|
|
|
|
FROM
|
|
|
|
|
`EFAlias`
|
|
|
|
|
WHERE
|
|
|
|
|
(`IPAddress`, `NAME`) IN(
|
|
|
|
|
SELECT DISTINCT
|
|
|
|
|
`IPAddress`,
|
|
|
|
|
`NAME`
|
|
|
|
|
FROM
|
|
|
|
|
`EFAlias`
|
|
|
|
|
GROUP BY
|
|
|
|
|
`EFAlias`.`IPAddress`,
|
|
|
|
|
`NAME`
|
|
|
|
|
HAVING
|
|
|
|
|
COUNT(`IPAddress`) > 1 AND COUNT(`NAME`) > 1
|
|
|
|
|
)
|
|
|
|
|
GROUP BY
|
|
|
|
|
`IPAddress`
|
|
|
|
|
ORDER BY
|
|
|
|
|
`IPAddress`;
|
|
|
|
|
SET
|
|
|
|
|
SQL_SAFE_UPDATES = 0;
|
|
|
|
|
UPDATE
|
|
|
|
|
`EFClients` AS `Client`
|
|
|
|
|
JOIN
|
|
|
|
|
DUPLICATE_ALIASES `Duplicate`
|
|
|
|
|
ON
|
|
|
|
|
`Client`.CurrentAliasId = `Duplicate`.`MIN`
|
|
|
|
|
SET
|
|
|
|
|
`Client`.CurrentAliasId = `Duplicate`.`MAX`
|
|
|
|
|
WHERE
|
|
|
|
|
`Client`.`CurrentAliasId` IN(
|
|
|
|
|
SELECT
|
|
|
|
|
`MIN`
|
|
|
|
|
FROM
|
|
|
|
|
DUPLICATE_ALIASES
|
|
|
|
|
);
|
|
|
|
|
DELETE
|
|
|
|
|
FROM
|
|
|
|
|
`EFAlias`
|
|
|
|
|
WHERE
|
|
|
|
|
`AliasId` IN(
|
|
|
|
|
SELECT
|
|
|
|
|
`MIN`
|
|
|
|
|
FROM
|
|
|
|
|
DUPLICATE_ALIASES
|
|
|
|
|
);
|
|
|
|
|
SET
|
|
|
|
|
SQL_SAFE_UPDATES = 1;
|
|
|
|
|
DROP TABLE
|
|
|
|
|
DUPLICATE_ALIASES;");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-11-25 13:05:12 -05:00
|
|
|
|
migrationBuilder.Sql(@"CREATE TEMPORARY TABLE DUPLICATE_ALIASES AS
|
|
|
|
|
SELECT
|
|
|
|
|
MIN(""AliasId"") ""MIN"",
|
|
|
|
|
MAX(""AliasId"") ""MAX"",
|
|
|
|
|
MIN(""LinkId"") ""LinkId""
|
|
|
|
|
FROM
|
|
|
|
|
""EFAlias""
|
|
|
|
|
WHERE
|
|
|
|
|
(""IPAddress"", ""Name"") IN(
|
|
|
|
|
SELECT DISTINCT
|
|
|
|
|
""IPAddress"",
|
|
|
|
|
""Name""
|
|
|
|
|
FROM
|
|
|
|
|
""EFAlias""
|
|
|
|
|
GROUP BY
|
|
|
|
|
""EFAlias"".""IPAddress"",
|
|
|
|
|
""Name""
|
|
|
|
|
HAVING
|
|
|
|
|
COUNT(""IPAddress"") > 1 AND COUNT(""Name"") > 1
|
|
|
|
|
)
|
|
|
|
|
GROUP BY
|
|
|
|
|
""IPAddress""
|
|
|
|
|
ORDER BY
|
|
|
|
|
""IPAddress"";
|
|
|
|
|
UPDATE
|
|
|
|
|
""EFClients"" AS ""Client""
|
|
|
|
|
SET
|
|
|
|
|
""CurrentAliasId"" = ""Duplicate"".""MAX""
|
|
|
|
|
FROM
|
|
|
|
|
DUPLICATE_ALIASES ""Duplicate""
|
|
|
|
|
WHERE
|
|
|
|
|
""Client"".""CurrentAliasId"" IN(
|
|
|
|
|
SELECT
|
|
|
|
|
""MIN""
|
|
|
|
|
FROM
|
|
|
|
|
DUPLICATE_ALIASES
|
|
|
|
|
)
|
|
|
|
|
AND
|
|
|
|
|
""Client"".""CurrentAliasId"" = ""Duplicate"".""MIN"";
|
|
|
|
|
DELETE
|
|
|
|
|
FROM
|
|
|
|
|
""EFAlias""
|
|
|
|
|
WHERE
|
|
|
|
|
""AliasId"" IN(
|
|
|
|
|
SELECT
|
|
|
|
|
""MIN""
|
|
|
|
|
FROM
|
|
|
|
|
DUPLICATE_ALIASES
|
|
|
|
|
);
|
|
|
|
|
DROP TABLE
|
|
|
|
|
DUPLICATE_ALIASES;");
|
2019-11-15 15:50:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
migrationBuilder.CreateIndex(
|
|
|
|
|
name: "IX_EFAlias_Name_IPAddress",
|
|
|
|
|
table: "EFAlias",
|
|
|
|
|
columns: new[] { "Name", "IPAddress" },
|
|
|
|
|
unique: true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Down(MigrationBuilder migrationBuilder)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|