From 4fd3dd36e2125ea468e645196fb5bf034518c614 Mon Sep 17 00:00:00 2001
From: Prasad <prasad@vtiger.com>
Date: Sat, 8 Jan 2022 23:06:15 +0530
Subject: [PATCH] Fixes #1672: Restored support for table alter=true rule
 handling

---
 libraries/adodb/adodb-datadict.inc.php  | 39 +++++++++++++++++++++++--
 libraries/adodb/adodb-xmlschema.inc.php | 23 ++++++++++-----
 2 files changed, 52 insertions(+), 10 deletions(-)

diff --git a/libraries/adodb/adodb-datadict.inc.php b/libraries/adodb/adodb-datadict.inc.php
index dbed95da7..bb4028698 100644
--- a/libraries/adodb/adodb-datadict.inc.php
+++ b/libraries/adodb/adodb-datadict.inc.php
@@ -994,7 +994,7 @@ class ADODB_DataDict {
 	This function changes/adds new fields to your table. You don't
 	have to know if the col is new or not. It will check on its own.
 	*/
-	function changeTableSQL($tablename, $flds, $tableoptions = false, $dropOldFlds=false)
+	function changeTableSQL($tablename, $flds, $tableoptions = false, $dropOldFlds=false, $forceAlter = false) /* Vtiger Fix for constraint impl - forceAlter */
 	{
 	global $ADODB_FETCH_MODE;
 
@@ -1011,7 +1011,7 @@ class ADODB_DataDict {
 		if (isset($savem)) $this->connection->setFetchMode($savem);
 		$ADODB_FETCH_MODE = $save;
 
-		if ( empty($cols)) {
+		if ( $forceAlter == false && empty($cols)) { /* Vtiger Fix for constraint impl */
 			return $this->createTableSQL($tablename, $flds, $tableoptions);
 		}
 
@@ -1084,6 +1084,39 @@ class ADODB_DataDict {
 			    if ( !isset($lines[$id]) )
 					$sql[] = $alter . $this->dropCol . ' ' . $v->name;
 		}
-		return $sql;
+
+		/* Vtiger Fix for constraint impl -- start */
+		if($forceAlter == false) {
+			return $sql;
+		}
+
+		$sqlarray = array();
+		$alter .= implode(",\n", $sql);
+		if (sizeof($pkey)>0) {
+			$alter .= ",\n PRIMARY KEY (";
+			$alter .= implode(", ",$pkey).")";
+		}
+		
+		if (isset($tableoptions['CONSTRAINTS'])) {
+			$alter .= "\n".$tableoptions['CONSTRAINTS'];
+		}
+
+		if (isset($tableoptions[$this->upperName.'_CONSTRAINTS'])) {
+			$alter .= "\n".$tableoptions[$this->upperName.'_CONSTRAINTS'];
+		}
+
+		if (isset($tableoptions[$this->upperName])) {
+			$alter .= $tableoptions[$this->upperName];
+		}
+		$sqlarray[] = $alter;
+		
+		$taboptions = $this->_Options($tableoptions);
+		$tsql = $this->_Triggers($this->TableName($tablename),$taboptions);
+		foreach($tsql as $s) {
+			$sqlarray[] = $s;
+		}
+		/* Vtiger Fix for constraint impl -- end */
+
+		return $sqlarray;
 	}
 } // class
diff --git a/libraries/adodb/adodb-xmlschema.inc.php b/libraries/adodb/adodb-xmlschema.inc.php
index 58e3affd6..824dcb960 100644
--- a/libraries/adodb/adodb-xmlschema.inc.php
+++ b/libraries/adodb/adodb-xmlschema.inc.php
@@ -248,6 +248,7 @@ class dbTable extends dbObject {
 	* @access private
 	*/
 	var $drop_field = array();
+	var $alter; /* Vtiger Fix for constraint impl */
 
 	/**
 	* Iniitializes a new table object.
@@ -258,6 +259,10 @@ class dbTable extends dbObject {
 	function __construct( &$parent, $attributes = NULL ) {
 		$this->parent = $parent;
 		$this->name = $this->prefix($attributes['NAME']);
+		/* Vtiger Fix for constraint impl */
+		if(isset($attributes['ALTER'])) {
+			$this->alter = $attributes['ALTER'];
+		}
 	}
 
 	/**
@@ -332,12 +337,12 @@ class dbTable extends dbObject {
 				if( isset( $this->current_field ) ) {
 					$this->addFieldOpt( $this->current_field, $this->currentElement, $cdata );
 				} else {
-					$this->addTableOpt( $cdata );
+					$this->addTableOpt( $cdata, 'CONSTRAINTS' ); /* Vtiger Fix for constraint impl */
 				}
 				break;
 			// Table option
 			case 'OPT':
-				$this->addTableOpt( $cdata );
+				$this->addTableOpt( $cdata, 'mysql' ); /* Vtiger Fix for constraint impl */
 				break;
 			default:
 
@@ -471,9 +476,13 @@ class dbTable extends dbObject {
 	* @param string $opt Table option
 	* @return array Options
 	*/
-	function addTableOpt( $opt ) {
-		if(isset($this->currentPlatform)) {
-			$this->opts[$this->parent->db->databaseType] = $opt;
+	function addTableOpt( $opt, $key = NULL) { /* Vtiger Fix for constraint impl */
+		if ($key) {
+			$this->opts[$key] = $opt;
+		} else {
+			if(isset($this->currentPlatform)) {
+				$this->opts[$this->parent->db->databaseType] = $opt;
+			}
 		}
 		return $this->opts;
 	}
@@ -556,7 +565,7 @@ class dbTable extends dbObject {
 			}
 		}
 
-		if( empty( $legacy_fields ) ) {
+		if( empty( $legacy_fields ) && !isset($this->alter)) { /* Vtiger Fix for constraint impl */
 			// Create the new table
 			$sql[] = $xmls->dict->CreateTableSQL( $this->name, $fldarray, $this->opts );
 			logMsg( end( $sql ), 'Generated CreateTableSQL' );
@@ -567,7 +576,7 @@ class dbTable extends dbObject {
 				// Use ChangeTableSQL
 				case 'ALTER':
 					logMsg( 'Generated ChangeTableSQL (ALTERing table)' );
-					$sql[] = $xmls->dict->ChangeTableSQL( $this->name, $fldarray, $this->opts );
+					$sql[] = $xmls->dict->ChangeTableSQL( $this->name, $fldarray, $this->opts, false, $this->alter ); /* Vtiger Fix for constraint impl */
 					break;
 				case 'REPLACE':
 					logMsg( 'Doing upgrade REPLACE (testing)' );
-- 
GitLab