From: Dagfinn Ilmari Mannsåker Date: Fri, 6 Oct 2017 14:47:03 +0000 (+0100) Subject: Support identity columns in PostgreSQL v10 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=013d8a3d7f3bf2e7b59a4655250160f66fc56846;p=dbsrgits%2FDBIx-Class-Schema-Loader.git Support identity columns in PostgreSQL v10 They are specified with GENERATED {ALWAYS | BY DEFAULT} AS IDENTITY, and don't show up with a defined default value in ->column_info, so we must check pg_attribute manually. --- diff --git a/Changes b/Changes index fbec7b0..7edbb9a 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,7 @@ Revision history for Perl extension DBIx::Class::Schema::Loader + - Support identity columns in PostgreSQL v10 + 0.07048_01 - 2018-02-23 - Convert from Module::Install to ExtUtils::MakeMaker + Distar (GH#17) diff --git a/lib/DBIx/Class/Schema/Loader/DBI/Pg.pm b/lib/DBIx/Class/Schema/Loader/DBI/Pg.pm index 78e8a9f..aaa2c92 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/Pg.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/Pg.pm @@ -329,6 +329,20 @@ EOF } } + # Identity columns can't have default values + if ($self->dbh->{pg_server_version} >= 100000 && !exists $info->{default_value}) { + my $identity = $self->schema->storage->dbh->selectrow_array(<<'EOF', {}, $table->sql_name, $col); +SELECT attidentity +FROM pg_catalog.pg_attribute +WHERE attrelid = ?::regclass +AND attname = ? +EOF + if ($identity) { + $info->{is_auto_increment} = 1; + $info->{extra}{generated_as_identity} = { a => 'always', d => 'by_default' }->{$identity}; + } + } + # detect 0/1 for booleans and rewrite if ($data_type =~ /^bool/i && exists $info->{default_value}) { if ($info->{default_value} eq '0') { diff --git a/t/10_03pg_common.t b/t/10_03pg_common.t index 2fe5fc9..5c03b68 100644 --- a/t/10_03pg_common.t +++ b/t/10_03pg_common.t @@ -514,6 +514,37 @@ dbixcsl_common_tests->new( 'unique indexes are dumped correctly'; }, }, + cond_extra => [ + { + desc => 'identity columns', + skip => 'requires PostgreSQL 10', + cond => sub { $_[0]->{pg_server_version} >= 100000 }, + create => [ + q{ + create table pg_loader_test13 ( + always integer generated always as identity, + by_default integer generated by default as identity + ) + }, + ], + drop => [ qw(pg_loader_test13) ], + run => sub { + my ($schema, $monikers, $classes) = @_; + + my $rsrc13 = $schema->source($monikers->{pg_loader_test13}); + for my $col (qw(by_default always)) { + my $info = $rsrc13->column_info($col); + (my $when = uc $col) =~ tr/_/ /; + + ok $info->{is_auto_increment}, + "is_auto_increment for GENERATED $when AS IDENTITY"; + + is $info->{extra}{generated_as_identity}, $col, + "generated_as_identity for GENERATED $when AS IDENTITY"; + } + }, + }, + ], )->run_tests(); END {