fix: Test connection before starting on create transaction (#15100)

* test connection before starting on create transaction

* Update create.py
This commit is contained in:
Hugh A. Miles II 2021-06-14 12:44:27 -04:00 committed by GitHub
parent 82ad053e50
commit 143a1b0650
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 10 deletions

View File

@ -44,20 +44,21 @@ class CreateDatabaseCommand(BaseCommand):
def run(self) -> Model:
self.validate()
try:
# Test connection before starting create transaction
TestConnectionDatabaseCommand(self._actor, self._properties).run()
except Exception as ex: # pylint: disable=broad-except
event_logger.log_with_context(
action=f"db_creation_failed.{ex.__class__.__name__}",
engine=self._properties.get("sqlalchemy_uri", "").split(":")[0],
)
raise DatabaseConnectionFailedError()
try:
database = DatabaseDAO.create(self._properties, commit=False)
database.set_sqlalchemy_uri(database.sqlalchemy_uri)
try:
TestConnectionDatabaseCommand(self._actor, self._properties).run()
except Exception as ex: # pylint: disable=broad-except
db.session.rollback()
event_logger.log_with_context(
action=f"db_creation_failed.{ex.__class__.__name__}",
engine=database.db_engine_spec.__name__,
)
raise DatabaseConnectionFailedError()
# adding a new database we always want to force refresh schema list
schemas = database.get_all_schema_names(cache=False)
for schema in schemas:
@ -67,6 +68,7 @@ class CreateDatabaseCommand(BaseCommand):
security_manager.add_permission_view_menu("database_access", database.perm)
db.session.commit()
except DAOCreateFailedError as ex:
db.session.rollback()
event_logger.log_with_context(
action=f"db_creation_failed.{ex.__class__.__name__}",
engine=database.db_engine_spec.__name__,