From b7587d2d5543819130eb97c58d0e40df8f00c529 Mon Sep 17 00:00:00 2001 From: AD7six Date: Thu, 6 Nov 2008 13:22:57 +0000 Subject: [PATCH] fixes #5696 incorrect timestamp definition generated by Posgres schema git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7832 3807eeeb-6ff5-0310-8944-8be069107fe0 --- .../model/datasources/dbo/dbo_postgres.php | 12 ++++- .../datasources/dbo/dbo_postgres.test.php | 46 ++++++++++++++++--- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/cake/libs/model/datasources/dbo/dbo_postgres.php b/cake/libs/model/datasources/dbo/dbo_postgres.php index fe82968ad..196e49a22 100644 --- a/cake/libs/model/datasources/dbo/dbo_postgres.php +++ b/cake/libs/model/datasources/dbo/dbo_postgres.php @@ -628,7 +628,13 @@ class DboPostgres extends DboSource { } $out = preg_replace('/integer\([0-9]+\)/', 'integer', parent::buildColumn($column)); $out = str_replace('integer serial', 'serial', $out); - + if (strpos($out, 'timestamp DEFAULT')) { + if (isset($column['null']) && $column['null']) { + $out = str_replace('DEFAULT NULL', '', $out); + } else { + $out = str_replace('DEFAULT NOT NULL', '', $out); + } + } if (strpos($out, 'DEFAULT DEFAULT')) { if (isset($column['null']) && $column['null']) { $out = str_replace('DEFAULT DEFAULT', 'DEFAULT NULL', $out); @@ -649,7 +655,9 @@ class DboPostgres extends DboSource { */ function buildIndex($indexes, $table = null) { $join = array(); - + if (!is_array($indexes)) { + return array(); + } foreach ($indexes as $name => $value) { if ($name == 'PRIMARY') { $out = 'PRIMARY KEY (' . $this->name($value['column']) . ')'; diff --git a/cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php b/cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php index 70f7afd39..ee0775422 100644 --- a/cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php @@ -187,9 +187,7 @@ class DboPostgresTest extends CakeTestCase { */ function skip() { $this->_initDb(); - $this->skipif( - $this->db->config['driver'] != 'postgres', 'PostgreSQL connection not available' - ); + $this->skipif($this->db->config['driver'] != 'postgres', 'PostgreSQL connection not available'); } /** * Set up test suite database connection @@ -353,7 +351,7 @@ class DboPostgresTest extends CakeTestCase { $this->assertEqual($db2->lastInsertId($table), 2); } /** - * Tests that table lists and descriptions are scoped to the proper Postgres schema + * Tests that table lists and descriptions are scoped to the proper Postgres schema * * @access public * @return void @@ -369,7 +367,7 @@ class DboPostgresTest extends CakeTestCase { array_merge($db1->config, array('driver' => 'postgres', 'schema' => '_scope_test')) ); $db2->cacheSources = false; - + $db2->query('DROP SCHEMA _scope_test'); } /** @@ -454,6 +452,42 @@ class DboPostgresTest extends CakeTestCase { $result = $this->db->createSchema($schema); $this->assertNoPattern('/^CREATE INDEX(.+);,$/', $result); } -} +/** + * testCakeSchema method + * + * Test that schema generated postgresql queries are valid. ref #5696 + * Check that the create statement for a schema generated table is the same as the original sql + * + * @return void + * @access public + */ + function testCakeSchema() { + $db1 =& ConnectionManager::getDataSource('test_suite'); + $db1->cacheSources = false; + $db1->reconnect(array('persistent' => false)); + $db1->query('CREATE TABLE ' . $db1->fullTableName('datatypes') . ' ( + id serial NOT NULL, + "varchar" character varying(40) NOT NULL, + "timestamp" timestamp without time zone, + date date, + CONSTRAINT test_suite_data_types_pkey PRIMARY KEY (id) + )'); + $model =& ClassRegistry::init('datatypes'); + $schema = new CakeSchema(array('connection' => 'test_suite')); + $result = $schema->read(array('connection' => 'test_suite')); + $schema->tables = $result['tables']['missing']; + $result = $db1->createSchema($schema, 'datatypes'); + $this->assertNoPattern('/timestamp DEFAULT/', $result); + $this->assertPattern('/timestamp\s*,/', $result); + $db1->query('DROP TABLE ' . $db1->fullTableName('datatypes')); + $db1->query($result); + $result2 = $schema->read(array('connection' => 'test_suite')); + $schema->tables = $result2['tables']['missing']; + $result2 = $db1->createSchema($schema, 'datatypes'); + $this->assertEqual($result, $result2); + + $db1->query('DROP TABLE ' . $db1->fullTableName('datatypes')); + } +} ?> \ No newline at end of file