Skip Menu |

This queue is for tickets about the DBIx-Class-Schema-Loader CPAN distribution.

Report information
The Basics
Id: 39478
Status: resolved
Priority: 0/
Queue: DBIx-Class-Schema-Loader

People
Owner: Nobody in particular
Requestors: wizard.wcs [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: Pg multi-schema database support patch
$ENV{PG_AUTOLOAD_MULI_SCHEMA} = "1"; before loading change to multi-schema logic if anyone know what else may be improved - your welcome to email me
Subject: pg_autoload_multi.tgz
Download pg_autoload_multi.tgz
application/x-gzip 1.9k

Message body not shown because it is not plain text.

Subject: Re: [rt.cpan.org #39478] Pg multi-schema database support patch
Date: Sat, 27 Sep 2008 17:39:30 +0100
To: Voronin Viktor via RT <bug-DBIx-Class-Schema-Loader [...] rt.cpan.org>
From: Matt S Trout <mst [...] shadowcat.co.uk>
On Sun, Sep 21, 2008 at 12:27:14AM -0400, Voronin Viktor via RT wrote: Show quoted text
> Sun Sep 21 00:27:00 2008: Request 39478 was acted upon. > Transaction: Ticket created by rwizard > Queue: DBIx-Class-Schema-Loader > Subject: Pg multi-schema database support patch > Broken in: (no value) > Severity: Wishlist > Owner: Nobody > Requestors: wizard.wcs@gmail.com > Status: new > Ticket <URL: http://rt.cpan.org/Ticket/Display.html?id=39478 > > > > $ENV{PG_AUTOLOAD_MULI_SCHEMA} = "1"; > before loading > change to multi-schema logic > if anyone know what else may be improved - your welcome to email me
An environment variable is completely the wrong way to do this - it should be a loader option instead. Could you please adjust your patch and tests? -- Matt S Trout Need help with your Catalyst or DBIx::Class project? Technical Director http://www.shadowcat.co.uk/catalyst/ Shadowcat Systems Ltd. Want a managed development or deployment platform? http://chainsawblues.vox.com/ http://www.shadowcat.co.uk/servers/
From: wizard.wcs [...] gmail.com
Show quoted text
> An environment variable is completely the wrong way to do this - it
should Show quoted text
> be a loader option instead. Could you please adjust your patch and
tests? Show quoted text
>
In fact, it looks like this: sub _setup { my $self = shift; [..cut..] $self->{multi_db_schema} ||= undef; $self->{multi_db_schema} = 1 if ( $ENV{PG_AUTOLOAD_MULI_SCHEMA} ); } this is the loader option? I use environment variables, because I do not understand how to provide the variables in the Schema-Loader from Catalyst-View-DBIx. I'm having trouble writing tests, but I will try to write them in few days. In addition, I attach the version with small fixes. P.S. Sorry for the bad language.
package DBIx::Class::Schema::Loader::DBI::Pg; use strict; use warnings; use base 'DBIx::Class::Schema::Loader::DBI'; use Carp::Clan qw/^DBIx::Class/; use Class::C3; our $VERSION = '0.04004'; =head1 NAME DBIx::Class::Schema::Loader::DBI::Pg - DBIx::Class::Schema::Loader::DBI PostgreSQL Implementation. =head1 SYNOPSIS package My::Schema; use base qw/DBIx::Class::Schema::Loader/; __PACKAGE__->loader_options( debug => 1 ); 1; =head1 DESCRIPTION See L<DBIx::Class::Schema::Loader::Base>. =cut sub _setup { my $self = shift; $self->next::method(@_); $self->{db_schema} ||= 'public'; $self->{multi_db_schema} ||= undef; $self->{multi_db_schema} = 1 if ( $ENV{PG_AUTOLOAD_MULI_SCHEMA} ); } sub _tables_list { my $self = shift; my @tables; my $dbh = $self->schema->storage->dbh; if ( !defined $self->{multi_db_schema} ) { @tables = $dbh->tables( undef, $self->db_schema, '%', '%' ); } else { my @t = $dbh->tables( undef, undef, '%', '%' ); foreach (@t) { next if ( $_ =~ /^pg_catalog/ ); next if ( $_ =~ /^information/ ); push @tables, $_; } } s/\Q$self->{_quoter}\E//g for @tables; if ( !defined $self->{multi_db_schema} ) { s/^.*\Q$self->{_namesep}\E// for @tables; } return @tables; } sub _table_columns { my ( $self, $table ) = @_; my $dbh = $self->schema->storage->dbh; if ( !defined $self->{multi_db_schema} ) { if ( $self->{db_schema} ) { $table = $self->{db_schema} . $self->{_namesep} . $table; } } my $sth = $dbh->prepare( $self->schema->storage->sql_maker->select( $table, undef, \'1 = 0' ) ); $sth->execute; my $retval = \@{ $sth->{NAME_lc} }; $sth->finish; $retval; } sub _table_pk_info { my ( $self, $table ) = @_; my @primary; my $dbh = $self->schema->storage->dbh; if ( !defined $self->{multi_db_schema} ) { @primary = map { lc } $dbh->primary_key( '', $self->db_schema, $table ); } else { my ( $s, $t ) = split( /\./, $table ); @primary = map { lc } $dbh->primary_key( '', $s, $t ); } s/\Q$self->{_quoter}\E//g for @primary; return \@primary; } sub _table_fk_info { my ( $self, $table ) = @_; my ( $s, $t ) = split( /\./, $table ); my $dbh = $self->schema->storage->dbh; my $sth; if ( !defined $self->{multi_db_schema} ) { my $sth = $dbh->foreign_key_info( '', '', '', '', $self->db_schema, $table ); } else { $sth = $dbh->foreign_key_info( '', '', '', '', $s, $t ); } return [] if !$sth; my %rels; my $i = 1; while ( my $raw_rel = $sth->fetchrow_arrayref ) { my $uk_tbl; if ( !defined $self->{multi_db_schema} ) { $uk_tbl = $raw_rel->[2]; } else { $uk_tbl = $raw_rel->[1] . '.' . $raw_rel->[2]; } my $uk_col = lc $raw_rel->[3]; my $fk_col = lc $raw_rel->[7]; my $relid = ( $raw_rel->[11] || ( "__dcsld__" . $i++ ) ); $uk_tbl =~ s/\Q$self->{_quoter}\E//g; $uk_col =~ s/\Q$self->{_quoter}\E//g; $fk_col =~ s/\Q$self->{_quoter}\E//g; $relid =~ s/\Q$self->{_quoter}\E//g; $rels{$relid}->{tbl} = $uk_tbl; $rels{$relid}->{cols}->{$uk_col} = $fk_col; } $sth->finish; my @rels; foreach my $relid ( keys %rels ) { push( @rels, { remote_columns => [ keys %{ $rels{$relid}->{cols} } ], local_columns => [ values %{ $rels{$relid}->{cols} } ], remote_table => $rels{$relid}->{tbl}, } ); } return \@rels; } sub _table_uniq_info { my ( $self, $table ) = @_; # Use the default support if available return $self->next::method($table) if $DBD::Pg::VERSION >= 1.50; my @uniqs; my $dbh = $self->schema->storage->dbh; # Most of the SQL here is mostly based on # Rose::DB::Object::Metadata::Auto::Pg, after some prodding from # John Siracusa to use his superior SQL code :) my $attr_sth = $self->{_cache}->{pg_attr_sth} ||= $dbh->prepare( q{SELECT attname FROM pg_catalog.pg_attribute WHERE attrelid = ? AND attnum = ?} ); my $uniq_sth = $self->{_cache}->{pg_uniq_sth} ||= $dbh->prepare( q{SELECT x.indrelid, i.relname, x.indkey FROM pg_catalog.pg_index x JOIN pg_catalog.pg_class c ON c.oid = x.indrelid JOIN pg_catalog.pg_class i ON i.oid = x.indexrelid JOIN pg_catalog.pg_constraint con ON con.conname = i.relname LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE x.indisunique = 't' AND c.relkind = 'r' AND i.relkind = 'i' AND con.contype = 'u' AND n.nspname = ? AND c.relname = ?} ); if ( !defined $self->{multi_db_schema} ) { $uniq_sth->execute( $self->db_schema, $table ); } else { my ( $s, $t ) = split( /\./, $table ); $uniq_sth->execute( $s, $t ); } while ( my $row = $uniq_sth->fetchrow_arrayref ) { my ( $tableid, $indexname, $col_nums ) = @$row; $col_nums =~ s/^\s+//; my @col_nums = split( /\s+/, $col_nums ); my @col_names; foreach (@col_nums) { $attr_sth->execute( $tableid, $_ ); my $name_aref = $attr_sth->fetchrow_arrayref; push( @col_names, $name_aref->[0] ) if $name_aref; } if ( !@col_names ) { warn "Failed to parse UNIQUE constraint $indexname on $table"; } else { push( @uniqs, [ $indexname => \@col_names ] ); } } return \@uniqs; } =head1 SEE ALSO L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>, L<DBIx::Class::Schema::Loader::DBI> =cut 1;
Subject: Re: [rt.cpan.org #39478] Pg multi-schema database support patch
Date: Sun, 28 Sep 2008 14:19:53 +0100
To: Voronin Viktor via RT <bug-DBIx-Class-Schema-Loader [...] rt.cpan.org>
From: Matt S Trout <mst [...] shadowcat.co.uk>
On Sat, Sep 27, 2008 at 01:21:57PM -0400, Voronin Viktor via RT wrote: Show quoted text
> I use environment variables, because I do not understand how to provide > the variables in the Schema-Loader from Catalyst-View-DBIx.
The fact you don't understand how to use it via some other tool is not a reason to make your patch dirty. It's a reason to get on the mailing lists and ask for help. Do that please :) -- Matt S Trout Need help with your Catalyst or DBIx::Class project? Technical Director http://www.shadowcat.co.uk/catalyst/ Shadowcat Systems Ltd. Want a managed development or deployment platform? http://chainsawblues.vox.com/ http://www.shadowcat.co.uk/servers/
From: wizard.wcs [...] gmail.com
Вск. Сен. 28 09:20:14 2008, mst@shadowcat.co.uk писал: Show quoted text
>Could you please adjust your patch and tests?
In the attachments are two versions of the patch: dbix-loader.patch - patches DBIx/Class/Schema/Loader/DBI.pm and DBIx/Class/Schema/Loader/DBI/Pg.pm adding logic to function in DBIx::Class::Schema::Loader::DBI and DBIx::Class::Schema::Loader::DBI::Pg this version in my humble opinion will be easier to maintain in the future dbix-loader-override.patch - only patches DBIx/Class/Schema/Loader/DBI/Pg.pm overriding the functions of DBIx::Class::Schema::Loader::DBI in DBIx::Class::Schema::Loader::DBI::Pg and tests made on the basis of dbixcsl_common_tests. Thank you for your patience with me and my patch :)

Message body not shown because it is not plain text.

From: wizard.wcs [...] gmail.com
In addition to the previous: switch to multi_shema logic controlled by option "pg_multi_db_schema"
Added in git master: - multi db_schema support with cross-schema rels (RT#39478) - added moniker_parts option for name clashes in multi db_schema setups
Added in git master: - multi db_schema support with cross-schema rels (RT#39478) - added moniker_parts option for name clashes in multi db_schema setups
Added full multi schema support for all supported DBs in 0.07011.