From: Stephen Bennett Date: Sun, 7 Nov 2010 21:51:40 +0000 (+0000) Subject: Understand Postgres enumerated types X-Git-Tag: 0.07003~38 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=12333562ee997c58552b40b5f5eef33ab2deda87;p=dbsrgits%2FDBIx-Class-Schema-Loader.git Understand Postgres enumerated types --- diff --git a/Changes b/Changes index ba5f445..1c12d4a 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,6 @@ Revision history for Perl extension DBIx::Class::Schema::Loader + - Added support for PostgreSQL enum types - Added table/column comment support for Oracle - Fix missing require (RT#62072) diff --git a/lib/DBIx/Class/Schema/Loader.pm b/lib/DBIx/Class/Schema/Loader.pm index 2f886ee..f4692ec 100644 --- a/lib/DBIx/Class/Schema/Loader.pm +++ b/lib/DBIx/Class/Schema/Loader.pm @@ -518,6 +518,8 @@ hobbs: Andrew Rodland domm: Thomas Klausner +spb: Stephen Bennett + ... and lots of other folks. If we forgot you, please write the current maintainer or RT. diff --git a/lib/DBIx/Class/Schema/Loader/DBI/Pg.pm b/lib/DBIx/Class/Schema/Loader/DBI/Pg.pm index 8e4d7c1..1b922f1 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/Pg.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/Pg.pm @@ -224,6 +224,18 @@ EOF elsif (lc($data_type) eq 'character') { $info->{data_type} = 'char'; } + else { + my ($typetype) = $self->schema->storage->dbh + ->selectrow_array(<{data_type} = 'enum'; + } + } + # process SERIAL columns if (ref($info->{default_value}) eq 'SCALAR' && ${ $info->{default_value} } =~ /\bnextval\(['"]([.\w]+)/i) { @@ -245,6 +257,28 @@ EOF return $result; } +sub _extra_column_info { + my ($self, $table, $col, $info, $dbi_info) = @_; + my %extra_info; + + # The following will extract a list of allowed values if this + # is an enumerated type. Otherwise, no results and we do nothing. + my $typevalues = $self->schema->storage->dbh + ->selectall_arrayref(<{data_type}); +SELECT e.enumlabel +FROM pg_catalog.pg_enum e +JOIN pg_catalog.pg_type t ON t.oid = e.enumtypid +WHERE t.typname = ? +EOF + + if (@$typevalues) { + $extra_info{extra}{list} = [ map { $_->[0] } @$typevalues ]; + } + + return \%extra_info; +} + + =head1 SEE ALSO L, L, diff --git a/t/12pg_common.t b/t/12pg_common.t index 3b7baa8..bdac57a 100644 --- a/t/12pg_common.t +++ b/t/12pg_common.t @@ -112,7 +112,17 @@ my $tester = dbixcsl_common_tests->new( # Blob Types bytea => { data_type => 'bytea' }, + + # Enum Types + pg_loader_test_enum => { data_type => 'enum', extra => { list => [ qw/foo bar baz/] }, size => 4 }, }, + pre_create => [ + q{ + CREATE TYPE pg_loader_test_enum AS ENUM ( + 'foo', 'bar', 'baz' + ) + }, + ], extra => { create => [ q{ @@ -145,6 +155,7 @@ my $tester = dbixcsl_common_tests->new( ], pre_drop_ddl => [ 'DROP SCHEMA dbicsl_test CASCADE', + 'DROP TYPE pg_loader_test_enum', ], drop => [ qw/ pg_loader_test1 pg_loader_test2 / ], count => 4, diff --git a/t/lib/dbixcsl_common_tests.pm b/t/lib/dbixcsl_common_tests.pm index 41bfdd3..7922a30 100644 --- a/t/lib/dbixcsl_common_tests.pm +++ b/t/lib/dbixcsl_common_tests.pm @@ -1585,6 +1585,8 @@ sub create { my $dbh = $self->dbconnect(1); + $dbh->do($_) for @{ $self->{pre_create} || [] }; + $dbh->do($_) foreach (@statements); $dbh->do($_) foreach (@{ $self->{data_type_tests}{ddl} || [] });