Skip to content

Commit 00001c6

Browse files
支持MySQL并移除不必要的lock (#43)
* Support MySQL * 添加依赖
1 parent 1ebd20b commit 00001c6

File tree

5 files changed

+675
-606
lines changed

5 files changed

+675
-606
lines changed

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
[project]
22
name = "nonebot-plugin-value"
3-
version = "0.0.8"
3+
version = "0.0.9"
44
description = "Value API for NoneBot2"
55
readme = "README.md"
66
requires-python = ">=3.10"
77
dependencies = [
8+
"aiomysql>=0.2.0",
89
"aiosqlite>=0.21.0",
910
"nonebot-plugin-orm>=0.8.1",
1011
"nonebot2>=2.4.2",
12+
"psycopg2-binary>=2.9.10",
1113
"pydantic>=2.11.7",
1214
"sqlalchemy>=2.0.41",
1315
]

src/nonebot_plugin_value/models/balance.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class UserAccount(Model):
2222
__tablename__ = "user_accounts"
2323

2424
# 每种货币账户的唯一ID(id currency_id-UUID.hex)
25-
uni_id: MappedColumn[str] = mapped_column(String(64), primary_key=True)
25+
uni_id: MappedColumn[str] = mapped_column(String(255), primary_key=True)
2626

2727
# 用户ID
2828
id: MappedColumn[str] = mapped_column(String(64))
@@ -67,16 +67,16 @@ class Transaction(Model):
6767
__tablename__ = "transactions"
6868

6969
# UUID作为主键
70-
id: MappedColumn[str] = mapped_column(String(64), primary_key=True)
70+
id: MappedColumn[str] = mapped_column(String(255), primary_key=True)
7171

7272
# 账户外键
7373
account_id: MappedColumn[str] = mapped_column(
74-
String, ForeignKey("user_accounts.id", ondelete="RESTRICT"), nullable=False
74+
String(255), ForeignKey("user_accounts.id", ondelete="RESTRICT"), nullable=False
7575
)
7676

7777
# 货币外键
7878
currency_id: MappedColumn[str] = mapped_column(
79-
String(32), ForeignKey("currency_meta.id", ondelete="RESTRICT"), nullable=False
79+
String(255), ForeignKey("currency_meta.id", ondelete="RESTRICT"), nullable=False
8080
)
8181

8282
# 交易金额

src/nonebot_plugin_value/models/currency.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class CurrencyMeta(Model):
1212
__tablename__ = "currency_meta"
1313

1414
# 货币ID作为主键
15-
id: Mapped[str] = mapped_column(String(64), primary_key=True, default=uuid4)
15+
id: Mapped[str] = mapped_column(String(255), primary_key=True, default=uuid4)
1616

1717
# 货币显示名称
1818
display_name: Mapped[str] = mapped_column(String(64), nullable=False)

src/nonebot_plugin_value/repository.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ async def createcurrency(self, currency_data: CurrencyData) -> CurrencyMeta:
2727
stmt = insert(CurrencyMeta).values(**dict(currency_data))
2828
await session.execute(stmt)
2929
await session.commit()
30-
stmt = (
31-
select(CurrencyMeta)
32-
.where(CurrencyMeta.id == currency_data.id)
33-
.with_for_update()
34-
)
30+
stmt = select(CurrencyMeta).where(CurrencyMeta.id == currency_data.id)
3531
result = await session.execute(stmt)
3632
currency_meta = result.scalar_one()
3733
session.add(currency_meta)
@@ -112,11 +108,7 @@ async def get_or_create_account(
112108
async with self.session as session:
113109
"""获取或创建用户账户"""
114110
# 获取货币配置
115-
stmt = (
116-
select(CurrencyMeta)
117-
.where(CurrencyMeta.id == currency_id)
118-
.with_for_update()
119-
)
111+
stmt = select(CurrencyMeta).where(CurrencyMeta.id == currency_id)
120112
result = await session.execute(stmt)
121113
currency = result.scalar_one_or_none()
122114
if currency is None:
@@ -149,13 +141,9 @@ async def get_or_create_account(
149141
session.add(account)
150142
await session.commit()
151143

152-
stmt = (
153-
select(UserAccount)
154-
.where(
155-
UserAccount.id == user_id,
156-
UserAccount.currency_id == currency_id,
157-
)
158-
.with_for_update()
144+
stmt = select(UserAccount).where(
145+
UserAccount.id == user_id,
146+
UserAccount.currency_id == currency_id,
159147
)
160148
result = await session.execute(stmt)
161149
account = result.scalar_one()
@@ -295,7 +283,6 @@ async def get_transaction_history(self, account_id: str, limit: int = 100):
295283
.where(Transaction.account_id == account_id)
296284
.order_by(Transaction.timestamp.desc())
297285
.limit(limit)
298-
.with_for_update()
299286
)
300287
data = result.scalars().all()
301288
self.session.add_all(data)

0 commit comments

Comments
 (0)