|
| 1 | +from azure.cosmos import exceptions, CosmosClient, PartitionKey |
| 2 | +import os, sys |
| 3 | + |
| 4 | +sys.path.append("/usr/src/app") |
| 5 | + |
| 6 | +DATABASE_ACCOUNT_URI = os.environ.get('DATABASE_ACCOUNT_URI') |
| 7 | +DATABASE_MASTER_KEY = os.environ.get('DATABASE_MASTER_KEY') |
| 8 | + |
| 9 | +endpoint = DATABASE_ACCOUNT_URI |
| 10 | +key = DATABASE_MASTER_KEY |
| 11 | + |
| 12 | +# <create_cosmos_client> |
| 13 | +client = CosmosClient(endpoint, key) |
| 14 | +# <create_database_if_not_exists> |
| 15 | +database_name = 'time-tracker-db' |
| 16 | +database = client.create_database_if_not_exists(id=database_name) |
| 17 | +# </create_containers_if_not_exists> |
| 18 | + |
| 19 | +print("Creating TimeTracker initial initial database schema...") |
| 20 | + |
| 21 | +try: |
| 22 | + print('- Project') |
| 23 | + from time_tracker_api.projects.projects_model import container_definition as project_definition |
| 24 | + database.create_container_if_not_exists(**project_definition) |
| 25 | + |
| 26 | + print('- Project type') |
| 27 | + from time_tracker_api.project_types.project_types_model import container_definition as project_type_definition |
| 28 | + database.create_container_if_not_exists(**project_type_definition) |
| 29 | + |
| 30 | + print('- Activity') |
| 31 | + from time_tracker_api.activities.activities_model import container_definition as activity_definition |
| 32 | + database.create_container_if_not_exists(**activity_definition) |
| 33 | + |
| 34 | + print('- Customer') |
| 35 | + from time_tracker_api.customers.customers_model import container_definition as customer_definition |
| 36 | + database.create_container_if_not_exists(**customer_definition) |
| 37 | + |
| 38 | + print('- Time entry') |
| 39 | + from time_tracker_api.time_entries.time_entries_model import container_definition as time_entry_definition |
| 40 | + database.create_container_if_not_exists(**time_entry_definition) |
| 41 | + |
| 42 | + print('- Technology') |
| 43 | + from time_tracker_api.technologies.technologies_model import container_definition as technologies_definition |
| 44 | + database.create_container_if_not_exists(**technologies_definition) |
| 45 | +except exceptions.CosmosResourceExistsError as e: |
| 46 | + print("Unexpected error while creating initial database schema: %s" % e.message) |
| 47 | + |
| 48 | +print("Done!") |
| 49 | + |
0 commit comments