Repository: mathesar-foundation/mathesar

Issue:
New connection creation flow does not handle schema creation failure scenarios
## Description
1. Create a database manually, say `test_db`, and create a schema called `Library Management` in it.
1. In Mathesar, create a new connection to the database.
1. While creating the connection, in the "New PostgreSQL Database Connection" modal, check the "Library Management" schema in the "Schemas to install" field.
   ![Screenshot 2024-01-26 at 1 01 21 PM](https://github.com/mathesar-foundation/mathesar/assets/11850603/ec5c109b-d779-4d56-96a0-1c209e3deb3d)
1. Click on 'Add Connection'.
1. An error is thrown:
   - `"(psycopg2.errors.DuplicateSchema) schema \"Library Management\" already exists\n\n[SQL: CREATE SCHEMA \"Library Management\";]\n(Background on this error at: https://sqlalche.me/e/14/f405)"`.
   - This is expected.
1. The modal remains open. Uncheck the "Library Management" schema, and try clicking on 'Add Connection' again.
1. Another error is thrown:
   - `duplicate key value violates unique constraint "mathesar_database_name_key" DETAIL: Key (name)=(pavish_install_test_1) already exists.`
   - From the user perpective, there's no clear next step for them to take when this happens.
1. Close the modal and refresh the page, notice that the connection has been created already.
1. Open the connection, and notice that it shows `Schemas (0)`.
1. Forcing a manual reflection using the "Sync external changes" button shows the public schema and existing tables.

### Note:
* I performed the above steps 1-3 only as a means to force an error in the schema creation flow. While testing, I encountered an error which occurred due to an unknown reason. 
* This issue primarily focuses on steps 4-10.

## Expected behavior
* When an error occurs, there should not be an inconsistent middle state in the DB vs the UI.
* When schema creation fails, we should try to revert back to the original state, or we should make it extra clear on the UI that it's a multi-step process.


Discussion:
@seancolsen @mathemancer  I've placed this in the 'High priority' milestone, because this isn't something I think we'd want to handle in 0.1.4. But if either of you feel otherwise, we can try to fix it in 0.1.4. 

Resolving patch:
diff --git a/mathesar/utils/connections.py b/mathesar/utils/connections.py
--- a/mathesar/utils/connections.py
+++ b/mathesar/utils/connections.py
@@ -1,6 +1,6 @@
 """Utilities to help with creating and managing connections in Mathesar."""
 from psycopg2.errors import DuplicateSchema
-from sqlalchemy.exc import OperationalError
+from sqlalchemy.exc import OperationalError, ProgrammingError
 from mathesar.models.base import Database
 from db import install, connection as dbconn
 from mathesar.state import reset_reflection
@@ -92,10 +92,11 @@ def _load_sample_data(engine, sample_data):
     for key in sample_data:
         try:
             DATASET_MAP[key](engine, safe_mode=True)
-        except DuplicateSchema:
-            # We swallow this error, since otherwise we'll raise an error on the
-            # front end even though installation generally succeeded.
-            continue
+        except ProgrammingError as e:
+            if isinstance(e.orig, DuplicateSchema):
+                # We swallow this error, since otherwise we'll raise an error on the
+                # front end even though installation generally succeeded.
+                continue
     reset_reflection()
 
 


Test patch: