Skip to content

Commit

Permalink
chore (backend): added helper function to check if unique constraint …
Browse files Browse the repository at this point in the history
…exists in a table
  • Loading branch information
aybruhm committed Sep 12, 2024
1 parent 31cdcdd commit e5b8e1f
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions agenta-backend/agenta_backend/migrations/postgres/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

import click
import asyncpg

from sqlalchemy import inspect, text, Engine
from sqlalchemy.exc import ProgrammingError
from sqlalchemy.ext.asyncio import create_async_engine, AsyncEngine

from alembic import command
from alembic.config import Config
from sqlalchemy import inspect, text
from alembic.script import ScriptDirectory
from sqlalchemy.ext.asyncio import create_async_engine, AsyncEngine

from agenta_backend.utils.common import isCloudEE, isCloudDev

Expand Down Expand Up @@ -173,3 +174,31 @@ async def check_if_templates_table_exist():
await engine.dispose()

return True


def unique_constraint_exists(
engine: Engine, table_name: str, constraint_name: str
) -> bool:
"""
The function checks if a unique constraint with a specific name exists on a table in a PostgreSQL
database.
Args:
- engine (Engine): instance of a database engine that represents a connection to a database.
- table_name (str): name of the table to check the existence of the unique constraint.
- constraint_name (str): name of the unique constraint to check for existence.
Returns:
- returns a boolean value indicating whether a unique constraint with the specified `constraint_name` exists in the table.
"""

with engine.connect() as conn:
result = conn.execute(
text(
f"""
SELECT conname FROM pg_constraint
WHERE conname = '{constraint_name}' AND conrelid = '{table_name}'::regclass;
"""
)
)
return result.fetchone() is not None

0 comments on commit e5b8e1f

Please sign in to comment.