/** * Tests Postgresql::hasDefault for autoincrement fields * * @issue https://github.com/phalcon/phalcon-devtools/issues/853 * @author Serghei Iakovlev <*****@*****.**> * @since 2016-09-28 */ public function testHasAutoIncrementDefault() { $this->specify('The autoincrement column has default value', function () { $column = Column::__set_state(['_columnName' => 'id', '_schemaName' => null, '_type' => 14, '_typeReference' => -1, '_typeValues' => null, '_isNumeric' => true, '_size' => 0, '_scale' => 0, '_default' => "nextval('images_id_seq'::regclass)", '_unsigned' => false, '_notNull' => true, '_primary' => false, '_autoIncrement' => true, '_first' => true, '_after' => null, '_bindType' => 1]); expect($column->hasDefault())->false(); expect($column->isAutoIncrement())->true(); }); }
/** * Return a better Phalcon type (Check, Select, Password, ...) according to previous Phalcon type * and size of the column * @param string $type * @param \Phalcon\Db\Column $column * @return string */ private function _getType($type, $column) { $size = $column->getSize(); $columntype = $type; if ($type == "Numeric") { if ($size == 1) { $columntype = "Check"; } else { if ($size == 11) { $columntype = "Select"; } } } else { if ($type == "Text") { if ($size > 255) { $columntype = "Textarea"; } if (preg_match("#(pass|pwd)#i", $column->getName())) { $columntype = "Password"; } } } return $columntype; }
/** * Modify table columns. * * @param string $tableName Table name. * @param string $schemaName Schema name. * @param array $definition Table Definition. * * @return int * @throws \Exception */ protected function _modifyColumns($tableName, $schemaName, $definition) { if (empty($definition['columns'])) { throw new \Exception('Table must have at least one column'); } $counter = 0; $db = $this->getDI()->get('db'); $fields = []; foreach ($definition['columns'] as $tableColumn) { if (!is_object($tableColumn)) { throw new \Exception('Wrong column definition, it must be a object'); } $fields[$tableColumn->getName()] = $tableColumn; } $localFields = []; $description = $db->describeColumns($tableName, $schemaName); foreach ($description as $field) { $localFields[$field->getName()] = $field; } foreach ($fields as $fieldName => $tableColumn) { if (!isset($localFields[$fieldName])) { $db->addColumn($tableName, $tableColumn->getSchemaName(), $tableColumn); $counter++; } else { $currentField = $localFields[$fieldName]; $changed = false; // Hack boolean type. if ($currentField->getType() == Column::TYPE_INTEGER && $currentField->getSize() === "1") { $currentField = new Column($fieldName, ['type' => Column::TYPE_BOOLEAN, 'size' => 0, 'notNull' => $currentField->isNotNull(), 'first' => $currentField->isFirst()]); } if ($currentField->getType() != $tableColumn->getType()) { $changed = true; } if ($currentField->getSize() != $tableColumn->getSize()) { $changed = true; } if ($tableColumn->isNotNull() != $currentField->isNotNull()) { $changed = true; } if ($changed == true) { $db->modifyColumn($tableName, $tableColumn->getSchemaName(), $tableColumn); $counter++; } } } foreach (array_keys($localFields) as $fieldName) { if (!isset($fields[$fieldName])) { $db->dropColumn($tableName, null, $fieldName); $counter++; } } return $counter; }
/** * Creates new cassandra column * @param string $name * @param array $definition */ public function __construct($name, $definition) { parent::__construct($name, $definition); if (!empty($definition['autoIncrement'])) { throw new CException('Auto increment columns not supported'); } if (!empty($definition['partitionKey'])) { $this->_partitionKey = true; } if (!empty($definition['clusteringKey'])) { $this->_clusteringKey = true; } if (!empty($definition['reversed'])) { $this->_reversed = true; } if (!empty($definition['static'])) { $this->_static = true; } if (!empty($definition['frozen'])) { $this->_frozen = true; } }
/** * Tests Postgresql::describeColumns for Postgresql autoincrement column * * @issue https://github.com/phalcon/phalcon-devtools/issues/853 * @author Serghei Iakovlev <*****@*****.**> * @since 2016-09-28 */ public function testDescribeAutoIncrementColumns() { $this->specify('The table columns array contains incorrect initialized objects', function () { $columns = [Column::__set_state(['_columnName' => 'id', '_schemaName' => null, '_type' => 14, '_typeReference' => -1, '_typeValues' => null, '_isNumeric' => true, '_size' => 0, '_scale' => 0, '_default' => "nextval('images_id_seq'::regclass)", '_unsigned' => false, '_notNull' => true, '_primary' => false, '_autoIncrement' => true, '_first' => true, '_after' => null, '_bindType' => 1]), Column::__set_state(['_columnName' => 'base64', '_schemaName' => null, '_type' => 6, '_typeReference' => -1, '_typeValues' => null, '_isNumeric' => false, '_size' => null, '_scale' => 0, '_default' => null, '_unsigned' => false, '_notNull' => false, '_primary' => false, '_autoIncrement' => false, '_first' => false, '_after' => 'id', '_bindType' => 2])]; expect($this->connection->describeColumns('images', null))->equals($columns); expect($this->connection->describeColumns('images', TEST_DB_POSTGRESQL_SCHEMA))->equals($columns); }); }