From 1c92e4c3e57a164f2a24315c4ad0ab146b649dd9 Mon Sep 17 00:00:00 2001 From: Gevik Babakhani Date: Thu, 6 Jun 2013 22:45:30 +0200 Subject: [PATCH 1/5] Force travis to create a postgresql test database --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8dff4a3..da09923 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,6 @@ env: before_script: - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'create database IF NOT EXISTS yiitest;'; fi" - - sh -c "if [ '$DB' = 'postgres' ]; then psql -U postgres -c 'drop database if exists yiitest;'; fi" - - sh -c "if [ '$DB' = 'postgres' ]; then psql -U postgres -c 'create database yiitest;'; fi" + - psql -U postgres -c 'drop database if exists yiitest;'; + - psql -U postgres -c 'create database yiitest;'; script: phpunit \ No newline at end of file From e45e061cc53c326cd3abc9fc8929e5f83bb31159 Mon Sep 17 00:00:00 2001 From: Gevik Babakhani Date: Thu, 6 Jun 2013 22:52:28 +0200 Subject: [PATCH 2/5] Removed conditional pgsql env --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index da09923..01abd50 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,6 @@ php: env: - DB=mysql - - DB=postgres before_script: - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'create database IF NOT EXISTS yiitest;'; fi" From 19793f7af056e6f3e5fec31ddf9dc252ce3d6b86 Mon Sep 17 00:00:00 2001 From: gevik Date: Sat, 8 Jun 2013 21:51:15 +0200 Subject: [PATCH 3/5] Removed unused columsn from find constraint sql. Fixed typo. Added extra schema check for when a foreign table is not in the same schema. Updated indentation to conform to other classes. --- framework/yii/db/pgsql/Schema.php | 53 +++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/framework/yii/db/pgsql/Schema.php b/framework/yii/db/pgsql/Schema.php index bec1803..7223a3b 100644 --- a/framework/yii/db/pgsql/Schema.php +++ b/framework/yii/db/pgsql/Schema.php @@ -73,7 +73,7 @@ class Schema extends \yii\db\Schema ); /** - * Creates a query builder for the MySQL database. + * Creates a query builder for the PostgreSQL database. * @return QueryBuilder query builder instance */ public function createQueryBuilder() @@ -89,13 +89,16 @@ class Schema extends \yii\db\Schema protected function resolveTableNames($table, $name) { $parts = explode('.', str_replace('"', '', $name)); - if (isset($parts[1])) { + if (isset($parts[1])) + { $table->schemaName = $parts[0]; $table->name = $parts[1]; - } else { + } else + { $table->name = $parts[0]; } - if ($table->schemaName === null) { + if ($table->schemaName === null) + { $table->schemaName = $this->defaultSchema; } } @@ -120,11 +123,10 @@ class Schema extends \yii\db\Schema { $table = new TableSchema(); $this->resolveTableNames($table, $name); - if ($this->findColumns($table)) { + if ($this->findColumns($table)) + { $this->findConstraints($table); return $table; - } else { - return null; } } @@ -143,14 +145,9 @@ class Schema extends \yii\db\Schema $sql = <<defaultSchema) + { + $foreign_table = $constraint['foreign_table_schema'] . '.' . $constraint['foreign_table_name']; + } else + { + $foreign_table = $constraint['foreign_table_name']; + } + $citem = array($foreign_table); foreach ($columns as $idx => $column) { $citem[] = array($fcolumns[$idx] => $column); } @@ -242,17 +246,21 @@ ORDER BY a.attnum; SQL; - $columns = $this->db->createCommand($sql)->queryAll(); - if (empty($columns)) { + try + { + $columns = $this->db->createCommand($sql)->queryAll(); + } catch (\Exception $e) + { return false; } - foreach ($columns as $column) { $column = $this->loadColumnSchema($column); $table->columns[$column->name] = $column; - if ($column->isPrimaryKey === true) { + if ($column->isPrimaryKey === true) + { $table->primaryKey[] = $column->name; - if ($table->sequenceName === null && preg_match("/nextval\('\w+'(::regclass)?\)/", $column->defaultValue) === 1) { + if ($table->sequenceName === null && preg_match("/nextval\('\w+'(::regclass)?\)/", $column->defaultValue) === 1) + { $table->sequenceName = preg_replace(array('/nextval/', '/::/', '/regclass/', '/\'\)/', '/\(\'/'), '', $column->defaultValue); } } @@ -281,12 +289,15 @@ SQL; $column->scale = $info['numeric_scale']; $column->size = $info['size']; - if (isset($this->typeMap[$column->dbType])) { + if (isset($this->typeMap[$column->dbType])) + { $column->type = $this->typeMap[$column->dbType]; - } else { + } else + { $column->type = self::TYPE_STRING; } $column->phpType = $this->getColumnPhpType($column); return $column; } -} + +} \ No newline at end of file From 6fd74f5c27cb37eaacb82b48e3847eaa63c6273d Mon Sep 17 00:00:00 2001 From: gevik Date: Sat, 8 Jun 2013 23:47:24 +0200 Subject: [PATCH 4/5] Updated code style. braces on the same line for control statements. --- framework/yii/db/pgsql/Schema.php | 36 ++++++++++++------------------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/framework/yii/db/pgsql/Schema.php b/framework/yii/db/pgsql/Schema.php index 7223a3b..66f0876 100644 --- a/framework/yii/db/pgsql/Schema.php +++ b/framework/yii/db/pgsql/Schema.php @@ -89,16 +89,13 @@ class Schema extends \yii\db\Schema protected function resolveTableNames($table, $name) { $parts = explode('.', str_replace('"', '', $name)); - if (isset($parts[1])) - { + if (isset($parts[1])) { $table->schemaName = $parts[0]; $table->name = $parts[1]; - } else - { + } else { $table->name = $parts[0]; } - if ($table->schemaName === null) - { + if ($table->schemaName === null) { $table->schemaName = $this->defaultSchema; } } @@ -123,8 +120,7 @@ class Schema extends \yii\db\Schema { $table = new TableSchema(); $this->resolveTableNames($table, $name); - if ($this->findColumns($table)) - { + if ($this->findColumns($table)) { $this->findConstraints($table); return $table; } @@ -166,11 +162,9 @@ SQL; foreach ($constraints as $constraint) { $columns = explode(',', $constraint['columns']); $fcolumns = explode(',', $constraint['foreign_columns']); - if ($constraint['foreign_table_schema'] !== $this->defaultSchema) - { + if ($constraint['foreign_table_schema'] !== $this->defaultSchema) { $foreign_table = $constraint['foreign_table_schema'] . '.' . $constraint['foreign_table_name']; - } else - { + } else { $foreign_table = $constraint['foreign_table_name']; } $citem = array($foreign_table); @@ -246,21 +240,17 @@ ORDER BY a.attnum; SQL; - try - { + try { $columns = $this->db->createCommand($sql)->queryAll(); - } catch (\Exception $e) - { + } catch (\Exception $e) { return false; } foreach ($columns as $column) { $column = $this->loadColumnSchema($column); $table->columns[$column->name] = $column; - if ($column->isPrimaryKey === true) - { + if ($column->isPrimaryKey === true) { $table->primaryKey[] = $column->name; - if ($table->sequenceName === null && preg_match("/nextval\('\w+'(::regclass)?\)/", $column->defaultValue) === 1) - { + if ($table->sequenceName === null && preg_match("/nextval\('\w+'(::regclass)?\)/", $column->defaultValue) === 1) { $table->sequenceName = preg_replace(array('/nextval/', '/::/', '/regclass/', '/\'\)/', '/\(\'/'), '', $column->defaultValue); } } @@ -289,11 +279,9 @@ SQL; $column->scale = $info['numeric_scale']; $column->size = $info['size']; - if (isset($this->typeMap[$column->dbType])) - { + if (isset($this->typeMap[$column->dbType])) { $column->type = $this->typeMap[$column->dbType]; - } else - { + } else { $column->type = self::TYPE_STRING; } $column->phpType = $this->getColumnPhpType($column); From ec9dab11fad43c869c19cd3119a2ce516d7ae886 Mon Sep 17 00:00:00 2001 From: gevik Date: Sun, 9 Jun 2013 00:04:37 +0200 Subject: [PATCH 5/5] [1] Redone missing code. [2] Added empty line at the end of file [3] Removed exception. --- framework/yii/db/pgsql/Schema.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/framework/yii/db/pgsql/Schema.php b/framework/yii/db/pgsql/Schema.php index 66f0876..8cfb535 100644 --- a/framework/yii/db/pgsql/Schema.php +++ b/framework/yii/db/pgsql/Schema.php @@ -123,6 +123,8 @@ class Schema extends \yii\db\Schema if ($this->findColumns($table)) { $this->findConstraints($table); return $table; + } else { + return null; } } @@ -240,11 +242,7 @@ ORDER BY a.attnum; SQL; - try { - $columns = $this->db->createCommand($sql)->queryAll(); - } catch (\Exception $e) { - return false; - } + $columns = $this->db->createCommand($sql)->queryAll(); foreach ($columns as $column) { $column = $this->loadColumnSchema($column); $table->columns[$column->name] = $column; @@ -288,4 +286,4 @@ SQL; return $column; } -} \ No newline at end of file +}