<!--- #10846: ensure pMed_Interaction_Report exists with Mark-as-reported documentation columns.
     Status: 0 = reported, 1 = cleared, 2 = communication created / awaiting report confirmation. --->
<cftry>
	<cfquery datasource="#Application.DataSrc#">
		CREATE TABLE IF NOT EXISTS #Request.prefix_db_agency#.pMed_Interaction_Report (
			Report_ID INT NOT NULL AUTO_INCREMENT,
			Patient_ID INT NOT NULL DEFAULT 0,
			Fingerprint VARCHAR(500) NOT NULL DEFAULT '',
			Comm_ID INT NULL DEFAULT NULL,
			Interaction_Summary TEXT NULL,
			Date_Create DATETIME NULL,
			Created_By INT NOT NULL DEFAULT 0,
			Status INT NOT NULL DEFAULT 0,
			Reported_Date DATE NULL,
			Reported_By INT NOT NULL DEFAULT 0,
			Reporting_Method VARCHAR(20) NULL,
			Report_Notes TEXT NULL,
			PRIMARY KEY (Report_ID),
			KEY idx_patient_status (Patient_ID, Status),
			KEY idx_fingerprint (Fingerprint(191))
		)
	</cfquery>
	<cfcatch type="any"></cfcatch>
</cftry>
<cfset _medIxCols = "Reported_Date,Reported_By,Reporting_Method,Report_Notes">
<cfloop list="#_medIxCols#" index="_medIxCol">
	<cftry>
		<cfquery name="chkMedIxCol" datasource="#Application.DataSrc#">
			SELECT COUNT(*) AS col_exists
			FROM INFORMATION_SCHEMA.COLUMNS
			WHERE TABLE_SCHEMA = <cfqueryparam value="#Request.prefix_db_agency#" cfsqltype="cf_sql_varchar">
			AND TABLE_NAME = 'pMed_Interaction_Report'
			AND COLUMN_NAME = <cfqueryparam value="#_medIxCol#" cfsqltype="cf_sql_varchar">
		</cfquery>
		<cfif Val(chkMedIxCol.col_exists) EQ 0>
			<cfquery datasource="#Application.DataSrc#">
				ALTER TABLE #Request.prefix_db_agency#.pMed_Interaction_Report
				ADD COLUMN
				<cfif _medIxCol EQ "Reported_Date">Reported_Date DATE NULL
				<cfelseif _medIxCol EQ "Reported_By">Reported_By INT NOT NULL DEFAULT 0
				<cfelseif _medIxCol EQ "Reporting_Method">Reporting_Method VARCHAR(20) NULL
				<cfelse>Report_Notes TEXT NULL
				</cfif>
			</cfquery>
		</cfif>
		<cfcatch type="any"></cfcatch>
	</cftry>
</cfloop>
