ISJSON constraints should not be escaped.

Your BRACKET_ALL_THE_THINGS() function needs an EXCEPTION encoded so that JSON validation Constraints like this do not get rewritten:

ALTER TABLE [PHI].[Study] ADD CONSTRAINT [PHI_STUDY_ExtJSON_VALID] CHECK (isjson([ExtJSON])>0)

It appears that something inside SQL Source control is rewriting the above so that it breaks when executing, it writes the following BAD T-SQL instead:

ALTER TABLE [PHI].[Study] ADD CONSTRAINT [STUDY_ExtJSON_VALID] CHECK (([isjson]([ExtJSON])>(0)))

Look at that. the function isjson should NOT have [] around it, and the several extra pairs of parenthesis are pathological too.

Not EVERYTHING matching an [a-zA-Z]* regex inside a CHECK() expression can be assumed to be a literal field reference!

Probably you have some logic that tries to be smart about this, but the new JSON constraint functions in SQL 2016 are not one of the things you handle correctly.

Warren

Comments

  • Hello,

    We've opened a ticket for this issue so we can troubleshoot further. I'll be responding to you through that ticket immediatly after this forum response, thanks.
    Allen LeVan
    Red Gate Software
    US Product Support
  • To reproduce you will need SQL server 2016 CTP 3.3. Create this table, and commit it into SQL Source control:
    SET ANSI_NULLS ON
    GO
    
    SET QUOTED_IDENTIFIER ON
    GO
    
    CREATE TABLE [dbo].[Group](
    	[InternalGroupID] [BIGINT] IDENTITY(1,1) NOT NULL,
    	[InternalRoleID] [BIGINT] NOT NULL,
    	[GroupName] [NVARCHAR](64) NOT NULL,
    	[ExtJson] [NVARCHAR](MAX) NULL,
     CONSTRAINT [PK_Group] PRIMARY KEY CLUSTERED 
    ( [InternalGroupID] ASC ) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
    
    GO
    

    After that commit, go to another working copy (another user's machine with SQL Source control) and try to fetch the changes and apply them.

    After that, run this tsql to add the constraint which contains an expression in the form ( isjson([ExtJSON])> 0):
    ALTER TABLE [dbo].[Group]  WITH CHECK ADD  CONSTRAINT [GROUP_ExtJSON_VALID] CHECK  ( isjson([ExtJSON])> 0)
    GO
    
    ALTER TABLE [dbo].[Group] CHECK CONSTRAINT [GROUP_ExtJSON_VALID]
    GO
    
    

    Now commit that change on working copy on workstation 1. This commit should only contain the above extjson validation constraint and its check enable.

    Now go back to workstation 2 and try to update.

    Expectation:

    ALTER TABLE [dbo].[Group] WITH CHECK ADD CONSTRAINT [GROUP_ExtJSON_VALID] CHECK ( isjson([ExtJSON])> 0)
    GO

    Actual:

    ALTER TABLE [dbo].[Group] WITH CHECK ADD CONSTRAINT [GROUP_ExtJSON_VALID] CHECK ( [isjson]([ExtJSON])> 0)
    GO

    You can see an extra set of brackets.
  • You know what would be great is if you open a ticket you post a link to that ticket here. I know. I'm smokin' weed right? Talking crazy?
    Just an idea. Did some ticket get opened? Who knows.

    Anyone with questions can contact me directly at wpostma at company dot com, and I work at a company called ramsoft.
Sign In or Register to comment.