|
1 | 1 | from azure.cosmos import exceptions, CosmosClient, PartitionKey
|
2 |
| -import os, sys |
3 |
| - |
| 2 | +import os, sys, json |
| 3 | + |
| 4 | +with open('/usr/src/app/cosmosdb-emulator/seed_database.json') as database_file: |
| 5 | + seed_database=json.load(database_file) |
| 6 | + |
4 | 7 | sys.path.append("/usr/src/app")
|
5 | 8 |
|
6 | 9 | DATABASE_ACCOUNT_URI = os.environ.get('DATABASE_ACCOUNT_URI')
|
|
21 | 24 | try:
|
22 | 25 | print('- Project')
|
23 | 26 | from time_tracker_api.projects.projects_model import container_definition as project_definition
|
24 |
| - database.create_container_if_not_exists(**project_definition) |
| 27 | + project_container=database.create_container_if_not_exists(**project_definition) |
| 28 | + for project in seed_database['projects']: |
| 29 | + project_container.create_item(body=project) |
25 | 30 |
|
26 | 31 | print('- Project type')
|
27 | 32 | 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) |
| 33 | + project_type_container=database.create_container_if_not_exists(**project_type_definition) |
| 34 | + for project_type in seed_database['project_types']: |
| 35 | + project_type_container.create_item(body=project_type) |
29 | 36 |
|
30 | 37 | print('- Activity')
|
31 | 38 | from time_tracker_api.activities.activities_model import container_definition as activity_definition
|
32 |
| - database.create_container_if_not_exists(**activity_definition) |
| 39 | + activity_container=database.create_container_if_not_exists(**activity_definition) |
| 40 | + for activity in seed_database['activities']: |
| 41 | + activity_container.create_item(body=activity) |
33 | 42 |
|
34 | 43 | print('- Customer')
|
35 | 44 | from time_tracker_api.customers.customers_model import container_definition as customer_definition
|
36 |
| - database.create_container_if_not_exists(**customer_definition) |
| 45 | + customer_container=database.create_container_if_not_exists(**customer_definition) |
| 46 | + for customer in seed_database['customers']: |
| 47 | + customer_container.create_item(body=customer) |
37 | 48 |
|
38 | 49 | print('- Time entry')
|
39 | 50 | 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) |
| 51 | + time_entry_container=database.create_container_if_not_exists(**time_entry_definition) |
| 52 | + for time_entry in seed_database['time_entries']: |
| 53 | + time_entry_container.create_item(body=time_entry) |
41 | 54 |
|
42 | 55 | print('- Technology')
|
43 | 56 | from time_tracker_api.technologies.technologies_model import container_definition as technologies_definition
|
44 | 57 | database.create_container_if_not_exists(**technologies_definition)
|
45 | 58 | except exceptions.CosmosResourceExistsError as e:
|
46 | 59 | print("Unexpected error while creating initial database schema: %s" % e.message)
|
47 | 60 |
|
| 61 | +database_file.close() |
| 62 | + |
48 | 63 | print("Done!")
|
49 | 64 |
|
0 commit comments