public function normalize($db_name, $mode = BasicModel::NORMALIZE_MODE_CHECK, $doCreate = false) { $adds = 0; $this->name = 'prodUpdate'; $chk = parent::normalize($db_name, $mode, $doCreate); if ($chk !== false) { $adds += $chk; } $this->connection = FannieDB::get($db_name); if ($this->connection->tableExists('prodUpdateArchive')) { $this->name = 'prodUpdateArchive'; $chk = parent::normalize($db_name, $mode, false); if ($chk !== false) { $adds += $chk; } } return $adds; }
/** Overrides (extends) the base function to check multiple tables that should all have identical or similar structure after doing a normal run of the base. */ public function normalize($db_name, $mode = BasicModel::NORMALIZE_MODE_CHECK, $doCreate = false) { $config = FannieConfig::factory(); $FANNIE_TRANS_DB = $config->get('TRANS_DB'); $FANNIE_ARCHIVE_DB = $config->get('ARCHIVE_DB'); $FANNIE_ARCHIVE_METHOD = $config->get('ARCHIVE_METHOD'); $trans_adds = 0; $log_adds = 0; //EL If this isn't initialized it is "dlog_15" on the 2nd, preview_only=false run $this->name = 'dtransactions'; // check self first $chk = parent::normalize($db_name, $mode, $doCreate); if ($chk !== false) { $trans_adds += $chk; } $this->columns['store_row_id']['increment'] = false; $this->columns['store_row_id']['primary_key'] = false; $this->columns['store_row_id']['index'] = false; $this->columns['pos_row_id']['index'] = false; $this->name = 'transarchive'; $chk = parent::normalize($db_name, $mode, $doCreate); if ($chk !== false) { $trans_adds += $chk; } $this->name = 'suspended'; $tmp1 = $this->columns['store_row_id']; $tmp2 = $this->columns['pos_row_id']; unset($this->columns['store_row_id']); unset($this->columns['pos_row_id']); $chk = parent::normalize($db_name, $mode, $doCreate); if ($chk !== false) { $trans_adds += $chk; } $this->columns['pos_row_id'] = $tmp2; $this->columns['store_row_id'] = $tmp1; $this->connection = FannieDB::get($FANNIE_ARCHIVE_DB); if ($FANNIE_ARCHIVE_METHOD == 'partitions') { $this->name = 'bigArchive'; $chk = parent::normalize($FANNIE_ARCHIVE_DB, $mode, $doCreate); if ($chk !== false) { $trans_adds += $chk; } } else { $pattern = '/^transArchive\\d\\d\\d\\d\\d\\d$/'; $tables = $this->connection->get_tables($FANNIE_ARCHIVE_DB); foreach ($tables as $t) { if (preg_match($pattern, $t)) { $this->name = $t; $chk = parent::normalize($FANNIE_ARCHIVE_DB, $mode, $doCreate); if ($chk !== False) { $trans_adds += $chk; } } } } // move on to dlog views. // dlog_15 is used for detection since it's the only // actual table. // In the model the datestamp field datetime is swapped out for tdate // and trans_num is tacked on the end $this->connection = FannieDB::get($FANNIE_TRANS_DB); $this->name = 'dlog_15'; unset($this->columns['datetime']); $tdate = array('tdate' => array('type' => 'datetime', 'index' => True)); $trans_num = array('trans_num' => array('type' => 'VARCHAR(25)')); $this->columns = $tdate + $this->columns + $trans_num; $chk = parent::normalize($db_name, $mode, $doCreate); if ($chk !== false) { $log_adds += $chk; } // rebuild views // use BasicModel::normalize in check mode to detect missing columns // the ALTER queries it suggests won't work but the return value is // still correct. If it returns > 0, the view needs to be rebuilt $this->name = 'dlog'; ob_start(); $chk = parent::normalize($db_name, BasicModel::NORMALIZE_MODE_CHECK); ob_end_clean(); if ($chk !== false && $chk > 0) { $log_adds += $chk; $this->normalizeLog('dlog', 'dtransactions', $mode); } $this->name = 'dlog_90_view'; ob_start(); $chk = parent::normalize($db_name, BasicModel::NORMALIZE_MODE_CHECK); ob_end_clean(); if ($chk !== false && $chk > 0) { $log_adds += $chk; $this->normalizeLog('dlog_90_view', 'transarchive', $mode); } $this->connection = FannieDB::get($FANNIE_ARCHIVE_DB); if ($FANNIE_ARCHIVE_METHOD == 'partitions') { $this->name = 'dlogBig'; ob_start(); $chk = parent::normalize($FANNIE_ARCHIVE_DB, BasicModel::NORMALIZE_MODE_CHECK); ob_end_clean(); if ($chk !== false && $chk > 0) { $log_adds += $chk; $this->normalizeLog('dlogBig', 'bigArchive', $mode); } } else { $pattern = '/^dlog\\d\\d\\d\\d\\d\\d$/'; $tables = $this->connection->get_tables($FANNIE_ARCHIVE_DB); foreach ($tables as $t) { if (preg_match($pattern, $t)) { $this->name = $t; ob_start(); $chk = parent::normalize($FANNIE_ARCHIVE_DB, BasicModel::NORMALIZE_MODE_CHECK); ob_end_clean(); if ($chk !== false && $chk > 0) { $log_adds += $chk; $this->normalizeLog($t, 'transArchive' . substr($t, 4), $mode); } } } } // EL: Need to restore $this-columns to original values. $this->connection = FannieDB::get($FANNIE_TRANS_DB); unset($this->columns['tdate']); unset($this->columns['trans_num']); $datetime = array('datetime' => array('type' => 'datetime', 'index' => true)); $this->columns = $datetime + $this->columns; $this->columns['store_row_id']['increment'] = true; $this->columns['store_row_id']['primary_key'] = true; $this->columns['store_row_id']['index'] = false; $this->columns['pos_row_id']['index'] = true; return $log_adds + $trans_adds; // normalize() }
/** A really, REALLY old version of this table might exist. If so, just delete it and start over with the new schema. */ public function normalize($db_name, $mode = BasicModel::NORMALIZE_MODE_CHECK, $doCreate = False) { $dbc = FannieDB::get($db_name); $this->connection = $dbc; if (!$dbc->table_exists($this->name)) { return parent::normalize($db_name, $mode, $doCreate); } $def = $dbc->table_definition($this->name); if (count($def) == 3 && isset($def['stamp']) && isset($def['id']) && isset($def['name'])) { echo "==========================================\n"; if ($mode == BasicModel::NORMALIZE_MODE_APPLY) { $dbc->query('DROP TABLE ' . $dbc->identifier_escape($this->name)); $success = $this->create(); echo "Recreating table " . $this->name . ": "; echo $success ? 'Succeeded' : 'Failed'; echo "\n"; echo "==========================================\n"; return $success; } else { echo $this->name . " is very old. It needs to be re-created\n"; echo "Any data in the current table will be lost\n"; echo "==========================================\n"; return count($this->columns); } } else { return parent::normalize($db_name, $mode, $doCreate); } }
/** Custom normalization: The original version of scaleItems contained a column named "class". "class" is not a valid PHP function name, so the model is unable to have a method corresponding to the column. This will rename the legacy "class" column to "reportingClass" if needed. Otherwise, it just calls BasicModel::normalize(). */ public function normalize($db_name, $mode = BasicModel::NORMALIZE_MODE_CHECK, $doCreate = False) { $this->connection = FannieDB::get($db_name); if (!$this->connection->table_exists($this->name)) { return parent::normalize($db_name, $mode, $doCreate); } $current_definition = $this->connection->tableDefinition($this->name); if (isset($current_definition['class']) && !isset($current_definition['reportingClass'])) { $alter = 'ALTER TABLE ' . $this->connection->identifier_escape($this->name) . ' CHANGE COLUMN ' . $this->connection->identifier_escape('class') . ' ' . $this->connection->identifier_escape('reportingClass') . ' ' . $this->getMeta($this->columns['reportingClass']['type']); printf("%s column class as reportingClass\n", $mode == BasicModel::NORMALIZE_MODE_CHECK ? "Need to rename" : "Renaming"); printf("\tSQL Details: %s\n", $alter); if ($mode == BasicModel::NORMALIZE_MODE_APPLY) { $renamed = $this->connection->query($alter); return 0; } else { return 1; } } else { return parent::normalize($db_name, $mode, $doCreate); } }