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

init db script #2

Merged
merged 2 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions utilities/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
URI=https://localhost:8081
PRIMARY_KEY=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==
DB_NAME=db
CONTAINER_NAMES=creditor_institutions,services
PARTITION_KEYS=fiscalCode,transferCategory
41 changes: 41 additions & 0 deletions utilities/cosmos_init_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from azure.cosmos import exceptions, CosmosClient, PartitionKey
import os
from dotenv import load_dotenv

load_dotenv()

URI = os.getenv('URI')
PRIMARY_KEY = os.getenv('PRIMARY_KEY')
DB_NAME = os.getenv('DB_NAME')
CONTAINER_NAMES = os.getenv('CONTAINER_NAMES')
PARTITION_KEYS = os.getenv('PARTITION_KEYS')

# Initialize the Cosmos client
endpoint = URI
key = PRIMARY_KEY

# <create_cosmos_client>
client = CosmosClient(endpoint, key)
# </create_cosmos_client>

# Create a database
# <create_database_if_not_exists>
database_name = DB_NAME
database = client.create_database_if_not_exists(id=database_name)
# </create_database_if_not_exists>

# Create a container
# Using a good partition key improves the performance of database operations.
# <create_container_if_not_exists>
container_names = CONTAINER_NAMES.split(',')
partition_keys = PARTITION_KEYS.split(',')

containers_infra = zip(container_names, partition_keys)

for container in containers_infra:
container = database.create_container_if_not_exists(
id=container[0], # container name
partition_key=PartitionKey(path=f"/{container[1]}"),
offer_throughput=400
)
# </create_container_if_not_exists>
File renamed without changes.