Fix transaction logic
This commit is contained in:
Родитель
314e6fd3cd
Коммит
500cb9deb2
|
@ -57,11 +57,7 @@ assert package_exists_in_scope("{sqlpkgname}", "{scopestr}")
|
|||
|
||||
@property
|
||||
def base_script(self) -> str:
|
||||
return """
|
||||
-- Wrap this in a transaction
|
||||
DECLARE @TransactionName varchar(30) = 'SqlPackageTransaction';
|
||||
BEGIN TRAN @TransactionName
|
||||
|
||||
return """
|
||||
-- Drop the library if it exists
|
||||
BEGIN TRY
|
||||
DROP EXTERNAL LIBRARY [{sqlpkgname}] {authorization}
|
||||
|
@ -84,13 +80,9 @@ BEGIN TRY
|
|||
exec sp_execute_external_script
|
||||
@language = N'Python',
|
||||
@script = %s
|
||||
-- Installation succeeded, commit the transaction
|
||||
COMMIT TRAN @TransactionName
|
||||
print('Package successfully installed.')
|
||||
END TRY
|
||||
BEGIN CATCH
|
||||
-- Installation failed, rollback the transaction
|
||||
ROLLBACK TRAN @TransactionName
|
||||
print('Package installation failed.');
|
||||
THROW;
|
||||
END CATCH
|
||||
|
|
|
@ -177,12 +177,16 @@ class SQLPackageManager:
|
|||
target_name = get_package_name_from_file(target_package_file)
|
||||
|
||||
with SQLQueryExecutor(connection=self._connection_info) as sqlexecutor:
|
||||
transaction = SQLTransaction(sqlexecutor, clean_library_name(target_name) + "InstallTransaction")
|
||||
transaction.begin()
|
||||
try:
|
||||
for pkgfile in dependency_files:
|
||||
self._install_single(sqlexecutor, pkgfile, scope, out_file=out_file)
|
||||
self._install_single(sqlexecutor, target_package_file, scope, True, out_file=out_file)
|
||||
except Exception:
|
||||
raise RuntimeError("Package installation failed, installed dependencies were rolled back.")
|
||||
transaction.commit()
|
||||
except Exception as e:
|
||||
transaction.rollback()
|
||||
raise RuntimeError("Package installation failed, installed dependencies were rolled back.") from e
|
||||
|
||||
@staticmethod
|
||||
def _install_single(sqlexecutor: SQLQueryExecutor, package_file: str, scope: Scope, is_target=False, out_file: str=None):
|
||||
|
|
|
@ -58,7 +58,7 @@ class SQLQueryExecutor:
|
|||
self._mssqlconn.execute_non_query(builder.base_script, builder.params)
|
||||
return []
|
||||
except Exception as e:
|
||||
raise RuntimeError(str.format("Error in SQL Execution: {error}", error=str(e)))
|
||||
raise RuntimeError("Error in SQL Execution") from e
|
||||
|
||||
def execute_query(self, query, params, out_file=None):
|
||||
if out_file is not None:
|
||||
|
|
Загрузка…
Ссылка в новой задаче