Skip to content

Question - Can you rollback inside an up one time script? #440

@rebeccapowell

Description

@rebeccapowell

Simplified example below, but I have a use case where I want to rollback the transaction on a one time script, only if the outcome isn't as expected. I realize that one time scripts are running in a transaction already. We have scenarios where some of our environmental databases are out of sync due to scripts being manually run outside of Roundhouse on occasion, and I can't always trust the data.

I'm basically saying, if outcome.Count isn't 15, then rollback. Is this possible in Roundhouse, or is there a better way to deal with this?

-- Required variables
DECLARE @Now DATETIME = GETDATE()
DECLARE @Code NVARCHAR(5) = 'ABCDE'
DECLARE @OldCode NVARCHAR(5) = 'XYX01'

-- Checks and balances table variable
DECLARE @Assignments TABLE
(
	-- ......
)

-- insert the existing assignment counts
INSERT INTO @Assignments
SELECT .....

-- The rows we are inserting
DECLARE @Regions TABLE
(
	-- ....
)

-- there are 15 inserts here
-- the checks and balances at
-- the end should confirm that
INSERT INTO @Regions
VALUES 
	....

-- Insert these new records
INSERT INTO [Table].......

-- how many assignments does each supplier have now?
UPDATE A SET 
	FinalAssignments = TotalAssignments
FROM @Assignments A
INNER JOIN (
SELECT ...
GROUP BY ...

-- If we have more than X new assignments for any supplier then something has gone wrong
IF EXISTS(SELECT NULL FROM @Assignments WHERE Assignments > 15)
BEGIN
	SELECT 'We have more than 15 added assignments to one or more. Something has gone wrong'
	ROLLBACK TRAN
	RETURN
END

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions