feat(db): 新增 asset_prices_history 表用于存储手动导入的历史标的价格

This commit is contained in:
kennethcheng 2026-04-30 10:05:42 +08:00
parent 838bb0ef95
commit 108023ae67
5 changed files with 487 additions and 0 deletions

View File

@ -12,6 +12,7 @@
- `assets` 表完成多次业务演进:新增 `latestPrice` (支持现价追踪)、`exchange` (显式交易所绑定) 以及 `name` (中文名称解析) 字段。
- `exchange_rates` (汇率表) 已建立,支持联合主键与跨币种交叉汇率架构。
- **引入 `portfolio_snapshots` 表**:用于每日记录投资组合快照,字段包括 `date` (唯一日期)、`total_value_cny` (当日总市值)、`total_cost_cny` (当日总投入本金),为历史净值走势图奠定底层数据结构。
- **新增 `asset_prices_history` 表**:用于存储手动导入的每日标的价格,字段包括 `assetId` (关联资产)、`price` (当日收盘价/净值numeric(36,18))、`date` (YYYY-MM-DD 格式)、`createdAt`,并对 `(assetId, date)` 建立联合唯一索引,为手动导入历史净值提供底层 Upsert 支持。
## 核心业务与服务端逻辑 (Server Actions)
- 完成高精度交易流水与资产的 Server Actions 开发,成功实现字符串级别的高精度防腐层拦截(基于 Zod & Big.js

View File

@ -0,0 +1,10 @@
CREATE TABLE "asset_prices_history" (
"id" uuid PRIMARY KEY NOT NULL,
"asset_id" uuid NOT NULL,
"price" numeric(36, 18) NOT NULL,
"date" date NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "asset_prices_history" ADD CONSTRAINT "asset_prices_history_asset_id_assets_id_fk" FOREIGN KEY ("asset_id") REFERENCES "public"."assets"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "asset_price_date_idx" ON "asset_prices_history" USING btree ("asset_id","date");

View File

@ -0,0 +1,455 @@
{
"id": "d95996ac-d802-4b08-83db-20ff0e9e64f7",
"prevId": "26b392bf-03db-4f71-86d5-395d5c07fae5",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.asset_prices_history": {
"name": "asset_prices_history",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true
},
"asset_id": {
"name": "asset_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"price": {
"name": "price",
"type": "numeric(36, 18)",
"primaryKey": false,
"notNull": true
},
"date": {
"name": "date",
"type": "date",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {
"asset_price_date_idx": {
"name": "asset_price_date_idx",
"columns": [
{
"expression": "asset_id",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "date",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": true,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {
"asset_prices_history_asset_id_assets_id_fk": {
"name": "asset_prices_history_asset_id_assets_id_fk",
"tableFrom": "asset_prices_history",
"tableTo": "assets",
"columnsFrom": [
"asset_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.assets": {
"name": "assets",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true
},
"symbol": {
"name": "symbol",
"type": "varchar(20)",
"primaryKey": false,
"notNull": true
},
"name": {
"name": "name",
"type": "varchar(100)",
"primaryKey": false,
"notNull": false
},
"type": {
"name": "type",
"type": "asset_type_enum",
"typeSchema": "public",
"primaryKey": false,
"notNull": true
},
"exchange": {
"name": "exchange",
"type": "varchar(10)",
"primaryKey": false,
"notNull": false,
"default": "'US'"
},
"base_currency": {
"name": "base_currency",
"type": "varchar(10)",
"primaryKey": false,
"notNull": true
},
"latest_price": {
"name": "latest_price",
"type": "numeric(36, 18)",
"primaryKey": false,
"notNull": true,
"default": "'0'"
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"assets_symbol_unique": {
"name": "assets_symbol_unique",
"nullsNotDistinct": false,
"columns": [
"symbol"
]
}
},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.exchange_rates": {
"name": "exchange_rates",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true
},
"from_currency": {
"name": "from_currency",
"type": "varchar(10)",
"primaryKey": false,
"notNull": true
},
"to_currency": {
"name": "to_currency",
"type": "varchar(10)",
"primaryKey": false,
"notNull": true
},
"rate": {
"name": "rate",
"type": "numeric(20, 8)",
"primaryKey": false,
"notNull": true
},
"updated_at": {
"name": "updated_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false,
"default": "now()"
}
},
"indexes": {
"currency_pair_idx": {
"name": "currency_pair_idx",
"columns": [
{
"expression": "from_currency",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "to_currency",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": true,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.portfolio_snapshots": {
"name": "portfolio_snapshots",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true
},
"date": {
"name": "date",
"type": "date",
"primaryKey": false,
"notNull": true
},
"total_value_cny": {
"name": "total_value_cny",
"type": "numeric(36, 18)",
"primaryKey": false,
"notNull": true
},
"total_cost_cny": {
"name": "total_cost_cny",
"type": "numeric(36, 18)",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"portfolio_snapshots_date_unique": {
"name": "portfolio_snapshots_date_unique",
"nullsNotDistinct": false,
"columns": [
"date"
]
}
},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.transactions": {
"name": "transactions",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true
},
"asset_id": {
"name": "asset_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"tx_type": {
"name": "tx_type",
"type": "transaction_type_enum",
"typeSchema": "public",
"primaryKey": false,
"notNull": true
},
"quantity": {
"name": "quantity",
"type": "numeric(36, 18)",
"primaryKey": false,
"notNull": true
},
"price": {
"name": "price",
"type": "numeric(36, 18)",
"primaryKey": false,
"notNull": true
},
"fee": {
"name": "fee",
"type": "numeric(36, 18)",
"primaryKey": false,
"notNull": true,
"default": "'0'"
},
"tx_currency": {
"name": "tx_currency",
"type": "varchar(10)",
"primaryKey": false,
"notNull": true
},
"exchange_rate": {
"name": "exchange_rate",
"type": "numeric(20, 8)",
"primaryKey": false,
"notNull": true,
"default": "'1'"
},
"executed_at": {
"name": "executed_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {
"transactions_asset_id_assets_id_fk": {
"name": "transactions_asset_id_assets_id_fk",
"tableFrom": "transactions",
"tableTo": "assets",
"columnsFrom": [
"asset_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.users": {
"name": "users",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true
},
"username": {
"name": "username",
"type": "varchar(50)",
"primaryKey": false,
"notNull": true
},
"password_hash": {
"name": "password_hash",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"users_username_unique": {
"name": "users_username_unique",
"nullsNotDistinct": false,
"columns": [
"username"
]
}
},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
}
},
"enums": {
"public.asset_type_enum": {
"name": "asset_type_enum",
"schema": "public",
"values": [
"STOCK",
"CRYPTO",
"CASH"
]
},
"public.transaction_type_enum": {
"name": "transaction_type_enum",
"schema": "public",
"values": [
"BUY",
"SELL",
"DIVIDEND",
"AIRDROP",
"FEE"
]
}
},
"schemas": {},
"sequences": {},
"roles": {},
"policies": {},
"views": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}

View File

@ -29,6 +29,13 @@
"when": 1777433725731,
"tag": "0003_watery_xorn",
"breakpoints": true
},
{
"idx": 4,
"version": "7",
"when": 1777514678798,
"tag": "0004_glamorous_stranger",
"breakpoints": true
}
]
}

View File

@ -78,3 +78,17 @@ export const portfolioSnapshots = pgTable("portfolio_snapshots", {
.defaultNow()
.notNull(),
});
export const assetPricesHistory = pgTable("asset_prices_history", {
id: uuid("id").primaryKey().$defaultFn(() => crypto.randomUUID()),
assetId: uuid("asset_id")
.notNull()
.references(() => assets.id),
price: numeric("price", { precision: 36, scale: 18 }).notNull(),
date: date("date", { mode: "string" }).notNull(),
createdAt: timestamp("created_at", { withTimezone: true, mode: "date" })
.defaultNow()
.notNull(),
}, (table) => [
uniqueIndex("asset_price_date_idx").on(table.assetId, table.date),
]);