Skip to content
Snippets Groups Projects
Commit c28241be authored by lobo's avatar lobo
Browse files

fix CRM-12002

parent 5af3d504
No related branches found
No related tags found
No related merge requests found
......@@ -196,7 +196,7 @@ AND TABLE_NAME LIKE 'log_civicrm_%'
$line = preg_grep("/^ `$col` /", $create);
$line = substr(array_pop($line), 0, -1);
// CRM-11179
$line = self::fixTimeStampSQL($line);
$line = self::fixTimeStampAndNotNullSQL($line);
CRM_Core_DAO::executeQuery("ALTER TABLE `{$this->db}`.log_$table ADD $line");
}
......@@ -207,10 +207,11 @@ AND TABLE_NAME LIKE 'log_civicrm_%'
}
}
function fixTimeStampSQL($query) {
function fixTimeStampAndNotNullSQL($query) {
$query = str_ireplace("TIMESTAMP NOT NULL", "TIMESTAMP NULL", $query);
$query = str_ireplace("DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", '', $query);
$query = str_ireplace("DEFAULT CURRENT_TIMESTAMP", '', $query);
$query = str_ireplace("NOT NULL", '', $query);
return $query;
}
......@@ -287,25 +288,27 @@ AND TABLE_NAME LIKE 'log_civicrm_%'
$query = $dao->Create_Table;
// rewrite the queries into CREATE TABLE queries for log tables:
// - prepend the name with log_
// - drop AUTO_INCREMENT columns
// - drop non-column rows of the query (keys, constraints, etc.)
// - set the ENGINE to ARCHIVE
// - add log-specific columns (at the end of the table)
$cols = <<<COLS
log_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
log_conn_id INTEGER,
log_user_id INTEGER,
log_action ENUM('Initialization', 'Insert', 'Update', 'Delete')
COLS;
// - prepend the name with log_
// - drop AUTO_INCREMENT columns
// - drop non-column rows of the query (keys, constraints, etc.)
// - set the ENGINE to ARCHIVE
// - add log-specific columns (at the end of the table)
$query = preg_replace("/^CREATE TABLE `$table`/i", "CREATE TABLE `{$this->db}`.log_$table", $query);
$query = preg_replace("/ AUTO_INCREMENT/i", '', $query);
$query = preg_replace("/^ [^`].*$/m", '', $query);
$query = preg_replace("/^\) ENGINE=[^ ]+ /im", ') ENGINE=ARCHIVE ', $query);
// log_civicrm_contact.modified_date for example would always be copied from civicrm_contact.modified_date,
// so there's no need for a default timestamp and therefore we remove such default timestamps by using fixTimeStampSQL() method,
// before we inject special log columns with default timestamps
$query = self::fixTimeStampSQL($query);
// so there's no need for a default timestamp and therefore we remove such default timestamps
// also eliminate the NOT NULL constraint, since we always copy and schema can change down the road)
$query = self::fixTimeStampAndNotNullSQL($query);
$query = preg_replace("/^\) /m", "$cols\n) ", $query);
CRM_Core_DAO::executeQuery($query);
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment