🐛 在Postgresql下,user_target字段使用jsonb代替json

This commit is contained in:
Cinte 2023-06-10 09:53:36 +08:00 committed by felinae98
parent 009bfb397e
commit 7dacae379a
3 changed files with 23 additions and 6 deletions

View File

@ -1,10 +1,10 @@
import datetime
from pathlib import Path
from typing import Optional
from nonebot_plugin_datastore import get_plugin_data
from nonebot_plugin_saa.utils import PlatformTarget
from sqlalchemy import JSON, ForeignKey, String, UniqueConstraint
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import Mapped, mapped_column, relationship
from ..types import Category, Tag
@ -15,7 +15,7 @@ get_plugin_data().set_migration_dir(Path(__file__).parent / "migrations")
class User(Model):
id: Mapped[int] = mapped_column(primary_key=True)
user_target: Mapped[dict] = mapped_column(JSON)
user_target: Mapped[dict] = mapped_column(JSON().with_variant(JSONB, "postgresql"))
subscribes: Mapped[list["Subscribe"]] = relationship(back_populates="user")

View File

@ -7,6 +7,7 @@ Create Date: 2023-03-20 00:39:30.199915
"""
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects.postgresql import JSONB
# revision identifiers, used by Alembic.
revision = "632b8086bc2b"
@ -19,7 +20,13 @@ def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("nonebot_bison_user", schema=None) as batch_op:
batch_op.drop_constraint("unique-user-constraint", type_="unique")
batch_op.add_column(sa.Column("user_target", sa.JSON(), nullable=True))
batch_op.add_column(
sa.Column(
"user_target",
sa.JSON().with_variant(JSONB, "postgresql"),
nullable=True,
)
)
# ### end Alembic commands ###

View File

@ -7,7 +7,7 @@ Create Date: 2023-03-20 11:08:42.883556
"""
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import sqlite
from sqlalchemy.dialects.postgresql import JSONB
# revision identifiers, used by Alembic.
revision = "67c38b3f39c2"
@ -16,11 +16,17 @@ branch_labels = None
depends_on = None
def jsonb_if_postgresql_else_json():
return sa.JSON().with_variant(JSONB, "postgresql")
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("nonebot_bison_user", schema=None) as batch_op:
batch_op.alter_column(
"user_target", existing_type=sqlite.JSON(), nullable=False
"user_target",
existing_type=jsonb_if_postgresql_else_json(),
nullable=False,
)
# ### end Alembic commands ###
@ -29,6 +35,10 @@ def upgrade() -> None:
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("nonebot_bison_user", schema=None) as batch_op:
batch_op.alter_column("user_target", existing_type=sqlite.JSON(), nullable=True)
batch_op.alter_column(
"user_target",
existing_type=jsonb_if_postgresql_else_json(),
nullable=True,
)
# ### end Alembic commands ###