Re: ILMARI, DID YOU SEE THIS? Patch for DBIx::Class::Schema::Loader to qualify relations if db_schema was passed

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: ILMARI, DID YOU SEE THIS? Patch for DBIx::Class::Schema::Loader to qualify relations if db_schema was passed

Matt S Trout
On Mon, Jun 30, 2008 at 08:12:02PM -0700, Dan Dascalescu wrote:
> Please find at http://dandascalescu.com/bugs/catalyst/DBIx-Class-Schema-Loader_patch-qualify_with_db_schema.txt
> a patch that qualifies table names, indexes and sequence names with
> the schema name if db_schema was specified. For
> sequences, I only handled the PostgreSQL syntax,
> nextval('table_id_seq'::regclass). castaway and mst also prodded me to
> add a Contributors section.
>
> Hope this patch gets us one step closer to multi-schema support,
> Dan

> Index: lib/DBIx/Class/Schema/Loader.pm
> ===================================================================
> --- lib/DBIx/Class/Schema/Loader.pm (revision 4507)
> +++ lib/DBIx/Class/Schema/Loader.pm (working copy)
> @@ -38,7 +38,7 @@
>    # -or-
>    my $schema1 = "My::Schema"; $schema1->connection(as above);
>  
> -=head1 DESCRIPTION
> +=head1 DESCRIPTION
>  
>  DBIx::Class::Schema::Loader automates the definition of a
>  L<DBIx::Class::Schema> by scanning database table definitions and
> @@ -86,7 +86,7 @@
>  
>  sub loader_options {
>      my $self = shift;
> -    
> +
>      my %args = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
>      $self->_loader_args(\%args);
>  
> @@ -342,11 +342,18 @@
>  
>  Based upon the work of IKEBE Tomohiro
>  
> -=head1 THANK YOU
> +=head1 CONTRIBUTORS
>  
> -Matt S Trout, all of the #dbix-class folks, and everyone who's ever sent
> -in a bug report or suggestion.
> +Matt S Trout
>  
> +Jess Robinson
> +
> +Dan Dascalescu
> +
> +All of the #dbix-class folks
> +
> +Everyone who's ever sent in a bug report or suggestion
> +
>  =head1 LICENSE
>  
>  This library is free software; you can redistribute it and/or modify it under
> Index: lib/DBIx/Class/Schema/Loader/Base.pm
> ===================================================================
> --- lib/DBIx/Class/Schema/Loader/Base.pm (revision 4507)
> +++ lib/DBIx/Class/Schema/Loader/Base.pm (working copy)
> @@ -642,7 +642,11 @@
>      my $table_class = $self->classes->{$table};
>      my $table_moniker = $self->monikers->{$table};
>  
> -    $self->_dbic_stmt($table_class,'table',$table);
> +    if ($self->{db_schema} and $table !~ /\./) {
> +        $self->_dbic_stmt($table_class, 'table', $self->{db_schema}.$self->{_namesep}.$table)
> +    } else {
> +        $self->_dbic_stmt($table_class, 'table', $table);
> +    }
>  
>      my $cols = $self->_table_columns($table);
>      my $col_info;
> Index: lib/DBIx/Class/Schema/Loader/DBI.pm
> ===================================================================
> --- lib/DBIx/Class/Schema/Loader/DBI.pm (revision 4507)
> +++ lib/DBIx/Class/Schema/Loader/DBI.pm (working copy)
> @@ -72,7 +72,7 @@
>  sub _setup { }
>  
>  # Returns an array of table names
> -sub _tables_list {
> +sub _tables_list {
>      my $self = shift;
>  
>      my $dbh = $self->schema->storage->dbh;
> @@ -116,7 +116,7 @@
>  }
>  
>  # Returns arrayref of pk col names
> -sub _table_pk_info {
> +sub _table_pk_info {
>      my ($self, $table) = @_;
>  
>      my $dbh = $self->schema->storage->dbh;
> @@ -155,6 +155,9 @@
>      my @retval;
>      foreach my $index_name (keys %indices) {
>          my $index = $indices{$index_name};
> +        # prefix index with schema name if supplied, unless index is already qualified with a schema name
> +        $index_name = $self->{db_schema}.$self->{_namesep}.$index_name
> +            if $self->{db_schema} and $index_name !~ /\./;
>          push(@retval, [ $index_name => [
>              map { $index->{$_} }
>                  sort keys %$index
> @@ -217,7 +220,17 @@
>                  $column_info{data_type}   = $info->{TYPE_NAME};
>                  $column_info{size}      = $info->{COLUMN_SIZE};
>                  $column_info{is_nullable}   = $info->{NULLABLE} ? 1 : 0;
> +
>                  $column_info{default_value} = $info->{COLUMN_DEF};
> +                # prefix sequences with schema name if supplied via db_schema
> +                # this only attempts to handle the sane cases, e.g. "nextval('accesslog_id_seq'::regclass)"
> +                # if the default value is something wacko like nextval('table1_id_seq'::regclass) + nextval('table2_id_seq'::regclass), tough luck
> +                if ($self->{db_schema} and defined $column_info{default_value}) {
> +                    $column_info{default_value} =~ s{^nextval\((['"])([^'".]+)\1}{
> +                        'nextval(' . $1 . $self->{db_schema} . $self->{_namesep} . $2 . $1
> +                    }xei;
> +                }
> +
>                  my $col_name = $info->{COLUMN_NAME};
>                  $col_name =~ s/^\"(.*)\"$/$1/;
>  
> Index: Changes
> ===================================================================
> --- Changes (revision 4507)
> +++ Changes (working copy)
> @@ -2,6 +2,8 @@
>  
>  0.04006 Not Yet Released
>          - Fix Oracle constraint detection for non-owned schemas (RT #35732)
> +        - qualified tables, sequences and unique constraints
> +          with schema name if supplied via db_schema (Dan Dascalescu)
>  
>  0.04005 Sat Apr 05, 2008
>          - Fix DB2 support
> @@ -73,7 +75,7 @@
>          - Workaround for new incompatible changes in DBD::mysql's "tables"
>            method, which was causing us to find no tables w/ DBD::mysql
>            4.002+
> -        - Fixed quoting problem in _table_columns (could cause crash when
> +        - Fixed quoting problem in _table_columns (could cause crash when
>            dumping/doing a static create) (from ash)
>  
>  0.03009 Wed Nov 15 14:03:37 UTC 2006

> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive: http://www.grokbase.com/group/dbix-class@...

--
      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/

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Loading...