Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rebeccapowell opened this issue Sep 13, 2021 · 1 comment
Open

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

rebeccapowell opened this issue Sep 13, 2021 · 1 comment

Comments

@rebeccapowell
Copy link

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
@wokket
Copy link

wokket commented Sep 19, 2021

What's the current behaviour if you try that rollback? Does RH terminate the migration (due to an error or otherwise)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants