mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-06 03:46:10 +08:00
still problem
This commit is contained in:
parent
0bf530035c
commit
4ea680e8dc
@ -52,7 +52,7 @@ version_path_separator = os # Use os.pathsep. Default configuration used for ne
|
||||
# are written from script.py.mako
|
||||
# output_encoding = utf-8
|
||||
|
||||
sqlalchemy.url = driver://user:pass@localhost/dbname
|
||||
sqlalchemy.url = sqlite:///data/data.db
|
||||
|
||||
|
||||
[post_write_hooks]
|
||||
@ -66,6 +66,13 @@ sqlalchemy.url = driver://user:pass@localhost/dbname
|
||||
# black.entrypoint = black
|
||||
# black.options = -l 79 REVISION_SCRIPT_FILENAME
|
||||
|
||||
hooks = pre-commit
|
||||
|
||||
pre-commit.type = console_scripts
|
||||
pre-commit.entrypoint = pre-commit
|
||||
pre-commit.options = run --files REVISION_SCRIPT_FILENAME
|
||||
pre-commit.cwd = %(here)s
|
||||
|
||||
# Logging configuration
|
||||
[loggers]
|
||||
keys = root,sqlalchemy,alembic
|
||||
|
@ -1,3 +1,5 @@
|
||||
from nonebot.plugin import require
|
||||
|
||||
from . import (
|
||||
admin_page,
|
||||
config,
|
||||
@ -11,6 +13,20 @@ from . import (
|
||||
)
|
||||
from .plugin_config import plugin_config
|
||||
|
||||
require("nonebot_plugin_localstore")
|
||||
|
||||
__help__version__ = "0.4.3"
|
||||
__help__plugin__name__ = "nonebot_bison"
|
||||
__usage__ = f"本bot可以提供b站、微博等社交媒体的消息订阅,详情请查看本bot文档,或者{'at本bot' if plugin_config.bison_to_me else '' }发送“添加订阅”订阅第一个帐号,发送“查询订阅”或“删除订阅”管理订阅"
|
||||
|
||||
__all__ = [
|
||||
"admin_page",
|
||||
"config",
|
||||
"config_manager",
|
||||
"post",
|
||||
"scheduler",
|
||||
"send",
|
||||
"platform",
|
||||
"types",
|
||||
"utils",
|
||||
]
|
||||
|
@ -1 +1,2 @@
|
||||
from .config_legacy import NoSuchSubscribeException, NoSuchUserException, config
|
||||
from .db import DATA
|
||||
|
18
src/plugins/nonebot_bison/config/db.py
Normal file
18
src/plugins/nonebot_bison/config/db.py
Normal file
@ -0,0 +1,18 @@
|
||||
from pathlib import Path
|
||||
|
||||
import nonebot
|
||||
from alembic import command
|
||||
from alembic.config import Config
|
||||
from nonebot_plugin_datastore import PluginData, create_session, db
|
||||
|
||||
DATA = PluginData("bison")
|
||||
|
||||
|
||||
@nonebot.get_driver().on_startup
|
||||
async def upgrade_db():
|
||||
alembic_cfg = Config()
|
||||
alembic_cfg.set_main_option(
|
||||
"script_location", str(Path(__file__).parent.joinpath("migrate"))
|
||||
)
|
||||
alembic_cfg.set_main_option("sqlalchemy.url", "")
|
||||
command.upgrade(alembic_cfg, "head")
|
@ -1,11 +1,8 @@
|
||||
from sqlalchemy.orm import declarative_base, relationship
|
||||
from sqlalchemy.orm.decl_api import DeclarativeMeta
|
||||
from sqlalchemy.sql.schema import Column, ForeignKey
|
||||
from sqlalchemy.sql.sqltypes import Integer, String
|
||||
|
||||
|
||||
class Base(metaclass=DeclarativeMeta):
|
||||
__abstract__ = True
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
class User(Base):
|
||||
@ -28,6 +25,7 @@ class Target(Base):
|
||||
class Subscribe(Base):
|
||||
__tablename__ = "subscribe"
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
target_id = Column(Integer, ForeignKey(Target.id))
|
||||
user_id = Column(Integer, ForeignKey(User.id))
|
||||
categories = Column(String(1024))
|
||||
|
@ -1,7 +1,9 @@
|
||||
import asyncio
|
||||
from logging.config import fileConfig
|
||||
|
||||
from alembic import context
|
||||
from sqlalchemy import engine_from_config, pool
|
||||
from sqlalchemy.engine.base import Connection
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
@ -9,13 +11,24 @@ config = context.config
|
||||
|
||||
# Interpret the config file for Python logging.
|
||||
# This line sets up loggers basically.
|
||||
fileConfig(config.config_file_name) # type:ignore
|
||||
if config.config_file_name:
|
||||
fileConfig(config.config_file_name) # type:ignore
|
||||
|
||||
# add your model's MetaData object here
|
||||
# for 'autogenerate' support
|
||||
# from myapp import mymodel
|
||||
# target_metadata = mymodel.Base.metadata
|
||||
from ..db_model import Base
|
||||
|
||||
import nonebot
|
||||
|
||||
try:
|
||||
nonebot.get_driver()
|
||||
__as_plugin = True
|
||||
except:
|
||||
__as_plugin = False
|
||||
nonebot.init()
|
||||
|
||||
from nonebot_bison.config.db_model import Base
|
||||
|
||||
target_metadata = Base.metadata
|
||||
|
||||
@ -49,6 +62,22 @@ def run_migrations_offline():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def do_run_migration(connection: Connection):
|
||||
context.configure(connection=connection, target_metadata=target_metadata)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
async def run_migrations_async():
|
||||
|
||||
from nonebot_plugin_datastore.db import get_engine
|
||||
|
||||
connectable = get_engine()
|
||||
async with connectable.connect() as connection:
|
||||
await connection.run_sync(do_run_migration)
|
||||
|
||||
|
||||
def run_migrations_online():
|
||||
"""Run migrations in 'online' mode.
|
||||
|
||||
@ -56,17 +85,18 @@ def run_migrations_online():
|
||||
and associate a connection with the context.
|
||||
|
||||
"""
|
||||
connectable = engine_from_config(
|
||||
config.get_section(config.config_ini_section),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
if not __as_plugin:
|
||||
connectable = engine_from_config(
|
||||
config.get_section(config.config_ini_section),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
|
||||
with connectable.connect() as connection:
|
||||
context.configure(connection=connection, target_metadata=target_metadata)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
with connectable.connect() as connection:
|
||||
do_run_migration(connection)
|
||||
else:
|
||||
# asyncio.run(run_migrations_async())
|
||||
asyncio.create_task(run_migrations_async())
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
|
@ -0,0 +1,60 @@
|
||||
"""init db
|
||||
|
||||
Revision ID: 0571870f5222
|
||||
Revises:
|
||||
Create Date: 2022-03-21 19:18:13.762626
|
||||
|
||||
"""
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "0571870f5222"
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table(
|
||||
"target",
|
||||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column("platform_name", sa.String(length=20), nullable=False),
|
||||
sa.Column("target", sa.String(length=1024), nullable=False),
|
||||
sa.Column("target_name", sa.String(length=1024), nullable=False),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.create_table(
|
||||
"user",
|
||||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column("type", sa.String(length=20), nullable=False),
|
||||
sa.Column("uid", sa.Integer(), nullable=False),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.create_table(
|
||||
"subscribe",
|
||||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column("target_id", sa.Integer(), nullable=True),
|
||||
sa.Column("user_id", sa.Integer(), nullable=True),
|
||||
sa.Column("categories", sa.String(length=1024), nullable=True),
|
||||
sa.Column("tags", sa.String(length=1024), nullable=True),
|
||||
sa.ForeignKeyConstraint(
|
||||
["target_id"],
|
||||
["target.id"],
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
["user_id"],
|
||||
["user.id"],
|
||||
),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_table("subscribe")
|
||||
op.drop_table("user")
|
||||
op.drop_table("target")
|
||||
# ### end Alembic commands ###
|
Loading…
x
Reference in New Issue
Block a user