using System; using System.Text.Json; using Microsoft.EntityFrameworkCore.Migrations; #nullable disable namespace Tiku.Infrastructure.Persistence.Migrations { /// public partial class AddPointsPersistenceFoundation : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( name: "point_activity_tasks", columns: table => new { id = table.Column(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"), task_key = table.Column(type: "citext", maxLength: 100, nullable: false), title = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), description = table.Column(type: "character varying(1000)", maxLength: 1000, nullable: true), task_type = table.Column(type: "character varying(32)", maxLength: 32, nullable: false), status = table.Column(type: "character varying(32)", maxLength: 32, nullable: false), points = table.Column(type: "integer", nullable: false), max_claims_per_user = table.Column(type: "integer", nullable: false), starts_at = table.Column(type: "timestamp with time zone", nullable: true), ends_at = table.Column(type: "timestamp with time zone", nullable: true), sort_order = table.Column(type: "integer", nullable: false), rules = table.Column(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"), metadata = table.Column(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"), tenant_id = table.Column(type: "uuid", nullable: false), created_at = table.Column(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"), updated_at = table.Column(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()") }, constraints: table => { table.PrimaryKey("pk_point_activity_tasks", x => x.id); table.UniqueConstraint("ak_point_activity_tasks_tenant_id_id", x => new { x.tenant_id, x.id }); table.CheckConstraint("ck_point_activity_tasks_max_claims", "max_claims_per_user > 0"); table.CheckConstraint("ck_point_activity_tasks_points", "points > 0"); table.ForeignKey( name: "fk_point_activity_tasks_tenants_tenant_id", column: x => x.tenant_id, principalTable: "tenants", principalColumn: "id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( name: "point_exchange_items", columns: table => new { id = table.Column(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"), region_id = table.Column(type: "uuid", nullable: true), item_key = table.Column(type: "citext", maxLength: 100, nullable: false), name = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), description = table.Column(type: "character varying(1000)", maxLength: 1000, nullable: true), item_type = table.Column(type: "character varying(32)", maxLength: 32, nullable: false), status = table.Column(type: "character varying(32)", maxLength: 32, nullable: false), points_cost = table.Column(type: "integer", nullable: false), stock = table.Column(type: "integer", nullable: true), days = table.Column(type: "integer", nullable: true), sort_order = table.Column(type: "integer", nullable: false), starts_at = table.Column(type: "timestamp with time zone", nullable: true), ends_at = table.Column(type: "timestamp with time zone", nullable: true), fulfillment_payload = table.Column(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"), metadata = table.Column(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"), tenant_id = table.Column(type: "uuid", nullable: false), created_at = table.Column(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"), updated_at = table.Column(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()") }, constraints: table => { table.PrimaryKey("pk_point_exchange_items", x => x.id); table.UniqueConstraint("ak_point_exchange_items_tenant_id_id", x => new { x.tenant_id, x.id }); table.CheckConstraint("ck_point_exchange_items_days", "days is null or days >= 0"); table.CheckConstraint("ck_point_exchange_items_points_cost", "points_cost > 0"); table.CheckConstraint("ck_point_exchange_items_stock", "stock is null or stock >= 0"); table.ForeignKey( name: "fk_point_exchange_items_regions_tenant_id_region_id", columns: x => new { x.tenant_id, x.region_id }, principalTable: "regions", principalColumns: new[] { "tenant_id", "id" }, onDelete: ReferentialAction.Restrict); table.ForeignKey( name: "fk_point_exchange_items_tenants_tenant_id", column: x => x.tenant_id, principalTable: "tenants", principalColumn: "id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( name: "point_activity_claims", columns: table => new { id = table.Column(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"), task_id = table.Column(type: "uuid", nullable: false), user_id = table.Column(type: "uuid", nullable: false), task_key = table.Column(type: "citext", maxLength: 100, nullable: false), points = table.Column(type: "integer", nullable: false), status = table.Column(type: "character varying(32)", maxLength: 32, nullable: false), source_type = table.Column(type: "character varying(100)", maxLength: 100, nullable: true), source_id = table.Column(type: "uuid", nullable: true), claimed_at = table.Column(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"), metadata = table.Column(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"), tenant_id = table.Column(type: "uuid", nullable: false), created_at = table.Column(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"), updated_at = table.Column(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()") }, constraints: table => { table.PrimaryKey("pk_point_activity_claims", x => x.id); table.UniqueConstraint("ak_point_activity_claims_tenant_id_id", x => new { x.tenant_id, x.id }); table.CheckConstraint("ck_point_activity_claims_points", "points > 0"); table.ForeignKey( name: "fk_point_activity_claims_point_activity_tasks_tenant_id_task_id", columns: x => new { x.tenant_id, x.task_id }, principalTable: "point_activity_tasks", principalColumns: new[] { "tenant_id", "id" }, onDelete: ReferentialAction.Restrict); table.ForeignKey( name: "fk_point_activity_claims_tenants_tenant_id", column: x => x.tenant_id, principalTable: "tenants", principalColumn: "id", onDelete: ReferentialAction.Cascade); table.ForeignKey( name: "fk_point_activity_claims_users_user_id", column: x => x.user_id, principalTable: "users", principalColumn: "id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( name: "point_exchange_orders", columns: table => new { id = table.Column(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"), item_id = table.Column(type: "uuid", nullable: false), user_id = table.Column(type: "uuid", nullable: false), order_no = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), item_name = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), item_type = table.Column(type: "character varying(32)", maxLength: 32, nullable: false), status = table.Column(type: "character varying(32)", maxLength: 32, nullable: false), points_cost = table.Column(type: "integer", nullable: false), ordered_at = table.Column(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"), completed_at = table.Column(type: "timestamp with time zone", nullable: true), cancelled_at = table.Column(type: "timestamp with time zone", nullable: true), fulfillment_snapshot = table.Column(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"), metadata = table.Column(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"), tenant_id = table.Column(type: "uuid", nullable: false), created_at = table.Column(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"), updated_at = table.Column(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()") }, constraints: table => { table.PrimaryKey("pk_point_exchange_orders", x => x.id); table.UniqueConstraint("ak_point_exchange_orders_tenant_id_id", x => new { x.tenant_id, x.id }); table.CheckConstraint("ck_point_exchange_orders_points_cost", "points_cost > 0"); table.ForeignKey( name: "fk_point_exchange_orders_point_exchange_items_tenant_id_item_id", columns: x => new { x.tenant_id, x.item_id }, principalTable: "point_exchange_items", principalColumns: new[] { "tenant_id", "id" }, onDelete: ReferentialAction.Restrict); table.ForeignKey( name: "fk_point_exchange_orders_tenants_tenant_id", column: x => x.tenant_id, principalTable: "tenants", principalColumn: "id", onDelete: ReferentialAction.Cascade); table.ForeignKey( name: "fk_point_exchange_orders_users_user_id", column: x => x.user_id, principalTable: "users", principalColumn: "id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateIndex( name: "ix_point_activity_claims_tenant_id_task_id", table: "point_activity_claims", columns: new[] { "tenant_id", "task_id" }); migrationBuilder.CreateIndex( name: "ix_point_activity_claims_tenant_id_user_id_created_at", table: "point_activity_claims", columns: new[] { "tenant_id", "user_id", "created_at" }); migrationBuilder.CreateIndex( name: "ix_point_activity_claims_tenant_id_user_id_task_id_source_type~", table: "point_activity_claims", columns: new[] { "tenant_id", "user_id", "task_id", "source_type", "source_id" }, unique: true, filter: "source_type is not null and source_id is not null"); migrationBuilder.CreateIndex( name: "ix_point_activity_claims_user_id", table: "point_activity_claims", column: "user_id"); migrationBuilder.CreateIndex( name: "ix_point_activity_tasks_tenant_id_status_sort_order", table: "point_activity_tasks", columns: new[] { "tenant_id", "status", "sort_order" }); migrationBuilder.CreateIndex( name: "ix_point_activity_tasks_tenant_id_task_key", table: "point_activity_tasks", columns: new[] { "tenant_id", "task_key" }, unique: true); migrationBuilder.CreateIndex( name: "ix_point_exchange_items_tenant_id_item_key", table: "point_exchange_items", columns: new[] { "tenant_id", "item_key" }, unique: true); migrationBuilder.CreateIndex( name: "ix_point_exchange_items_tenant_id_region_id_status_sort_order", table: "point_exchange_items", columns: new[] { "tenant_id", "region_id", "status", "sort_order" }); migrationBuilder.CreateIndex( name: "ix_point_exchange_orders_tenant_id_item_id", table: "point_exchange_orders", columns: new[] { "tenant_id", "item_id" }); migrationBuilder.CreateIndex( name: "ix_point_exchange_orders_tenant_id_order_no", table: "point_exchange_orders", columns: new[] { "tenant_id", "order_no" }, unique: true); migrationBuilder.CreateIndex( name: "ix_point_exchange_orders_tenant_id_user_id_created_at", table: "point_exchange_orders", columns: new[] { "tenant_id", "user_id", "created_at" }); migrationBuilder.CreateIndex( name: "ix_point_exchange_orders_user_id", table: "point_exchange_orders", column: "user_id"); } /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable( name: "point_activity_claims"); migrationBuilder.DropTable( name: "point_exchange_orders"); migrationBuilder.DropTable( name: "point_activity_tasks"); migrationBuilder.DropTable( name: "point_exchange_items"); } } }