warns about C::M::DBIC::Schema upgrading / regenerating
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / Pg.pm
1 package DBIx::Class::Schema::Loader::DBI::Pg;
2
3 use strict;
4 use warnings;
5 use base 'DBIx::Class::Schema::Loader::DBI';
6 use Class::C3;
7
8 =head1 NAME
9
10 DBIx::Class::Schema::Loader::DBI::Pg - DBIx::Class::Schema::Loader::DBI
11 PostgreSQL Implementation.
12
13 =head1 SYNOPSIS
14
15   package My::Schema;
16   use base qw/DBIx::Class::Schema::Loader/;
17
18   __PACKAGE__->loader_options(
19     relationships => 1,
20   );
21
22   1;
23
24 =head1 DESCRIPTION
25
26 See L<DBIx::Class::Schema::Loader::Base>.
27
28 =cut
29
30 sub _setup {
31     my $self = shift;
32
33     $self->next::method(@_);
34     $self->{db_schema} ||= 'public';
35 }
36
37 sub _table_uniq_info {
38     my ($self, $table) = @_;
39
40     my @uniqs;
41     my $dbh = $self->schema->storage->dbh;
42
43     # Most of the SQL here is mostly based on
44     #   Rose::DB::Object::Metadata::Auto::Pg, after some prodding from
45     #   John Siracusa to use his superior SQL code :)
46
47     my $attr_sth = $self->{_cache}->{pg_attr_sth} ||= $dbh->prepare(
48         q{SELECT attname FROM pg_catalog.pg_attribute
49         WHERE attrelid = ? AND attnum = ?}
50     );
51
52     my $uniq_sth = $self->{_cache}->{pg_uniq_sth} ||= $dbh->prepare(
53         q{SELECT x.indrelid, i.relname, x.indkey
54         FROM
55           pg_catalog.pg_index x
56           JOIN pg_catalog.pg_class c ON c.oid = x.indrelid
57           JOIN pg_catalog.pg_class i ON i.oid = x.indexrelid
58           JOIN pg_catalog.pg_constraint con ON con.conname = i.relname
59           LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
60         WHERE
61           x.indisunique = 't' AND
62           c.relkind     = 'r' AND
63           i.relkind     = 'i' AND
64           con.contype   = 'u' AND
65           n.nspname     = ? AND
66           c.relname     = ?}
67     );
68
69     $uniq_sth->execute($self->db_schema, $table);
70     while(my $row = $uniq_sth->fetchrow_arrayref) {
71         my ($tableid, $indexname, $col_nums) = @$row;
72         $col_nums =~ s/^\s+//;
73         my @col_nums = split(/\s+/, $col_nums);
74         my @col_names;
75
76         foreach (@col_nums) {
77             $attr_sth->execute($tableid, $_);
78             my $name_aref = $attr_sth->fetchrow_arrayref;
79             push(@col_names, $name_aref->[0]) if $name_aref;
80         }
81
82         if(!@col_names) {
83             warn "Failed to parse UNIQUE constraint $indexname on $table";
84         }
85         else {
86             push(@uniqs, [ $indexname => \@col_names ]);
87         }
88     }
89
90     return \@uniqs;
91 }
92
93 =head1 SEE ALSO
94
95 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
96 L<DBIx::Class::Schema::Loader::DBI>
97
98 =cut
99
100 1;