Files
tiku-backend.net/Tiku.Infrastructure/Persistence/Migrations/20260725213505_AddCommercePersistence.cs
2026-07-26 05:36:03 +08:00

854 lines
50 KiB
C#

using System;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Tiku.Infrastructure.Persistence.Migrations
{
/// <inheritdoc />
public partial class AddCommercePersistence : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "code_batches",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
region_id = table.Column<Guid>(type: "uuid", nullable: true),
created_by = table.Column<Guid>(type: "uuid", nullable: true),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
sale_type = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
channel = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
campaign_name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
default_unit_price_cents = table.Column<int>(type: "integer", nullable: false),
cost_price_cents = table.Column<int>(type: "integer", nullable: false),
total_count = table.Column<int>(type: "integer", nullable: false),
days = table.Column<int>(type: "integer", nullable: true),
legacy_region_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
issued_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
remark = table.Column<string>(type: "text", nullable: true),
commission_rate = table.Column<decimal>(type: "numeric(6,4)", precision: 6, scale: 4, nullable: true),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_code_batches", x => x.id);
table.UniqueConstraint("ak_code_batches_tenant_id_id", x => new { x.tenant_id, x.id });
table.CheckConstraint("ck_code_batches_amounts", "default_unit_price_cents >= 0 and cost_price_cents >= 0");
table.CheckConstraint("ck_code_batches_counts", "total_count >= 0");
table.ForeignKey(
name: "fk_code_batches_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "entitlements",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
user_id = table.Column<Guid>(type: "uuid", nullable: false),
entitlement_type = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
scope_type = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
scope_id = table.Column<Guid>(type: "uuid", nullable: true),
source_type = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
source_id = table.Column<Guid>(type: "uuid", nullable: true),
legacy_source_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
starts_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
expires_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_entitlements", x => x.id);
table.UniqueConstraint("ak_entitlements_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_entitlements_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_entitlements_users_user_id",
column: x => x.user_id,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "products",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
region_id = table.Column<Guid>(type: "uuid", nullable: true),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
title = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: false),
legacy_price_text = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
link = table.Column<string>(type: "character varying(2048)", maxLength: 2048, nullable: true),
type = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
tags = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'[]'::jsonb"),
sort_order = table.Column<int>(type: "integer", nullable: false),
cover = table.Column<string>(type: "character varying(2048)", maxLength: 2048, nullable: true),
preview_iframe = table.Column<string>(type: "character varying(2048)", maxLength: 2048, nullable: true),
detail_images = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'[]'::jsonb"),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_products", x => x.id);
table.UniqueConstraint("ak_products_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_products_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_products_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "svip_plans",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
region_id = table.Column<Guid>(type: "uuid", nullable: true),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
price_cents = table.Column<int>(type: "integer", nullable: false),
original_price_cents = table.Column<int>(type: "integer", nullable: true),
days = table.Column<int>(type: "integer", nullable: false),
description = table.Column<string>(type: "text", nullable: true),
per_day_label = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
badge = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
recommended = table.Column<bool>(type: "boolean", nullable: false),
coupon_only = table.Column<bool>(type: "boolean", nullable: false),
vp_product_id = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
vp_enabled = table.Column<bool>(type: "boolean", nullable: false),
sort_order = table.Column<int>(type: "integer", nullable: false),
is_active = table.Column<bool>(type: "boolean", nullable: false),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_svip_plans", x => x.id);
table.UniqueConstraint("ak_svip_plans_tenant_id_id", x => new { x.tenant_id, x.id });
table.CheckConstraint("ck_svip_plans_days", "days >= 0");
table.CheckConstraint("ck_svip_plans_price", "price_cents >= 0 and (original_price_cents is null or original_price_cents >= 0)");
table.ForeignKey(
name: "fk_svip_plans_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_svip_plans_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "tenant_payment_accounts",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
provider = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
mode = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
display_name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
config_public = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_tenant_payment_accounts", x => x.id);
table.UniqueConstraint("ak_tenant_payment_accounts_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_tenant_payment_accounts_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "tenant_subscriptions",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
plan_code = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
starts_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
expires_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
billing_cycle = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
amount_cents = table.Column<int>(type: "integer", nullable: false),
metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_tenant_subscriptions", x => x.id);
table.UniqueConstraint("ak_tenant_subscriptions_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_tenant_subscriptions_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "tenant_usage_records",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
metric_key = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
metric_value = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
period_start = table.Column<DateOnly>(type: "date", nullable: false),
period_end = table.Column<DateOnly>(type: "date", nullable: false),
metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_tenant_usage_records", x => x.id);
table.UniqueConstraint("ak_tenant_usage_records_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_tenant_usage_records_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "coupons",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
plan_id = table.Column<Guid>(type: "uuid", nullable: true),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
code = table.Column<string>(type: "citext", maxLength: 100, nullable: false),
discount_type = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: true),
discount_value = table.Column<decimal>(type: "numeric(10,2)", precision: 10, scale: 2, nullable: true),
valid_from = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
valid_to = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
max_uses = table.Column<int>(type: "integer", nullable: true),
used_count = table.Column<int>(type: "integer", nullable: false),
source = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
remark = table.Column<string>(type: "text", nullable: true),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_coupons", x => x.id);
table.UniqueConstraint("ak_coupons_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_coupons_svip_plans_tenant_id_plan_id",
columns: x => new { x.tenant_id, x.plan_id },
principalTable: "svip_plans",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_coupons_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "orders",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
user_id = table.Column<Guid>(type: "uuid", nullable: true),
plan_id = table.Column<Guid>(type: "uuid", nullable: true),
region_id = table.Column<Guid>(type: "uuid", nullable: true),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
legacy_user_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
legacy_plan_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
legacy_region_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
order_no = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
product_type = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
product_name = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: true),
amount_cents = table.Column<int>(type: "integer", nullable: false),
pay_method = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
pay_provider = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
trade_no = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
days = table.Column<int>(type: "integer", nullable: true),
paid_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
raw_payload = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_orders", x => x.id);
table.UniqueConstraint("ak_orders_tenant_id_id", x => new { x.tenant_id, x.id });
table.CheckConstraint("ck_orders_amount", "amount_cents >= 0");
table.ForeignKey(
name: "fk_orders_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_orders_svip_plans_tenant_id_plan_id",
columns: x => new { x.tenant_id, x.plan_id },
principalTable: "svip_plans",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_orders_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_orders_users_user_id",
column: x => x.user_id,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
});
migrationBuilder.CreateTable(
name: "coupon_redemptions",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
coupon_id = table.Column<Guid>(type: "uuid", nullable: true),
user_id = table.Column<Guid>(type: "uuid", nullable: true),
plan_id = table.Column<Guid>(type: "uuid", nullable: true),
order_id = table.Column<Guid>(type: "uuid", nullable: true),
region_id = table.Column<Guid>(type: "uuid", nullable: true),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
coupon_code = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
discount_applied_cents = table.Column<int>(type: "integer", nullable: true),
source = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
remark = table.Column<string>(type: "text", nullable: true),
claimed_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
used_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_coupon_redemptions", x => x.id);
table.UniqueConstraint("ak_coupon_redemptions_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_coupon_redemptions_coupons_tenant_id_coupon_id",
columns: x => new { x.tenant_id, x.coupon_id },
principalTable: "coupons",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_coupon_redemptions_orders_tenant_id_order_id",
columns: x => new { x.tenant_id, x.order_id },
principalTable: "orders",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_coupon_redemptions_regions_tenant_id_region_id",
columns: x => new { x.tenant_id, x.region_id },
principalTable: "regions",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_coupon_redemptions_svip_plans_tenant_id_plan_id",
columns: x => new { x.tenant_id, x.plan_id },
principalTable: "svip_plans",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_coupon_redemptions_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_coupon_redemptions_users_user_id",
column: x => x.user_id,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
});
migrationBuilder.CreateTable(
name: "order_items",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
order_id = table.Column<Guid>(type: "uuid", nullable: false),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
item_type = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
item_id = table.Column<Guid>(type: "uuid", nullable: true),
name = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: false),
quantity = table.Column<int>(type: "integer", nullable: false),
unit_amount_cents = table.Column<int>(type: "integer", nullable: false),
total_amount_cents = table.Column<int>(type: "integer", nullable: false),
metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_order_items", x => x.id);
table.UniqueConstraint("ak_order_items_tenant_id_id", x => new { x.tenant_id, x.id });
table.CheckConstraint("ck_order_items_amounts", "unit_amount_cents >= 0 and total_amount_cents >= 0");
table.CheckConstraint("ck_order_items_quantity", "quantity > 0");
table.ForeignKey(
name: "fk_order_items_orders_tenant_id_order_id",
columns: x => new { x.tenant_id, x.order_id },
principalTable: "orders",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_order_items_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "payments",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
order_id = table.Column<Guid>(type: "uuid", nullable: false),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
legacy_order_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
provider = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
method = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
amount_cents = table.Column<int>(type: "integer", nullable: false),
provider_trade_no = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
paid_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
raw_payload = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_payments", x => x.id);
table.UniqueConstraint("ak_payments_tenant_id_id", x => new { x.tenant_id, x.id });
table.CheckConstraint("ck_payments_amount", "amount_cents >= 0");
table.ForeignKey(
name: "fk_payments_orders_tenant_id_order_id",
columns: x => new { x.tenant_id, x.order_id },
principalTable: "orders",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_payments_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "activation_codes",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
used_by = table.Column<Guid>(type: "uuid", nullable: true),
agent_user_id = table.Column<Guid>(type: "uuid", nullable: true),
batch_id = table.Column<Guid>(type: "uuid", nullable: true),
used_region_id = table.Column<Guid>(type: "uuid", nullable: true),
coupon_redemption_id = table.Column<Guid>(type: "uuid", nullable: true),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
code = table.Column<string>(type: "citext", maxLength: 100, nullable: false),
days = table.Column<int>(type: "integer", nullable: false),
is_used = table.Column<bool>(type: "boolean", nullable: false),
used_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
sale_type = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
unit_price_cents = table.Column<int>(type: "integer", nullable: true),
sold_to = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
coupon_code = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
remark = table.Column<string>(type: "text", nullable: true),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_activation_codes", x => x.id);
table.UniqueConstraint("ak_activation_codes_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_activation_codes_code_batches_tenant_id_batch_id",
columns: x => new { x.tenant_id, x.batch_id },
principalTable: "code_batches",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_activation_codes_coupon_redemptions_tenant_id_coupon_redemp~",
columns: x => new { x.tenant_id, x.coupon_redemption_id },
principalTable: "coupon_redemptions",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_activation_codes_regions_tenant_id_used_region_id",
columns: x => new { x.tenant_id, x.used_region_id },
principalTable: "regions",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_activation_codes_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_activation_codes_users_agent_user_id",
column: x => x.agent_user_id,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_activation_codes_users_used_by",
column: x => x.used_by,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
});
migrationBuilder.CreateTable(
name: "payment_events",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
payment_id = table.Column<Guid>(type: "uuid", nullable: true),
provider = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
event_type = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
event_id = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
signature_valid = table.Column<bool>(type: "boolean", nullable: true),
payload = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
processed_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
error = table.Column<string>(type: "text", nullable: true),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_payment_events", x => x.id);
table.UniqueConstraint("ak_payment_events_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_payment_events_payments_tenant_id_payment_id",
columns: x => new { x.tenant_id, x.payment_id },
principalTable: "payments",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_payment_events_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "ix_activation_codes_agent_user_id",
table: "activation_codes",
column: "agent_user_id");
migrationBuilder.CreateIndex(
name: "ix_activation_codes_tenant_id_batch_id",
table: "activation_codes",
columns: new[] { "tenant_id", "batch_id" });
migrationBuilder.CreateIndex(
name: "ix_activation_codes_tenant_id_code",
table: "activation_codes",
columns: new[] { "tenant_id", "code" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_activation_codes_tenant_id_coupon_redemption_id",
table: "activation_codes",
columns: new[] { "tenant_id", "coupon_redemption_id" });
migrationBuilder.CreateIndex(
name: "ix_activation_codes_tenant_id_legacy_id",
table: "activation_codes",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_activation_codes_tenant_id_used_region_id",
table: "activation_codes",
columns: new[] { "tenant_id", "used_region_id" });
migrationBuilder.CreateIndex(
name: "ix_activation_codes_used_by",
table: "activation_codes",
column: "used_by");
migrationBuilder.CreateIndex(
name: "ix_code_batches_tenant_id_legacy_id",
table: "code_batches",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_coupon_redemptions_tenant_id_coupon_id",
table: "coupon_redemptions",
columns: new[] { "tenant_id", "coupon_id" });
migrationBuilder.CreateIndex(
name: "ix_coupon_redemptions_tenant_id_legacy_id",
table: "coupon_redemptions",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_coupon_redemptions_tenant_id_order_id_user_id",
table: "coupon_redemptions",
columns: new[] { "tenant_id", "order_id", "user_id" },
filter: "order_id is not null");
migrationBuilder.CreateIndex(
name: "ix_coupon_redemptions_tenant_id_plan_id",
table: "coupon_redemptions",
columns: new[] { "tenant_id", "plan_id" });
migrationBuilder.CreateIndex(
name: "ix_coupon_redemptions_tenant_id_region_id",
table: "coupon_redemptions",
columns: new[] { "tenant_id", "region_id" });
migrationBuilder.CreateIndex(
name: "ix_coupon_redemptions_tenant_id_user_id_coupon_id",
table: "coupon_redemptions",
columns: new[] { "tenant_id", "user_id", "coupon_id" },
unique: true,
filter: "coupon_id is not null");
migrationBuilder.CreateIndex(
name: "ix_coupon_redemptions_user_id",
table: "coupon_redemptions",
column: "user_id");
migrationBuilder.CreateIndex(
name: "ix_coupons_tenant_id_code",
table: "coupons",
columns: new[] { "tenant_id", "code" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_coupons_tenant_id_legacy_id",
table: "coupons",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_coupons_tenant_id_plan_id",
table: "coupons",
columns: new[] { "tenant_id", "plan_id" });
migrationBuilder.CreateIndex(
name: "ix_entitlements_tenant_id_user_id_status_expires_at",
table: "entitlements",
columns: new[] { "tenant_id", "user_id", "status", "expires_at" });
migrationBuilder.CreateIndex(
name: "ix_entitlements_user_id",
table: "entitlements",
column: "user_id");
migrationBuilder.CreateIndex(
name: "ix_order_items_tenant_id_legacy_id",
table: "order_items",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_order_items_tenant_id_order_id",
table: "order_items",
columns: new[] { "tenant_id", "order_id" });
migrationBuilder.CreateIndex(
name: "ix_orders_tenant_id_legacy_id",
table: "orders",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_orders_tenant_id_order_no",
table: "orders",
columns: new[] { "tenant_id", "order_no" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_orders_tenant_id_plan_id",
table: "orders",
columns: new[] { "tenant_id", "plan_id" });
migrationBuilder.CreateIndex(
name: "ix_orders_tenant_id_region_id",
table: "orders",
columns: new[] { "tenant_id", "region_id" });
migrationBuilder.CreateIndex(
name: "ix_orders_tenant_id_user_id_created_at",
table: "orders",
columns: new[] { "tenant_id", "user_id", "created_at" });
migrationBuilder.CreateIndex(
name: "ix_orders_user_id",
table: "orders",
column: "user_id");
migrationBuilder.CreateIndex(
name: "ix_payment_events_provider_event_id",
table: "payment_events",
columns: new[] { "provider", "event_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_payment_events_tenant_id_payment_id",
table: "payment_events",
columns: new[] { "tenant_id", "payment_id" });
migrationBuilder.CreateIndex(
name: "ix_payments_provider_provider_trade_no",
table: "payments",
columns: new[] { "provider", "provider_trade_no" },
unique: true,
filter: "provider_trade_no is not null");
migrationBuilder.CreateIndex(
name: "ix_payments_tenant_id_legacy_id",
table: "payments",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_payments_tenant_id_order_id_updated_at",
table: "payments",
columns: new[] { "tenant_id", "order_id", "updated_at" });
migrationBuilder.CreateIndex(
name: "ix_products_tenant_id_legacy_id",
table: "products",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_products_tenant_id_region_id_type_sort_order",
table: "products",
columns: new[] { "tenant_id", "region_id", "type", "sort_order" });
migrationBuilder.CreateIndex(
name: "ix_svip_plans_tenant_id_legacy_id",
table: "svip_plans",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_svip_plans_tenant_id_region_id_is_active_sort_order",
table: "svip_plans",
columns: new[] { "tenant_id", "region_id", "is_active", "sort_order" });
migrationBuilder.CreateIndex(
name: "ix_tenant_payment_accounts_tenant_id_provider",
table: "tenant_payment_accounts",
columns: new[] { "tenant_id", "provider" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_tenant_subscriptions_tenant_id_status_expires_at",
table: "tenant_subscriptions",
columns: new[] { "tenant_id", "status", "expires_at" });
migrationBuilder.CreateIndex(
name: "ix_tenant_usage_records_tenant_id_metric_key_period_start_peri~",
table: "tenant_usage_records",
columns: new[] { "tenant_id", "metric_key", "period_start", "period_end" });
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "activation_codes");
migrationBuilder.DropTable(
name: "entitlements");
migrationBuilder.DropTable(
name: "order_items");
migrationBuilder.DropTable(
name: "payment_events");
migrationBuilder.DropTable(
name: "products");
migrationBuilder.DropTable(
name: "tenant_payment_accounts");
migrationBuilder.DropTable(
name: "tenant_subscriptions");
migrationBuilder.DropTable(
name: "tenant_usage_records");
migrationBuilder.DropTable(
name: "code_batches");
migrationBuilder.DropTable(
name: "coupon_redemptions");
migrationBuilder.DropTable(
name: "payments");
migrationBuilder.DropTable(
name: "coupons");
migrationBuilder.DropTable(
name: "orders");
migrationBuilder.DropTable(
name: "svip_plans");
}
}
}