Repository: microsoft/AzureTRE

Issue:
make all/tre-deploy fails due to db-migrate
**Describe the bug**
Running make `all` (or `tre-deploy` for that matter) fails because it runs `db-migrate` that needs direct access to cosmos.

**Steps to reproduce**

1. Make sure you don't have `ENABLE_LOCAL_DEBUGGING=true` in your .env files
2. Deploy a new environment by running `make all` or `make tre-deploy`
3. See that the activity fail due to lack of access to cosmos



Discussion:

Resolving patch:
diff --git a/scripts/db_migrations.py b/scripts/db_migrations.py
--- a/scripts/db_migrations.py
+++ b/scripts/db_migrations.py
@@ -16,11 +16,14 @@
 class TRECosmosDBMigrations:
 
     def __init__(self):
+        if (self.can_connect_to_cosmos()):
+            url = os.environ['STATE_STORE_ENDPOINT']
+            key = self.get_store_key()
+            self.client = CosmosClient(url=url, credential=key)
+            self.database = self.client.get_database_client(STATE_STORE_DATABASE)
 
-        url = os.environ['STATE_STORE_ENDPOINT']
-        key = self.get_store_key()
-        self.client = CosmosClient(url=url, credential=key)
-        self.database = self.client.get_database_client(STATE_STORE_DATABASE)
+    def can_connect_to_cosmos(self) -> bool:
+        return os.getenv('ENABLE_LOCAL_DEBUGGING', 'False').lower() in ('true', 1, 't') if 'ENABLE_LOCAL_DEBUGGING' in os.environ else False
 
     def get_store_key(self) -> str:
         if 'STATE_STORE_KEY' in os.environ:
@@ -106,24 +109,27 @@ def moveAuthInformationToProperties(self, resources_container_name):
 
 def main():
     migrations = TRECosmosDBMigrations()
-    # PR 1030
-    migrations.renameCosmosDBFields("Resources", 'resourceTemplateName', 'templateName')
-    migrations.renameCosmosDBFields("Resources", 'resourceTemplateVersion', 'templateVersion')
-    migrations.renameCosmosDBFields("Resources", 'resourceTemplateParameters', 'properties')
-
-    # PR 1031
-    migrations.renameCosmosDBFields("Resources", 'workspaceType', 'templateName')
-    migrations.renameCosmosDBFields("Resources", 'workspaceServiceType', 'templateName')
-    migrations.renameCosmosDBFields("Resources", 'userResourceType', 'templateName')
-
-    # Operations History
-    migrations.moveDeploymentsToOperations("Resources", "Operations")
-
-    # Shared services (PR #1717)
-    migrations.deleteDuplicatedSharedServices("Resources")
-
-    # Authentication needs to be in properties so we can update them. (PR #1726)
-    migrations.moveAuthInformationToProperties("Resources")
+    if not migrations.can_connect_to_cosmos():
+        print('You cannot migrate the cosmos database without setting ENABLE_LOCAL_DEBUGGING to true.')
+    else:
+        # PR 1030
+        migrations.renameCosmosDBFields("Resources", 'resourceTemplateName', 'templateName')
+        migrations.renameCosmosDBFields("Resources", 'resourceTemplateVersion', 'templateVersion')
+        migrations.renameCosmosDBFields("Resources", 'resourceTemplateParameters', 'properties')
+
+        # PR 1031
+        migrations.renameCosmosDBFields("Resources", 'workspaceType', 'templateName')
+        migrations.renameCosmosDBFields("Resources", 'workspaceServiceType', 'templateName')
+        migrations.renameCosmosDBFields("Resources", 'userResourceType', 'templateName')
+
+        # Operations History
+        migrations.moveDeploymentsToOperations("Resources", "Operations")
+
+        # Shared services (PR #1717)
+        migrations.deleteDuplicatedSharedServices("Resources")
+
+        # Authentication needs to be in properties so we can update them. (PR #1726)
+        migrations.moveAuthInformationToProperties("Resources")
 
 
 if __name__ == "__main__":


Test patch: