he0119 / nonebot-plugin-wordcloud

适用于 NoneBot2 的词云插件
https://pypi.org/project/nonebot-plugin-wordcloud/
MIT License
87 stars 7 forks source link

Bug: v0.4.5版本使用postgresql+asyncpg报错 #120

Closed LambdaYH closed 1 year ago

LambdaYH commented 1 year ago

描述问题

发送今日词云,日志里报错 上一个版本是可以正常用的

如何复现

群里发送今日词云

期望的结果

没报错

环境信息

截图或日志

03-04 18:32:37 [ERROR] nonebot | Running Matcher(type='message', module=nonebot_plugin_wordcloud) failed.
Traceback (most recent call last):
  File "asyncpg/protocol/prepared_stmt.pyx", line 168, in asyncpg.protocol.protocol.PreparedStatementState._encode_bind_msg
    codec.encode(self.settings, writer, arg)
  File "asyncpg/protocol/codecs/base.pyx", line 206, in asyncpg.protocol.protocol.Codec.encode
    return self.encoder(self, settings, buf, obj)
  File "asyncpg/protocol/codecs/base.pyx", line 111, in asyncpg.protocol.protocol.Codec.encode_scalar
    self.c_encoder(settings, buf, obj)
  File "asyncpg/pgproto/./codecs/datetime.pyx", line 152, in asyncpg.pgproto.pgproto.timestamp_encode
