Update support for unique user IDs (#2921)
This commit is contained in:
@@ -0,0 +1,36 @@
|
|||||||
|
"""remove_global_unique_constraint_on_app_name_add_composite_unique
|
||||||
|
|
||||||
|
Revision ID: afd00efbd06b
|
||||||
|
Revises: add_config_table
|
||||||
|
Create Date: 2025-06-04 01:59:41.637440
|
||||||
|
|
||||||
|
"""
|
||||||
|
from typing import Sequence, Union
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision: str = 'afd00efbd06b'
|
||||||
|
down_revision: Union[str, None] = 'add_config_table'
|
||||||
|
branch_labels: Union[str, Sequence[str], None] = None
|
||||||
|
depends_on: Union[str, Sequence[str], None] = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
"""Upgrade schema."""
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_index('ix_apps_name', table_name='apps')
|
||||||
|
op.create_index(op.f('ix_apps_name'), 'apps', ['name'], unique=False)
|
||||||
|
op.create_index('idx_app_owner_name', 'apps', ['owner_id', 'name'], unique=True)
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
"""Downgrade schema."""
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_index('idx_app_owner_name', table_name='apps')
|
||||||
|
op.drop_index(op.f('ix_apps_name'), table_name='apps')
|
||||||
|
op.create_index('ix_apps_name', 'apps', ['name'], unique=True)
|
||||||
|
# ### end Alembic commands ###
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import enum
|
import enum
|
||||||
import uuid
|
import uuid
|
||||||
import datetime
|
import datetime
|
||||||
|
import sqlalchemy as sa
|
||||||
from sqlalchemy import (
|
from sqlalchemy import (
|
||||||
Column, String, Boolean, ForeignKey, Enum, Table,
|
Column, String, Boolean, ForeignKey, Enum, Table,
|
||||||
DateTime, JSON, Integer, UUID, Index, event
|
DateTime, JSON, Integer, UUID, Index, event
|
||||||
@@ -43,7 +44,7 @@ class App(Base):
|
|||||||
__tablename__ = "apps"
|
__tablename__ = "apps"
|
||||||
id = Column(UUID, primary_key=True, default=lambda: uuid.uuid4())
|
id = Column(UUID, primary_key=True, default=lambda: uuid.uuid4())
|
||||||
owner_id = Column(UUID, ForeignKey("users.id"), nullable=False, index=True)
|
owner_id = Column(UUID, ForeignKey("users.id"), nullable=False, index=True)
|
||||||
name = Column(String, unique=True, nullable=False, index=True)
|
name = Column(String, nullable=False, index=True)
|
||||||
description = Column(String)
|
description = Column(String)
|
||||||
metadata_ = Column('metadata', JSON, default=dict)
|
metadata_ = Column('metadata', JSON, default=dict)
|
||||||
is_active = Column(Boolean, default=True, index=True)
|
is_active = Column(Boolean, default=True, index=True)
|
||||||
@@ -55,6 +56,10 @@ class App(Base):
|
|||||||
owner = relationship("User", back_populates="apps")
|
owner = relationship("User", back_populates="apps")
|
||||||
memories = relationship("Memory", back_populates="app")
|
memories = relationship("Memory", back_populates="app")
|
||||||
|
|
||||||
|
__table_args__ = (
|
||||||
|
sa.UniqueConstraint('owner_id', 'name', name='idx_app_owner_name'),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class Config(Base):
|
class Config(Base):
|
||||||
__tablename__ = "configs"
|
__tablename__ = "configs"
|
||||||
|
|||||||
@@ -212,7 +212,8 @@ async def create_memory(
|
|||||||
if not user:
|
if not user:
|
||||||
raise HTTPException(status_code=404, detail="User not found")
|
raise HTTPException(status_code=404, detail="User not found")
|
||||||
# Get or create app
|
# Get or create app
|
||||||
app_obj = db.query(App).filter(App.name == request.app).first()
|
app_obj = db.query(App).filter(App.name == request.app,
|
||||||
|
App.owner_id == user.id).first()
|
||||||
if not app_obj:
|
if not app_obj:
|
||||||
app_obj = App(name=request.app, owner_id=user.id)
|
app_obj = App(name=request.app, owner_id=user.id)
|
||||||
db.add(app_obj)
|
db.add(app_obj)
|
||||||
|
|||||||
Reference in New Issue
Block a user