65 lines
2.5 KiB
C#
65 lines
2.5 KiB
C#
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
using Microsoft.EntityFrameworkCore.Migrations;
|
|
using line_gestao_api.Data;
|
|
|
|
#nullable disable
|
|
|
|
namespace line_gestao_api.Migrations
|
|
{
|
|
[DbContext(typeof(AppDbContext))]
|
|
[Migration("20260303193000_FixAparelhosArquivoPathColumns")]
|
|
public partial class FixAparelhosArquivoPathColumns : Migration
|
|
{
|
|
protected override void Up(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.Sql("""
|
|
ALTER TABLE "Aparelhos"
|
|
ADD COLUMN IF NOT EXISTS "NotaFiscalArquivoPath" character varying(500) NULL;
|
|
""");
|
|
|
|
migrationBuilder.Sql("""
|
|
ALTER TABLE "Aparelhos"
|
|
ADD COLUMN IF NOT EXISTS "ReciboArquivoPath" character varying(500) NULL;
|
|
""");
|
|
|
|
// Backfill seguro para bancos que já tinham os campos antigos de URL/nome.
|
|
migrationBuilder.Sql("""
|
|
DO $$
|
|
BEGIN
|
|
IF EXISTS (
|
|
SELECT 1
|
|
FROM information_schema.columns
|
|
WHERE table_name = 'Aparelhos'
|
|
AND column_name = 'NotaFiscalAnexoUrl'
|
|
) THEN
|
|
UPDATE "Aparelhos"
|
|
SET "NotaFiscalArquivoPath" = COALESCE("NotaFiscalArquivoPath", "NotaFiscalAnexoUrl")
|
|
WHERE "NotaFiscalAnexoUrl" IS NOT NULL
|
|
AND "NotaFiscalAnexoUrl" <> '';
|
|
END IF;
|
|
|
|
IF EXISTS (
|
|
SELECT 1
|
|
FROM information_schema.columns
|
|
WHERE table_name = 'Aparelhos'
|
|
AND column_name = 'ReciboAnexoUrl'
|
|
) THEN
|
|
UPDATE "Aparelhos"
|
|
SET "ReciboArquivoPath" = COALESCE("ReciboArquivoPath", "ReciboAnexoUrl")
|
|
WHERE "ReciboAnexoUrl" IS NOT NULL
|
|
AND "ReciboAnexoUrl" <> '';
|
|
END IF;
|
|
END
|
|
$$;
|
|
""");
|
|
}
|
|
|
|
protected override void Down(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.Sql("""ALTER TABLE "Aparelhos" DROP COLUMN IF EXISTS "ReciboArquivoPath";""");
|
|
migrationBuilder.Sql("""ALTER TABLE "Aparelhos" DROP COLUMN IF EXISTS "NotaFiscalArquivoPath";""");
|
|
}
|
|
}
|
|
}
|
|
|