Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #142 from jmoberley/issue_96

GH-96 Postgres adapter get_column_info query fix
  • Loading branch information...
commit 9dbd41adb9e72f96a82461a9b6a2ad2959c80446 2 parents 04cb7f3 + 7211e98
@greut greut authored
View
35 lib/adapters/PgsqlAdapter.php
@@ -37,21 +37,30 @@ public function limit($sql, $offset, $limit)
public function query_column_info($table)
{
$sql = <<<SQL
-SELECT a.attname AS field, a.attlen,
-REPLACE(pg_catalog.format_type(a.atttypid, a.atttypmod),'character varying','varchar') AS type,
-a.attnotnull AS not_nullable,
-i.indisprimary as pk,
-REGEXP_REPLACE(REGEXP_REPLACE(REGEXP_REPLACE(s.column_default,'::[a-z_ ]+',''),'\'$',''),'^\'','') AS default
-FROM pg_catalog.pg_attribute a
-LEFT JOIN pg_catalog.pg_class c ON(a.attrelid=c.oid)
-LEFT JOIN pg_catalog.pg_index i ON(c.oid=i.indrelid AND a.attnum=any(i.indkey))
-LEFT JOIN information_schema.columns s ON(s.table_name=? AND a.attname=s.column_name)
-WHERE a.attrelid = (select c.oid from pg_catalog.pg_class c inner join pg_catalog.pg_namespace n on(n.oid=c.relnamespace) where c.relname=? and pg_catalog.pg_table_is_visible(c.oid))
-AND a.attnum > 0
-AND NOT a.attisdropped
+SELECT
+ a.attname AS field,
+ a.attlen,
+ REPLACE(pg_catalog.format_type(a.atttypid, a.atttypmod), 'character varying', 'varchar') AS type,
+ a.attnotnull AS not_nullable,
+ (SELECT 't'
+ FROM pg_index
+ WHERE c.oid = pg_index.indrelid
+ AND a.attnum = ANY (pg_index.indkey)
+ AND pg_index.indisprimary = 't'
+ ) IS NOT NULL AS pk,
+ REGEXP_REPLACE(REGEXP_REPLACE(REGEXP_REPLACE((SELECT pg_attrdef.adsrc
+ FROM pg_attrdef
+ WHERE c.oid = pg_attrdef.adrelid
+ AND pg_attrdef.adnum=a.attnum
+ ),'::[a-z_ ]+',''),'''$',''),'^''','') AS default
+FROM pg_attribute a, pg_class c, pg_type t
+WHERE c.relname = ?
+ AND a.attnum > 0
+ AND a.attrelid = c.oid
+ AND a.atttypid = t.oid
ORDER BY a.attnum
SQL;
- $values = array($table,$table);
+ $values = array($table);
return $this->query($sql,$values);
}
View
9 test/ColumnTest.php
@@ -3,13 +3,18 @@
use ActiveRecord\Column;
use ActiveRecord\DateTime;
+use ActiveRecord\DatabaseException;
class ColumnTest extends SnakeCase_PHPUnit_Framework_TestCase
{
public function set_up()
{
$this->column = new Column();
- $this->conn = ActiveRecord\ConnectionManager::get_connection(ActiveRecord\Config::instance()->get_default_connection());
+ try {
+ $this->conn = ActiveRecord\ConnectionManager::get_connection(ActiveRecord\Config::instance()->get_default_connection());
+ } catch (DatabaseException $e) {
+ $this->mark_test_skipped('failed to connect using default connection. '.$e->getMessage());
+ }
}
public function assert_mapped_type($type, $raw_type)
@@ -114,4 +119,4 @@ public function test_empty_and_null_datetime_strings_should_return_null()
$this->assert_equals(null,$column->cast('',$this->conn));
}
}
-?>
+?>
View
7 test/DateTimeTest.php
@@ -1,6 +1,7 @@
<?php
include 'helpers/config.php';
use ActiveRecord\DateTime as DateTime;
+use ActiveRecord\DatabaseException;
class DateTimeTest extends SnakeCase_PHPUnit_Framework_TestCase
{
@@ -17,7 +18,11 @@ public function tear_down()
private function assert_dirtifies($method /*, method params, ...*/)
{
- $model = new Author();
+ try {
+ $model = new Author();
+ } catch (DatabaseException $e) {
+ $this->mark_test_skipped('failed to connect. '.$e->getMessage());
+ }
$datetime = new DateTime();
$datetime->attribute_of($model,'some_date');
View
10 test/ExpressionsTest.php
@@ -3,6 +3,8 @@
require '../lib/Expressions.php';
use ActiveRecord\Expressions;
+use ActiveRecord\ConnectionManager;
+use ActiveRecord\DatabaseException;
class ExpressionsTest extends SnakeCase_PHPUnit_Framework_TestCase
{
@@ -131,7 +133,11 @@ public function test_substitute_escapes_quotes()
public function test_substitute_escape_quotes_with_connections_escape_method()
{
- $conn = ActiveRecord\ConnectionManager::get_connection();
+ try {
+ $conn = ConnectionManager::get_connection();
+ } catch (DatabaseException $e) {
+ $this->mark_test_skipped('failed to connect. '.$e->getMessage());
+ }
$a = new Expressions(null,'name=?',"Tito's Guild");
$a->set_connection($conn);
$escaped = $conn->escape("Tito's Guild");
@@ -193,4 +199,4 @@ public function test_hash_with_array()
$this->assert_equals('id=? AND name IN(?,?)',$a->to_s());
}
}
-?>
+?>
View
5 test/PgsqlAdapterTest.php
@@ -36,5 +36,10 @@ public function test_set_charset()
$conn = ActiveRecord\Connection::instance($connection_string . '?charset=utf8');
$this->assert_equals("SET NAMES 'utf8'",$conn->last_query);
}
+
+ public function test_gh96_columns_not_duplicated_by_index()
+ {
+ $this->assert_equals(3,$this->conn->query_column_info("user_newsletters")->rowCount());
+ }
}
?>
View
7 test/helpers/DatabaseLoader.php
@@ -30,10 +30,11 @@ public function reset_table_data()
$this->load_fixture_data($table);
}
+ $after_fixtures = $this->db->protocol.'-after-fixtures';
try {
- $this->exec_sql_script("{$this->db->protocol}-after-fixtures");
+ $this->exec_sql_script($after_fixtures);
} catch (Exception $e) {
- // ignore
+ // pass
}
}
@@ -126,4 +127,4 @@ public function quote_name($name)
return $this->db->quote_name($name);
}
}
-?>
+?>
View
6 test/helpers/DatabaseTest.php
@@ -24,7 +24,11 @@ public function set_up($connection_name=null)
}
$this->connection_name = $connection_name;
- $this->conn = ActiveRecord\ConnectionManager::get_connection($connection_name);
+ try {
+ $this->conn = ActiveRecord\ConnectionManager::get_connection($connection_name);
+ } catch (ActiveRecord\DatabaseException $e) {
+ $this->mark_test_skipped($connection_name . ' failed to connect. '.$e->getMessage());
+ }
$GLOBALS['ACTIVERECORD_LOG'] = false;
View
9 test/sql/pgsql-after-fixtures.sql
@@ -1,12 +1,15 @@
SELECT setval('authors_author_id_seq', max(author_id)) FROM authors;
SELECT setval('books_book_id_seq', max(book_id)) FROM books;
-SELECT setval('venues_Id_seq', max(Id)) FROM venues;
+SELECT setval('venues_id_seq', max(id)) FROM venues;
SELECT setval('events_id_seq', max(id)) FROM events;
SELECT setval('hosts_id_seq', max(id)) FROM hosts;
SELECT setval('employees_id_seq', max(id)) FROM employees;
SELECT setval('positions_id_seq', max(id)) FROM positions;
SELECT setval('"rm-bldg_rm-id_seq"', max("rm-id")) FROM "rm-bldg";
-SELECT setval('awesome_people_id_seq', max(people_id)) FROM awesome_people;
+SELECT setval('awesome_people_id_seq', max(id)) FROM awesome_people;
SELECT setval('amenities_amenity_id_seq', max(amenity_id)) FROM amenities;
SELECT setval('property_property_id_seq', max(property_id)) FROM property;
-SELECT setval('property_amenities_id_seq', max(id)) FROM property_amenities;
+SELECT setval('property_amenities_id_seq', max(id)) FROM property_amenities;
+SELECT setval('users_id_seq', max(id)) FROM users;
+SELECT setval('newsletters_id_seq', max(id)) FROM newsletters;
+SELECT setval('user_newsletters_id_seq', max(id)) FROM user_newsletters;
View
31 test/sql/pgsql.sql
@@ -13,7 +13,7 @@ CREATE TABLE authors(
CREATE TABLE books(
book_id SERIAL PRIMARY KEY,
- Author_Id INT,
+ author_id INT,
secondary_author_id INT,
name VARCHAR(50),
numeric_test VARCHAR(10) DEFAULT '0',
@@ -21,7 +21,7 @@ CREATE TABLE books(
);
CREATE TABLE venues (
- Id SERIAL PRIMARY KEY,
+ id SERIAL PRIMARY KEY,
name varchar(50),
city varchar(60),
state char(2),
@@ -71,16 +71,33 @@ CREATE TABLE awesome_people(
);
CREATE TABLE amenities(
- amenity_id serial primary key,
- type varchar(40) NOT NULL
+ amenity_id serial primary key,
+ type varchar(40) NOT NULL
);
CREATE TABLE property(
- property_id serial primary key
+ property_id serial primary key
);
CREATE TABLE property_amenities(
+ id serial primary key,
+ amenity_id int not null,
+ property_id int not null
+);
+
+CREATE TABLE users(
+ id serial primary key
+);
+
+CREATE TABLE newsletters(
+ id serial primary key
+);
+
+CREATE TABLE user_newsletters(
id serial primary key,
- amenity_id int not null,
- property_id int not null
+ user_id int not null,
+ newsletter_id int not null
);
+
+-- reproduces issue GH-96 for testing
+CREATE INDEX user_newsletters_id_and_user_id_idx ON user_newsletters USING btree(id, user_id);
Please sign in to comment.
Something went wrong with that request. Please try again.