/** * Perform upgrade for a table, by comparing one table to an old version * of the same table * * @param Table $old_table */ public function upgrade(Table $old_table) { if ($old_table->getVersion() != $this->getVersion() - 1) { throw new Exception('Cannot upgrade from ' . get_class($old_table) . ' version ' . $old_table->getVersion() . ', must be version ' . ($this->getVersion() - 1)); } $old_columns = $old_table->getColumns(); $new_columns = $this->getColumns(); $added_columns = \array_diff_key($new_columns, $old_columns); $altered_columns = Tools::array_diff_recursive($old_columns, $new_columns); $dropped_columns = \array_keys(array_diff_key($old_columns, $new_columns)); $sqls = array(); foreach ($added_columns as $column => $details) { $sqls[] = $this->_getAddColumnSQL($column, $details); } if (count($sqls)) { foreach ($sqls as $sqlStmt) { $statement = Statement::getPreparedStatement($sqlStmt); $res = $statement->performQuery('alter'); } } $this->_migrateData($old_table); $sqls = array(); foreach ($altered_columns as $column => $details) { if (in_array($column, $dropped_columns)) { continue; } $sqls[] = $this->_getAlterColumnSQL($old_columns[$column], $new_columns[$column]); } foreach ($dropped_columns as $details) { $sqls[] = $this->_getDropColumnSQL($details); } if (count($sqls)) { foreach ($sqls as $sqlStmt) { $statement = Statement::getPreparedStatement($sqlStmt); $res = $statement->performQuery('alter'); } } }
/** * Perform upgrade for a table, by comparing one table to an old version * of the same table * * @param Table $old_table */ public function upgrade(Table $old_table) { $old_columns = $old_table->getColumns(); $new_columns = $this->getColumns(); $added_columns = \array_diff_key($new_columns, $old_columns); $altered_columns = Tools::array_diff_recursive($old_columns, $new_columns); $dropped_columns = \array_keys(array_diff_key($old_columns, $new_columns)); $sqls = array(); foreach ($added_columns as $details) { $sqls[] = $this->_getAddColumnSQL($details); } if (count($sqls)) { foreach ($sqls as $sqlStmt) { if ($sqlStmt) { Statement::getPreparedStatement($sqlStmt)->performQuery(); } } } $this->_migrateData($old_table); $sqls = array(); foreach ($altered_columns as $column => $details) { if (in_array($column, $dropped_columns)) { continue; } $sqls[] = $this->_getAlterColumnSQL($new_columns[$column]); $altersql = $this->_getAlterColumnDefaultSQL($new_columns[$column]); if ($altersql) { $sqls[] = $altersql; } } foreach ($dropped_columns as $details) { $sqls[] = $this->_getDropColumnSQL($details); } if (count($sqls)) { foreach ($sqls as $sqlStmt) { if ($sqlStmt) { Statement::getPreparedStatement($sqlStmt)->performQuery(); } } } }