using System; using Microsoft.EntityFrameworkCore.Migrations; #nullable disable namespace HrynCo.NotificationService.DAL.EF.Migrations { /// public partial class AddChannelTemplates : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( name: "templates", columns: table => new { id = table.Column(type: "uuid", nullable: false), service_id = table.Column(type: "uuid", nullable: false), key = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), created = table.Column(type: "timestamp with time zone", nullable: false), updated = table.Column(type: "timestamp with time zone", nullable: true) }, constraints: table => { table.PrimaryKey("PK_templates", x => x.id); table.UniqueConstraint("AK_templates_service_id_key", x => new { x.service_id, x.key }); table.ForeignKey( name: "FK_templates_services_service_id", column: x => x.service_id, principalTable: "services", principalColumn: "id", onDelete: ReferentialAction.Restrict); }); migrationBuilder.CreateTable( name: "push_template_definitions", columns: table => new { id = table.Column(type: "uuid", nullable: false), service_id = table.Column(type: "uuid", nullable: false), key = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), revision = table.Column(type: "uuid", nullable: false), created = table.Column(type: "timestamp with time zone", nullable: false), updated = table.Column(type: "timestamp with time zone", nullable: true), variables = table.Column(type: "jsonb", nullable: true) }, constraints: table => { table.PrimaryKey("PK_push_template_definitions", x => x.id); table.UniqueConstraint("AK_push_template_definitions_id_service_id", x => new { x.id, x.service_id }); table.ForeignKey( name: "FK_push_template_definitions_templates_service_id_key", columns: x => new { x.service_id, x.key }, principalTable: "templates", principalColumns: new[] { "service_id", "key" }, onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( name: "push_templates", columns: table => new { id = table.Column(type: "uuid", nullable: false), definition_id = table.Column(type: "uuid", nullable: false), service_id = table.Column(type: "uuid", nullable: false), service_language_id = table.Column(type: "uuid", nullable: false), title = table.Column(type: "text", nullable: false), body = table.Column(type: "text", nullable: false), created = table.Column(type: "timestamp with time zone", nullable: false), updated = table.Column(type: "timestamp with time zone", nullable: true) }, constraints: table => { table.PrimaryKey("PK_push_templates", x => x.id); table.ForeignKey( name: "FK_push_templates_push_template_definitions_definition_id_serv~", columns: x => new { x.definition_id, x.service_id }, principalTable: "push_template_definitions", principalColumns: new[] { "id", "service_id" }, onDelete: ReferentialAction.Cascade); table.ForeignKey( name: "FK_push_templates_service_languages_service_language_id_servic~", columns: x => new { x.service_language_id, x.service_id }, principalTable: "service_languages", principalColumns: new[] { "id", "service_id" }, onDelete: ReferentialAction.Restrict); }); migrationBuilder.CreateIndex( name: "IX_push_template_definitions_service_id_key", table: "push_template_definitions", columns: new[] { "service_id", "key" }, unique: true); migrationBuilder.CreateIndex( name: "IX_push_templates_definition_id_service_id", table: "push_templates", columns: new[] { "definition_id", "service_id" }); migrationBuilder.CreateIndex( name: "IX_push_templates_definition_id_service_language_id", table: "push_templates", columns: new[] { "definition_id", "service_language_id" }, unique: true); migrationBuilder.CreateIndex( name: "IX_push_templates_service_language_id_service_id", table: "push_templates", columns: new[] { "service_language_id", "service_id" }); // Preserve existing email identity, variables, revisions and translations in place. migrationBuilder.Sql(""" INSERT INTO templates (id, service_id, key, created, updated) SELECT id, service_id, key, created, updated FROM email_template_definitions; """); migrationBuilder.AddForeignKey( name: "FK_email_template_definitions_templates_service_id_key", table: "email_template_definitions", columns: new[] { "service_id", "key" }, principalTable: "templates", principalColumns: new[] { "service_id", "key" }, onDelete: ReferentialAction.Cascade); } /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.Sql(""" DO $$ BEGIN IF EXISTS (SELECT 1 FROM push_template_definitions) THEN RAISE EXCEPTION 'Cannot roll back channel templates while push definitions exist. Export and explicitly remove them first.'; END IF; END $$; """); migrationBuilder.DropForeignKey( name: "FK_email_template_definitions_templates_service_id_key", table: "email_template_definitions"); migrationBuilder.DropTable( name: "push_templates"); migrationBuilder.DropTable( name: "push_template_definitions"); migrationBuilder.DropTable( name: "templates"); } } }