Skip to content

Commit

Permalink
Merge pull request #12263 from sergeyklay/db/column/has_default
Browse files Browse the repository at this point in the history
Fixed Column::hasDefault
  • Loading branch information
sergeyklay authored Sep 28, 2016
2 parents 9e03b34 + 52b1927 commit 04054d6
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
- Fixed `Phalcon\Session\Adapter\Libmemcached::read`, `Phalcon\Session\Adapter\Libmemcached::destroy`, `Phalcon\Session\Adapter\Memcache::read`, `Phalcon\Session\Adapter\Memcache::destroy`, `Phalcon\Session\Adapter\Redis::read` and `Phalcon\Session\Adapter\Redis::destroy` in accordance with the `session_set_save_handler` API [#12206](https://github.com/phalcon/cphalcon/pull/12206)
- Fixed `Phalcon\Validation::getValue()` to use filters when having entity
- Fixed `Phalcon\Db\Dialect\Mysql::describeReferences()` and `Phalcon\Db\Dialect\Postgresql::describeReferences()` to return proper sql

- Fixed `Phalcon\Db\Column::hasDefault` to return `false` for autoincrement columns [phalcon/phalcon-devtools#853](https://github.com/phalcon/phalcon-devtools/issues/853)

# [3.0.1](https://github.com/phalcon/cphalcon/releases/tag/v3.0.1) (2016-08-24)
- Fixed `Phalcon\Cache\Backend\Redis::flush` in order to flush cache correctly
Expand Down
6 changes: 5 additions & 1 deletion phalcon/db/column.zep
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2016 Phalcon Team (https://phalconphp.com) |
| Copyright (c) 2011-2016 Phalcon Team (https://phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
Expand Down Expand Up @@ -569,6 +569,10 @@ class Column implements ColumnInterface
*/
public function hasDefault() -> boolean
{
if this->isAutoIncrement() {
return false;
}

return this->_default !== null;
}
}
28 changes: 25 additions & 3 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,21 @@ First you need to re-generate base classes for all suites:
$ vendor/bin/codecept build
```

A MySQL database is also bundled in this suite.
The SQL file is required for several tests. You can create a database as follows:
A MySQL/PostgreSQL databases is also bundled in this suite. You can create a databases as follows:

*MySQL*
```sh
$ echo 'create database phalcon_test charset=utf8mb4 collate=utf8mb4_unicode_ci;' | mysql -u root
$ mysql -uroot phalcon_test < tests/_data/schemas/mysql/mysql.dump.sql
```

**Note:** For these tests we use the user `root` without a password.
*PostgreSQL*
```
psql -c 'create database phalcon_test;' -U postgres
psql -U postgres phalcon_test -q -f tests/_data/schemas/postgresql/phalcon_test.sql
```

**Note:** For these MySQL-related we use the user `root` without a password.
You may need to change this in `codeception.yml` file.

Obviously, Beanstalk-tests use Beanstalk, Memcached-tests use Memcached, etc.
Expand All @@ -62,6 +69,14 @@ We use the following settings of these services:
* DB Name: `phalcon_test`
* Charset: `utf8`

**PostgreSQL**

* Host: `127.0.0.1`
* Port: `5432`
* Username: `postgres`
* Password: `''` (empty string)
* DB Name: `phalcon_test`

**Mongo**

* Host: `127.0.0.1`
Expand Down Expand Up @@ -95,6 +110,13 @@ export TEST_DB_MYSQL_PASSWD=""
export TEST_DB_MYSQL_NAME="phalcon_test"
export TEST_DB_MYSQL_CHARSET="utf8"

# Postgresql
export TEST_DB_POSTGRESQL_HOST="127.0.0.1"
export TEST_DB_POSTGRESQL_PORT="5432"
export TEST_DB_POSTGRESQL_USER="postgres"
export TEST_DB_POSTGRESQL_PASSWD=""
export TEST_DB_POSTGRESQL_NAME="phalcon_test"

# Mongo
export TEST_DB_MONGO_HOST="127.0.0.1"
export TEST_DB_MONGO_PORT="27017"
Expand Down
12 changes: 12 additions & 0 deletions tests/_data/schemas/postgresql/phalcon_test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ CREATE TABLE parts (

ALTER TABLE public.parts OWNER TO postgres;

--
-- Name: images; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--

CREATE TABLE images (
id BIGSERIAL,
base64 TEXT
);


ALTER TABLE public.images OWNER TO postgres;

--
-- Name: personas; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
Expand Down
70 changes: 65 additions & 5 deletions tests/unit/Db/Adapter/Pdo/PostgresqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace Phalcon\Test\Unit\Db\Adapter\Pdo;

use Phalcon\Db\Adapter\Pdo\Postgresql;
use Phalcon\Db\Column;
use Phalcon\Db\Reference;
use Phalcon\Test\Module\UnitTest;
use Phalcon\Db\Adapter\Pdo\Postgresql;

/**
* \Phalcon\Test\Unit\Db\Adapter\Pdo\Postgresql
* \Phalcon\Test\Unit\Db\Adapter\Pdo\PostgresqlTest
* Tests the \Phalcon\Db\Adapter\Pdo\Postgresql component
*
* @copyright (c) 2011-2016 Phalcon Team
Expand Down Expand Up @@ -54,14 +55,73 @@ public function _before()
public function testDescribeReferencesColumnsCount()
{
$this->specify(
'Postgresql::describeReferences return wrong number of columns in Phalcon\Db\Reference',
'The table references list contains wrong number of columns',
function() {
$references = $this->connection->describeReferences("robots_parts", TEST_DB_POSTGRESQL_NAME);
$references = $this->connection->describeReferences('robots_parts', TEST_DB_POSTGRESQL_SCHEMA);
expect($references)->count(2);

/** @var Reference $reference */
foreach($references as $reference) {
expect($reference->getColumns())->count(1);
}
}
);
}
}

/**
* Tests Postgresql::describeColumns for Postgresql autoincrement column
*
* @issue https://github.com/phalcon/phalcon-devtools/issues/853
* @author Serghei Iakovlev <[email protected]>
* @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);
}
);
}
}
63 changes: 63 additions & 0 deletions tests/unit/Db/Column/PostgresqlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Phalcon\Test\Unit\Db\Column;

use Phalcon\Db\Column;
use Phalcon\Test\Module\UnitTest;

/**
* \Phalcon\Test\Unit\Db\Column\PostgresqlTest
* Tests the \Phalcon\Db\Column component
*
* @copyright (c) 2011-2016 Phalcon Team
* @link https://www.phalconphp.com
* @author Andres Gutierrez <[email protected]>
* @author Serghei Iakovlev <[email protected]>
* @package Phalcon\Test\Unit\Db\Column
*
* The contents of this file are subject to the New BSD License that is
* bundled with this package in the file docs/LICENSE.txt
*
* If you did not receive a copy of the license and are unable to obtain it
* through the world-wide-web, please send an email to [email protected]
* so that we can send you a copy immediately.
*/
class PostgresqlTest extends UnitTest
{
/**
* Tests Postgresql::hasDefault for autoincrement fields
*
* @issue https://github.com/phalcon/phalcon-devtools/issues/853
* @author Serghei Iakovlev <[email protected]>
* @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();
}
);
}
}
20 changes: 10 additions & 10 deletions unit-tests/DbDescribeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,6 @@ public function testDbMysql()

public function testDbPostgresql()
{

require 'unit-tests/config.db.php';
if (empty($configPostgresql)) {
$this->markTestSkipped("Skipped");
Expand All @@ -660,15 +659,16 @@ public function testDbPostgresql()

//List tables
$expectedTables = array (
0 => 'customers',
1 => 'parts',
2 => 'personas',
3 => 'personnes',
4 => 'prueba',
5 => 'robots',
6 => 'robots_parts',
7 => 'subscriptores',
8 => 'tipo_documento',
'customers',
'images',
'parts',
'personas',
'personnes',
'prueba',
'robots',
'robots_parts',
'subscriptores',
'tipo_documento',
);

$tables = $connection->listTables();
Expand Down

0 comments on commit 04054d6

Please sign in to comment.