Allow Duplicate Naming for CC-Pair (#862)

This commit is contained in:
Yuhong Sun
2023-12-22 23:03:44 -08:00
committed by GitHub
parent 016a087b10
commit 8b7d01fb3b
3 changed files with 44 additions and 8 deletions

View File

@@ -0,0 +1,32 @@
"""CC-Pair Name not Unique
Revision ID: 76b60d407dfb
Revises: b156fa702355
Create Date: 2023-12-22 21:42:10.018804
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "76b60d407dfb"
down_revision = "b156fa702355"
branch_labels = None
depends_on = None
def upgrade() -> None:
op.execute("DELETE FROM connector_credential_pair WHERE name IS NULL")
op.drop_constraint(
"connector_credential_pair__name__key",
"connector_credential_pair",
type_="unique",
)
op.alter_column(
"connector_credential_pair", "name", existing_type=sa.String(), nullable=False
)
def downgrade() -> None:
# This wasn't really required by the code either, no good reason to make it unique again
pass

View File

@@ -36,8 +36,12 @@ def fetch_connectors(
return list(results.all())
def connector_by_name_exists(connector_name: str, db_session: Session) -> bool:
stmt = select(Connector).where(Connector.name == connector_name)
def connector_by_name_source_exists(
connector_name: str, source: DocumentSource, db_session: Session
) -> bool:
stmt = select(Connector).where(
Connector.name == connector_name, Connector.source == source
)
result = db_session.execute(stmt)
connector = result.scalar_one_or_none()
return connector is not None
@@ -67,7 +71,9 @@ def create_connector(
connector_data: ConnectorBase,
db_session: Session,
) -> ObjectCreationIdResponse:
if connector_by_name_exists(connector_data.name, db_session):
if connector_by_name_source_exists(
connector_data.name, connector_data.source, db_session
):
raise ValueError(
"Connector by this name already exists, duplicate naming not allowed."
)
@@ -95,8 +101,8 @@ def update_connector(
if connector is None:
return None
if connector_data.name != connector.name and connector_by_name_exists(
connector_data.name, db_session
if connector_data.name != connector.name and connector_by_name_source_exists(
connector_data.name, connector_data.source, db_session
):
raise ValueError(
"Connector by this name already exists, duplicate naming not allowed."

View File

@@ -173,9 +173,7 @@ class ConnectorCredentialPair(Base):
unique=True,
nullable=False,
)
name: Mapped[str] = mapped_column(
String, unique=True, nullable=True
) # nullable for backwards compatability
name: Mapped[str] = mapped_column(String, nullable=False)
connector_id: Mapped[int] = mapped_column(
ForeignKey("connector.id"), primary_key=True
)