forked from babelfish-for-postgresql/babelfish_extensions
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for trigger FK reference fix on table/temp variables (babel…
…fish-for-postgresql#3440) This commit adds test cases to validate the fix for trigger failures with foreign key references on table variables and temp tables. Task: BABEL-5270 Signed-off-by: Roshan Kanwar <[email protected]> (cherry picked from commit 3c342e3)
- Loading branch information
1 parent
fb1b02c
commit 411928e
Showing
7 changed files
with
1,381 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
-- triggers | ||
DROP TRIGGER IF EXISTS tr_SpecialChar_Insert | ||
DROP TRIGGER IF EXISTS tr_Child_CaseInsensitive | ||
DROP TRIGGER IF EXISTS tr_Level3_Insert | ||
DROP TRIGGER IF EXISTS tr_Child_NullHandling | ||
DROP TRIGGER IF EXISTS tr_Parent_Insert | ||
DROP TRIGGER IF EXISTS tr_Child_Update | ||
DROP TRIGGER IF EXISTS tr_Child_Insert | ||
DROP TRIGGER IF EXISTS [trg_i_COD] | ||
DROP TRIGGER IF EXISTS tr_UniqueChild_Insert; | ||
DROP TRIGGER IF EXISTS tr_UniqueChild_Update; | ||
DROP TRIGGER IF EXISTS tr_UniqueChild_ComplexInsert; | ||
GO | ||
|
||
-- constraints | ||
ALTER TABLE UniqueChild DROP CONSTRAINT IF EXISTS FK_UniqueChild_UniqueParent; | ||
ALTER TABLE ChildNoAction DROP CONSTRAINT IF EXISTS FK_ChildNoAction_Parent; | ||
ALTER TABLE ChildCascade DROP CONSTRAINT IF EXISTS FK_ChildCascade_Parent; | ||
ALTER TABLE ChildSetNull DROP CONSTRAINT IF EXISTS FK_ChildSetNull_Parent; | ||
ALTER TABLE ChildSetDefault DROP CONSTRAINT IF EXISTS FK_ChildSetDefault_Parent; | ||
ALTER TABLE [dbo].[OrderItem] DROP CONSTRAINT IF EXISTS [FK_OrderItem_Order]; | ||
ALTER TABLE [dbo].[Parent] DROP CONSTRAINT IF EXISTS [FK_Parent_GrandParent]; | ||
ALTER TABLE [dbo].[Child] DROP CONSTRAINT IF EXISTS [FK_Child_Parent]; | ||
ALTER TABLE [dbo].[Child#Table] DROP CONSTRAINT IF EXISTS [FK_ChildTable_ParentTable]; | ||
ALTER TABLE [dbo].[Level2] DROP CONSTRAINT IF EXISTS [FK_Level2_Level1]; | ||
ALTER TABLE [dbo].[Level3] DROP CONSTRAINT IF EXISTS [FK_Level3_Level2]; | ||
GO | ||
|
||
-- tables | ||
DROP TABLE IF EXISTS ChildNoAction | ||
DROP TABLE IF EXISTS ChildCascade | ||
DROP TABLE IF EXISTS ChildSetNull | ||
DROP TABLE IF EXISTS ChildSetDefault | ||
DROP TABLE IF EXISTS ParentNoAction | ||
DROP TABLE IF EXISTS ParentCascade | ||
DROP TABLE IF EXISTS ParentSetNull | ||
DROP TABLE IF EXISTS ParentSetDefault | ||
DROP TABLE IF EXISTS Level3 | ||
DROP TABLE IF EXISTS Level2 | ||
DROP TABLE IF EXISTS Level1 | ||
DROP TABLE IF EXISTS [Child#Table] | ||
DROP TABLE IF EXISTS [Parent@Table] | ||
DROP TABLE IF EXISTS Child | ||
DROP TABLE IF EXISTS Parent | ||
DROP TABLE IF EXISTS GrandParent | ||
DROP TABLE IF EXISTS [dbo].[OrderItem] | ||
DROP TABLE IF EXISTS [order] | ||
DROP TABLE IF EXISTS UniqueChild; | ||
DROP TABLE IF EXISTS UniqueParent; | ||
GO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,248 @@ | ||
USE MASTER; | ||
GO | ||
|
||
CREATE TABLE [order] ( | ||
pkorder INT IDENTITY(25001000,1) PRIMARY KEY, | ||
screatedby sys.varchar(255) | ||
); | ||
GO | ||
|
||
CREATE TABLE orderitem ( | ||
pkorderitem INT IDENTITY(1,1) PRIMARY KEY, | ||
fkorder integer, | ||
screatedby sys.varchar(255) | ||
); | ||
GO | ||
|
||
ALTER TABLE [dbo].[OrderItem] ADD CONSTRAINT [FK_OrderItem_Order] FOREIGN KEY([fkOrder])REFERENCES [dbo].[Order] ([pkOrder]); | ||
GO | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
CREATE TRIGGER [trg_i_COD] ON [dbo].[OrderItem] | ||
AFTER INSERT | ||
AS | ||
BEGIN | ||
SET NOCOUNT ON ; | ||
DECLARE @HasMoreRow BIT , | ||
@pkOrderItem INT , | ||
@IsExpedit BIT , | ||
@IsShippingItem BIT , | ||
@IsPackage BIT , | ||
@IsAddOn BIT , | ||
@IsCrossSell BIT , | ||
@ExpeditePackageBitValue INT , | ||
@PackageRelationshipType INT, | ||
@ShippingRelationshipType INT , | ||
@AddOnRelationshipType INT , | ||
@CrossSellRelationshipType INT | ||
DECLARE @OrderItemList TABLE | ||
( | ||
pkOrderItem INT NOT NULL , | ||
IsUsed BIT DEFAULT ( 0 )/*REWRITTEN*/ , /**/ PRIMARY KEY ( pkOrderItem ) | ||
) | ||
INSERT INTO @OrderItemList | ||
( pkOrderItem ) | ||
SELECT pkOrderItem | ||
FROM inserted | ||
SELECT TOP 1 | ||
@pkOrderItem = pkOrderItem , | ||
@hasMoreRow = 1 | ||
FROM @OrderItemList | ||
WHERE IsUsed = 0 | ||
END | ||
GO | ||
|
||
INSERT INTO [dbo].[order] (sCreatedBy) VALUES('test'); | ||
GO | ||
~~ROW COUNT: 1~~ | ||
|
||
|
||
INSERT INTO [dbo].[orderItem] (fkOrder, sCreatedBy) VALUES(25001000, 'test'); | ||
GO | ||
~~ROW COUNT: 1~~ | ||
|
||
|
||
-- Create base tables | ||
CREATE TABLE GrandParent (ID INT PRIMARY KEY); | ||
GO | ||
|
||
CREATE TABLE Parent ( | ||
ID INT PRIMARY KEY, | ||
GrandParentID INT | ||
); | ||
GO | ||
|
||
ALTER TABLE [dbo].[Parent] ADD CONSTRAINT [FK_Parent_GrandParent] FOREIGN KEY ([GrandParentID]) REFERENCES [dbo].[GrandParent] ([ID]); | ||
GO | ||
|
||
CREATE TABLE Child ( | ||
ID INT PRIMARY KEY, | ||
ParentID INT | ||
); | ||
GO | ||
|
||
ALTER TABLE [dbo].[Child] ADD CONSTRAINT [FK_Child_Parent] FOREIGN KEY ([ParentID]) REFERENCES [dbo].[Parent] ([ID]); | ||
GO | ||
|
||
-- Create special character tables | ||
CREATE TABLE [Parent@Table] (ID INT PRIMARY KEY); | ||
GO | ||
|
||
CREATE TABLE [Child#Table] ( | ||
ID INT PRIMARY KEY, | ||
ParentID INT | ||
); | ||
GO | ||
|
||
ALTER TABLE [dbo].[Child#Table] ADD CONSTRAINT [FK_ChildTable_ParentTable] FOREIGN KEY ([ParentID]) REFERENCES [dbo].[Parent@Table] ([ID]); | ||
GO | ||
|
||
-- Insert some initial data | ||
INSERT INTO GrandParent VALUES (1), (2); | ||
GO | ||
~~ROW COUNT: 2~~ | ||
|
||
|
||
INSERT INTO Parent VALUES (1, 1), (2, 2); | ||
GO | ||
~~ROW COUNT: 2~~ | ||
|
||
|
||
INSERT INTO [Parent@Table] VALUES (1), (2); | ||
GO | ||
~~ROW COUNT: 2~~ | ||
|
||
|
||
-- Create multi-level tables for nesting tests | ||
CREATE TABLE Level1 (ID INT PRIMARY KEY); | ||
GO | ||
|
||
CREATE TABLE Level2 ( | ||
ID INT PRIMARY KEY, | ||
Level1ID INT | ||
); | ||
GO | ||
|
||
ALTER TABLE [dbo].[Level2] ADD CONSTRAINT [FK_Level2_Level1] FOREIGN KEY ([Level1ID]) REFERENCES [dbo].[Level1] ([ID]); | ||
GO | ||
|
||
CREATE TABLE Level3 ( | ||
ID INT PRIMARY KEY, | ||
Level2ID INT | ||
); | ||
GO | ||
|
||
ALTER TABLE [dbo].[Level3] ADD CONSTRAINT [FK_Level3_Level2] FOREIGN KEY ([Level2ID]) REFERENCES [dbo].[Level2] ([ID]); | ||
GO | ||
|
||
INSERT INTO Level1 VALUES (1), (2); | ||
GO | ||
~~ROW COUNT: 2~~ | ||
|
||
|
||
INSERT INTO Level2 VALUES (1, 1), (2, 2); | ||
GO | ||
~~ROW COUNT: 2~~ | ||
|
||
|
||
CREATE TABLE UniqueParent ( | ||
ID INT PRIMARY KEY, | ||
UniqueValue VARCHAR(50) UNIQUE | ||
); | ||
GO | ||
|
||
CREATE TABLE UniqueChild ( | ||
ID INT PRIMARY KEY, | ||
ParentID INT, | ||
UniqueChildValue VARCHAR(50) UNIQUE | ||
); | ||
GO | ||
|
||
ALTER TABLE UniqueChild ADD CONSTRAINT FK_UniqueChild_UniqueParent FOREIGN KEY (ParentID) REFERENCES UniqueParent(ID); | ||
GO | ||
|
||
INSERT INTO UniqueParent (ID, UniqueValue) VALUES (1, 'Parent1'), (2, 'Parent2'); | ||
GO | ||
~~ROW COUNT: 2~~ | ||
|
||
|
||
CREATE TABLE ParentNoAction (ParentID INT PRIMARY KEY, ParentName NVARCHAR(50)); | ||
GO | ||
|
||
CREATE TABLE ParentCascade (ParentID INT PRIMARY KEY, ParentName NVARCHAR(50)); | ||
GO | ||
|
||
CREATE TABLE ParentSetNull (ParentID INT PRIMARY KEY, ParentName NVARCHAR(50)); | ||
GO | ||
|
||
CREATE TABLE ParentSetDefault (ParentID INT PRIMARY KEY, ParentName NVARCHAR(50)); | ||
GO | ||
|
||
CREATE TABLE ChildNoAction (ChildID INT PRIMARY KEY, ParentID INT, ChildName NVARCHAR(50)); | ||
GO | ||
|
||
CREATE TABLE ChildCascade (ChildID INT PRIMARY KEY, ParentID INT, ChildName NVARCHAR(50)); | ||
GO | ||
|
||
CREATE TABLE ChildSetNull (ChildID INT PRIMARY KEY, ParentID INT, ChildName NVARCHAR(50)); | ||
GO | ||
|
||
CREATE TABLE ChildSetDefault (ChildID INT PRIMARY KEY, ParentID INT DEFAULT 0, ChildName NVARCHAR(50)); | ||
GO | ||
|
||
ALTER TABLE ChildNoAction ADD CONSTRAINT FK_ChildNoAction_Parent FOREIGN KEY (ParentID) REFERENCES ParentNoAction(ParentID); | ||
GO | ||
|
||
ALTER TABLE ChildCascade ADD CONSTRAINT FK_ChildCascade_Parent FOREIGN KEY (ParentID) REFERENCES ParentCascade(ParentID) ON DELETE CASCADE ON UPDATE CASCADE; | ||
GO | ||
|
||
ALTER TABLE ChildSetNull ADD CONSTRAINT FK_ChildSetNull_Parent FOREIGN KEY (ParentID) REFERENCES ParentSetNull(ParentID) ON DELETE SET NULL ON UPDATE SET NULL; | ||
GO | ||
|
||
ALTER TABLE ChildSetDefault ADD CONSTRAINT FK_ChildSetDefault_Parent FOREIGN KEY (ParentID) REFERENCES ParentSetDefault(ParentID) ON DELETE SET DEFAULT ON UPDATE SET DEFAULT; | ||
GO | ||
|
||
INSERT INTO ParentNoAction (ParentID, ParentName) VALUES (1, 'ParentNoAction1'), (2, 'ParentNoAction2'); | ||
GO | ||
~~ROW COUNT: 2~~ | ||
|
||
|
||
INSERT INTO ParentCascade (ParentID, ParentName) VALUES (1, 'ParentCascade1'), (2, 'ParentCascade2'); | ||
GO | ||
~~ROW COUNT: 2~~ | ||
|
||
|
||
INSERT INTO ParentSetNull (ParentID, ParentName) VALUES (1, 'ParentSetNull1'), (2, 'ParentSetNull2'); | ||
GO | ||
~~ROW COUNT: 2~~ | ||
|
||
|
||
INSERT INTO ParentSetDefault (ParentID, ParentName) VALUES (1, 'ParentSetDefault1'), (2, 'ParentSetDefault2'); | ||
GO | ||
~~ROW COUNT: 2~~ | ||
|
||
|
||
INSERT INTO ChildNoAction (ChildID, ParentID, ChildName) VALUES (1, 1, 'ChildNoAction1'), (2, 2, 'ChildNoAction2'); | ||
GO | ||
~~ROW COUNT: 2~~ | ||
|
||
|
||
INSERT INTO ChildCascade (ChildID, ParentID, ChildName) VALUES (1, 1, 'ChildCascade1'), (2, 2, 'ChildCascade2'); | ||
GO | ||
~~ROW COUNT: 2~~ | ||
|
||
|
||
INSERT INTO ChildSetNull (ChildID, ParentID, ChildName) VALUES (1, 1, 'ChildSetNull1'), (2, 2, 'ChildSetNull2'); | ||
GO | ||
~~ROW COUNT: 2~~ | ||
|
||
|
||
INSERT INTO ChildSetDefault (ChildID, ParentID, ChildName) VALUES (1, 1, 'ChildSetDefault1'), (2, 2, 'ChildSetDefault2'); | ||
GO | ||
~~ROW COUNT: 2~~ | ||
|
Oops, something went wrong.