Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Role Duplication Error in azd up Command for PostgreSQL #1621

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading