Skip to content

Commit

Permalink
fix: Role Duplication Error in azd up Command for PostgreSQL (#1621)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavan-Microsoft authored Jan 10, 2025
1 parent 2d1af79 commit c553312
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions scripts/data_scripts/create_postgres_tables.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import json
from azure.keyvault.secrets import SecretClient
from azure.identity import DefaultAzureCredential
import psycopg2
from psycopg2 import sql
Expand All @@ -24,12 +22,21 @@ def grant_permissions(cursor, dbname, schema_name, principal_name):
- principal_name: Name of the principal (role or user) to grant permissions.
"""

add_principal_user_query = sql.SQL("SELECT * FROM pgaadauth_create_principal({principal}, false, false)")
# Check if the principal exists in the database
cursor.execute(
add_principal_user_query.format(
principal=sql.Literal(principal_name),
sql.SQL("SELECT 1 FROM pg_roles WHERE rolname = {principal}").format(
principal=sql.Literal(principal_name)
)
)
if cursor.fetchone() is None:
add_principal_user_query = sql.SQL(
"SELECT * FROM pgaadauth_create_principal({principal}, false, false)"
)
cursor.execute(
add_principal_user_query.format(
principal=sql.Literal(principal_name),
)
)

# Grant CONNECT on database
grant_connect_query = sql.SQL("GRANT CONNECT ON DATABASE {database} TO {principal}")
Expand Down Expand Up @@ -123,7 +130,9 @@ def grant_permissions(cursor, dbname, schema_name, principal_name):
conn.commit()


cursor.execute("CREATE INDEX vector_store_content_vector_idx ON vector_store USING hnsw (content_vector vector_cosine_ops);")
cursor.execute(
"CREATE INDEX vector_store_content_vector_idx ON vector_store USING hnsw (content_vector vector_cosine_ops);"
)
conn.commit()

grant_permissions(cursor, dbname, "public", principal_name)
Expand Down

0 comments on commit c553312

Please sign in to comment.