TypeError: can't subtract offset-naive and offset-aware datetimes

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 482, in _prepare_and_execute
    self._rows = await prepared_stmt.fetch(*parameters)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/asyncpg/prepared_stmt.py", line 176, in fetch
    data = await self.__bind_execute(args, 0, timeout)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/asyncpg/prepared_stmt.py", line 241, in __bind_execute
    data, status, _ = await self.__do_execute(
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/asyncpg/prepared_stmt.py", line 230, in __do_execute
    return await executor(protocol)
  File "asyncpg/protocol/protocol.pyx", line 183, in bind_execute
    args_buf = state._encode_bind_msg(args)
  File "asyncpg/protocol/prepared_stmt.pyx", line 197, in asyncpg.protocol.protocol.PreparedStatementState._encode_bind_msg
    raise exceptions.DataError(
asyncpg.exceptions.DataError: invalid input for query argument $2: datetime.datetime(2023, 3, 3, 16, 0, tzi... (can't subtract offset-naive and offset-aware datetimes)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1964, in _exec_single_context
    self.dialect.do_execute(
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 747, in do_execute
    cursor.execute(statement, parameters)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 515, in execute
    self._adapt_connection.await_(
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 102, in await_only
    return current.driver.switch(awaitable)  # type: ignore[no-any-return]
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 160, in greenlet_spawn
    value = await result
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 494, in _prepare_and_execute
    self._handle_exception(error)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 444, in _handle_exception
    self._adapt_connection._handle_exception(error)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 717, in _handle_exception
    raise translated_error from error
sqlalchemy.dialects.postgresql.asyncpg.AsyncAdapt_asyncpg_dbapi.Error: <class 'asyncpg.exceptions.DataError'>: invalid input for query argument $2: datetime.datetime(2023, 3, 3, 16, 0, tzi... (can't subtract offset-naive and offset-aware datetimes)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/srv/dev/bot/bot.py", line 26, in <module>
    nonebot.run()
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/nonebot/__init__.py", line 273, in run
    get_driver().run(*args, **kwargs)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/nonebot/drivers/fastapi.py", line 187, in run
    uvicorn.run(
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/uvicorn/main.py", line 569, in run
    server.run()
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/uvicorn/server.py", line 60, in run
    return asyncio.run(self.serve(sockets=sockets))
  File "/usr/local/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/nonebot/message.py", line 141, in _check_matcher
    await _run_matcher(Matcher, bot, event, state, stack, dependency_cache)
> File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/nonebot/message.py", line 187, in _run_matcher
    await matcher.run(bot, event, state, stack, dependency_cache)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/nonebot/internal/matcher/matcher.py", line 732, in run
    await self.simple_run(bot, event, state, stack, dependency_cache)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/nonebot/internal/matcher/matcher.py", line 707, in simple_run
    await handler(
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/nonebot/dependencies/__init__.py", line 108, in __call__
    return await cast(Callable[..., Awaitable[R]], self.call)(**values)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/nonebot_plugin_wordcloud/__init__.py", line 237, in handle_get_messages_group_message
    state["messages"] = await get_messages_plain_text(
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/nonebot_plugin_chatrecorder/record.py", line 157, in get_messages_plain_text
    records = await get_message_records(**kwargs)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/nonebot_plugin_chatrecorder/record.py", line 114, in get_message_records
    records = (await session.scalars(statement)).all()
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/ext/asyncio/session.py", line 428, in scalars
    result = await self.execute(
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/ext/asyncio/session.py", line 313, in execute
    result = await greenlet_spawn(
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 165, in greenlet_spawn
    result = context.throw(*sys.exc_info())
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 2229, in execute
    return self._execute_internal(
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 2124, in _execute_internal
    result: Result[Any] = compile_state_cls.orm_execute_statement(
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/orm/context.py", line 253, in orm_execute_statement
    result = conn.execute(
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1414, in execute
    return meth(
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/sql/elements.py", line 485, in _execute_on_connection
    return connection._execute_clauseelement(
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1638, in _execute_clauseelement
    ret = self._execute_context(
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1842, in _execute_context
    return self._exec_single_context(
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1983, in _exec_single_context
    self._handle_dbapi_exception(
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2325, in _handle_dbapi_exception
    raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1964, in _exec_single_context
    self.dialect.do_execute(
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 747, in do_execute
    cursor.execute(statement, parameters)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 515, in execute
    self._adapt_connection.await_(
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 102, in await_only
    return current.driver.switch(awaitable)  # type: ignore[no-any-return]
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 160, in greenlet_spawn
    value = await result
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 494, in _prepare_and_execute
    self._handle_exception(error)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 444, in _handle_exception
    self._adapt_connection._handle_exception(error)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 717, in _handle_exception
    raise translated_error from error
sqlalchemy.exc.DBAPIError: (sqlalchemy.dialects.postgresql.asyncpg.Error) <class 'asyncpg.exceptions.DataError'>: invalid input for query argument $2: datetime.datetime(2023, 3, 3, 16, 0, tzi... (can't subtract offset-naive and offset-aware datetimes)
[SQL: SELECT nonebot_plugin_chatrecorder_messagerecord.id, nonebot_plugin_chatrecorder_messagerecord.bot_type, nonebot_plugin_chatrecorder_messagerecord.bot_id, nonebot_plugin_chatrecorder_messagerecord.platform, nonebot_plugin_chatrecorder_messagerecord.time, nonebot_plugin_chatrecorder_messagerecord.type, nonebot_plugin_chatrecorder_messagerecord.detail_type, nonebot_plugin_chatrecorder_messagerecord.message_id, nonebot_plugin_chatrecorder_messagerecord.message, nonebot_plugin_chatrecorder_messagerecord.plain_text, nonebot_plugin_chatrecorder_messagerecord.user_id, nonebot_plugin_chatrecorder_messagerecord.group_id, nonebot_plugin_chatrecorder_messagerecord.guild_id, nonebot_plugin_chatrecorder_messagerecord.channel_id 
FROM nonebot_plugin_chatrecorder_messagerecord 
WHERE nonebot_plugin_chatrecorder_messagerecord.platform = $1::VARCHAR AND nonebot_plugin_chatrecorder_messagerecord.time >= $2::TIMESTAMP WITHOUT TIME ZONE AND nonebot_plugin_chatrecorder_messagerecord.time <= $3::TIMESTAMP WITHOUT TIME ZONE AND nonebot_plugin_chatrecorder_messagerecord.type = $4::VARCHAR AND nonebot_plugin_chatrecorder_messagerecord.group_id = $5::VARCHAR]
[parameters: ('qq', datetime.datetime(2023, 3, 3, 16, 0, tzinfo=zoneinfo.ZoneInfo(key='UTC')), datetime.datetime(2023, 3, 4, 10, 32, 37, 481133, tzinfo=zoneinfo.ZoneInfo(key='UTC')), 'message', '12345678')]
(Background on this error at: https://sqlalche.me/e/20/dbapi)
he0119 commented 1 year ago

看来是时候加个 PostgreSQL 的测试了😥

LambdaYH commented 1 year ago

看来是时候加个 PostgreSQL 的测试了😥

mysql(mariadb10.5.18)+asyncmy下也报错,是建表时候就报了(小声

  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/starlette/routing.py", line 671, in lifespan
    async with self.lifespan_context(app):
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/starlette/routing.py", line 566, in __aenter__
    await self._router.startup()
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/starlette/routing.py", line 648, in startup
    await handler()
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/nonebot_plugin_datastore/db.py", line 65, in init_db
    await run_upgrade()
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/nonebot_plugin_datastore/script/utils.py", line 120, in run_upgrade
    await upgrade(config, "head")
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/nonebot_plugin_datastore/script/command.py", line 245, in upgrade
    await run_migration()
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/nonebot_plugin_datastore/script/utils.py", line 108, in run_migration
    await connection.run_sync(do_run_migrations, plugin_name)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/ext/asyncio/engine.py", line 787, in run_sync
    return await greenlet_spawn(fn, self._proxied, *arg, **kw)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 167, in greenlet_spawn
    result = context.switch(value)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/nonebot_plugin_datastore/script/utils.py", line 100, in do_run_migrations
    context.run_migrations()
  File "<string>", line 8, in run_migrations
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/alembic/runtime/environment.py", line 867, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/alembic/runtime/migration.py", line 624, in run_migrations
    step.migration_fn(**kw)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/nonebot_plugin_chatrecorder/migrations/7228a3a08576_ob12.py", line 58, in upgrade
    op.add_column(
  File "<string>", line 8, in add_column
  File "<string>", line 3, in add_column
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/alembic/operations/ops.py", line 2049, in add_column
    return operations.invoke(op)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/alembic/operations/base.py", line 401, in invoke
    return fn(self, operation)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/alembic/operations/toimpl.py", line 154, in add_column
    operations.impl.add_column(table_name, column, schema=schema, **kw)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/alembic/ddl/impl.py", line 322, in add_column
    self._exec(base.AddColumn(table_name, column, schema=schema))
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/alembic/ddl/impl.py", line 193, in _exec
    return conn.execute(  # type: ignore[call-overload]
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1414, in execute
    return meth(
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/sql/ddl.py", line 181, in _execute_on_connection
    return connection._execute_ddl(
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1523, in _execute_ddl
    compiled = ddl.compile(
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/sql/elements.py", line 282, in compile
    return self._compiler(dialect, **kw)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/sql/ddl.py", line 69, in _compiler
    return dialect.ddl_compiler(dialect, self, **kw)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/sql/compiler.py", line 710, in __init__
    self.string = self.process(self.statement, **compile_kwargs)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/sql/compiler.py", line 755, in process
    return obj._compiler_dispatch(self, **kwargs)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/ext/compiler.py", line 493, in <lambda>
    lambda *arg, **kw: existing(*arg, **kw),
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/ext/compiler.py", line 546, in __call__
    expr = fn(element, compiler, **kw)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/alembic/ddl/base.py", line 188, in visit_add_column
    add_column(compiler, element.column, **kw),
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/alembic/ddl/base.py", line 322, in add_column
    text = "ADD COLUMN %s" % compiler.get_column_specification(column, **kw)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/dialects/mysql/base.py", line 1775, in get_column_specification
    self.dialect.type_compiler_instance.process(
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/sql/compiler.py", line 800, in process
    return type_._compiler_dispatch(self, **kw)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/sql/visitors.py", line 143, in _compiler_dispatch
    return meth(self, **kw)  # type: ignore  # noqa: E501
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/sql/compiler.py", line 6544, in visit_string
    return self.visit_VARCHAR(type_, **kw)
  File "/root/.cache/pypoetry/virtualenvs/bot-AmtLDguX-py3.10/lib/python3.10/site-packages/sqlalchemy/dialects/mysql/base.py", line 2273, in visit_VARCHAR
    raise exc.CompileError(
sqlalchemy.exc.CompileError: VARCHAR requires a length on dialect mysql
he0119 commented 1 year ago

迁移脚本看来不支持 MySQL(暂时就这样吧,先把 PostgreSQL 支持了

LambdaYH commented 1 year ago

嗯,看了下好像迁移脚本里的sa.String()sa.VARCHAR()就会导致这问题,之前用sqlmodel的datastore的sqlmodel.sql.sqltypes.AutoString()就可以在mysql下用。

he0119 commented 1 year ago

是的,看起来 AutoString 做了处理。看了一下,它默认给 MySQL 设置了 255 的长度。

he0119 commented 1 year ago

把 MySQL 和 PostgreSQL 一起修了,欢迎尝试(