<cfif form.Type EQ "Patch">
	<cfinclude template="patch_12.5.14.cfm">
	<cfinclude template="patch_12.5.15.cfm">
</cfif>

<!--- Remove references to non-existing appbuilder fileUploadIDs and adding a foreign key constraint to prevent it from happening.--->
<cfquery name="AppbuilderDataTables" datasource="#application.config.DSN#">				
	SELECT		TableName1
	FROM		Application
	WHERE		AppTypeID = 31
	GROUP BY	TableName1
</cfquery>
<cfloop query="AppbuilderDataTables" >
	<cfquery datasource="#application.config.DSN#">
		IF NOT EXISTS(SELECT * FROM sys.objects WHERE type_desc LIKE '%CONSTRAINT%' AND OBJECT_NAME(OBJECT_ID) = 'FK_#TableName1#_FileUploadID')
		BEGIN
			<!--- First remove any FileUploadID that doesn't exist in the FileUploadTable --->			
			UPDATE		AB
			SET			AB.FileUploadID = NULL
			FROM		#TableName1# AB
							LEFT JOIN AppbuilderFileUpload ABF on ABF.FileUploadID = AB.FileUploadID
			WHERE		ABF.FileName IS NULL
						AND AB.FileUploadID IS NOT NULL
						
			<!--- Add the foreign key constraint --->
			ALTER TABLE #TableName1#
			ADD CONSTRAINT FK_#TableName1#_FileUploadID
			FOREIGN KEY (FileUploadID)
			REFERENCES AppbuilderFileUpload(FileUploadID)
		END
	</cfquery>
</cfloop>

<!--- Bug 6503: Incorrect values in EmployeeFields --->
<cfquery datasource="#application.config.DSN#">				
	UPDATE EmployeeFields
	SET FieldTable = 1
	WHERE FieldID = 28
	
	DELETE FROM EmployeeFields
	WHERE FieldID = 35
</cfquery>
