[Bast-commits] r3934 - in DBIx-Class-Partitioned/1.000/trunk:
lib/DBIx/Class t t/lib/My t/lib/My/Schema
edenc at dev.catalyst.perl.org
edenc at dev.catalyst.perl.org
Mon Jan 14 03:12:06 GMT 2008
Author: edenc
Date: 2008-01-14 03:12:05 +0000 (Mon, 14 Jan 2008)
New Revision: 3934
Added:
DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Affiliation.pm
DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Post.pm
DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/UserAffiliation.pm
DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/UserIdent.pm
Modified:
DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/Partitioned.pm
DBIx-Class-Partitioned/1.000/trunk/t/01-basic.t
DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema.pm
DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/ActiveUser.pm
DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/User.pm
DBIx-Class-Partitioned/1.000/trunk/t/test.db
Log:
dealing with partitioned relationships
Modified: DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/Partitioned.pm
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/Partitioned.pm 2008-01-12 19:51:36 UTC (rev 3933)
+++ DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/Partitioned.pm 2008-01-14 03:12:05 UTC (rev 3934)
@@ -3,6 +3,7 @@
use warnings;
use strict;
use base qw/DBIx::Class/;
+use Data::Dump;
=head1 NAME
@@ -39,7 +40,6 @@
=cut
__PACKAGE__->mk_classdata('_partitions');
-__PACKAGE__->mk_classdata('select_partition_via');
sub partitions {
my ( $class, @parts ) = @_;
@@ -53,6 +53,11 @@
my $new_source_name = join '::', map { ucfirst } split '_', $partition;
$new_source->source_name($new_source_name);
$new_source->name($partition);
+ foreach my $rel_name ( $source->relationships ) {
+ my $rel_info = $source->relationship_info($rel_name);
+ $new_source->add_relationship( $rel_name,
+ @{$rel_info}{qw/source cond attrs/} );
+ }
$class->_partitions->{$partition} = $new_source;
}
}
@@ -76,9 +81,8 @@
}
sub set_partition_source {
- my ($self) = @_;
- my $method = $self->select_partition_via;
- my $partition_name = $self->$method;
+ my ($self) = @_;
+ my $partition_name = $self->_partition_name;
$self->result_source( $self->partition($partition_name) );
}
@@ -95,12 +99,11 @@
}
sub update {
- my $self = shift;
+ my $self = shift;
my $old_partition = $self->result_source;
$self->set_partition_source;
my $new_partition = $self->result_source;
- if ( $new_partition->source_name ne $old_partition->source_name )
- {
+ if ( $new_partition->source_name ne $old_partition->source_name ) {
$self->result_source($old_partition);
$self->delete;
$self->result_source($new_partition);
@@ -133,7 +136,6 @@
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
-
=cut
1;
Modified: DBIx-Class-Partitioned/1.000/trunk/t/01-basic.t
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/t/01-basic.t 2008-01-12 19:51:36 UTC (rev 3933)
+++ DBIx-Class-Partitioned/1.000/trunk/t/01-basic.t 2008-01-14 03:12:05 UTC (rev 3934)
@@ -6,19 +6,23 @@
use My::Schema;
my $schema = My::Schema->connect('dbi:SQLite:dbname=t/test.db');
my $test = sub {
+ my $user_ident = $schema->resultset('UserIdent')->create( { id => 1 } );
+ $user_ident->discard_changes;
my $user_rs = $schema->resultset('User');
- my $data = { foo => 'bar', is_expired => 0 };
- isa_ok( $user_rs->create($data), 'My::Schema::User' );
+ my $data = { id => $user_ident->id, foo => 'bar', is_expired => 0 };
+ isa_ok( my $puser = $user_rs->create($data), 'My::Schema::User' );
+ $puser->user_ident->add_to_affiliations( { name => 'foo' } );
row_fields_equal(
$user_rs->search($data)->single,
- $schema->resultset('ActiveUser')->search($data)->single,
+ my $user = $schema->resultset('ActiveUser')->search($data)->single,
[qw/id foo is_expired/]
);
+ is( $puser->affiliations->first->name, 'foo' );
$data->{is_expired} = 1;
isa_ok( $user_rs->create($data), 'My::Schema::User' );
row_fields_equal(
- my $user = $user_rs->search($data)->single,
+ $user = $user_rs->search($data)->single,
$schema->resultset('InactiveUser')->search($data)->single,
[qw/id foo is_expired/]
);
@@ -28,6 +32,7 @@
->count );
ok( !$schema->resultset('InactiveUser')->search( { $user->get_columns } )
->count );
+ is( $user->affiliations->first->name, 'foo' );
die 'rollback';
};
eval { $schema->txn_do($test) };
Modified: DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/ActiveUser.pm
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/ActiveUser.pm 2008-01-12 19:51:36 UTC (rev 3933)
+++ DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/ActiveUser.pm 2008-01-14 03:12:05 UTC (rev 3934)
@@ -13,4 +13,7 @@
__PACKAGE__->add_columns(qw/foo is_expired/);
__PACKAGE__->set_primary_key('id');
+__PACKAGE__->has_many( 'affiliations',
+ 'UserAffiliation' => { 'foreign.user_id' => 'self.id' } );
+
1;
Added: DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Affiliation.pm
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Affiliation.pm (rev 0)
+++ DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Affiliation.pm 2008-01-14 03:12:05 UTC (rev 3934)
@@ -0,0 +1,20 @@
+package My::Schema::Affiliation;
+
+use warnings;
+use strict;
+
+use base qw/DBIx::Class/;
+
+__PACKAGE__->load_components(qw/Core/);
+__PACKAGE__->table('affiliation');
+__PACKAGE__->add_columns(
+ id => { data_type => 'integer', is_auto_increment => 1 } );
+__PACKAGE__->add_columns(qw/name/);
+__PACKAGE__->set_primary_key('id');
+
+__PACKAGE__->has_many( 'user_affiliations',
+ 'UserAffiliation' => { 'foreign.affiliation_id' => 'self.id' } );
+
+__PACKAGE__->many_to_many( 'members', 'user_affiliations', 'user' );
+
+1;
Added: DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Post.pm
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Post.pm (rev 0)
+++ DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Post.pm 2008-01-14 03:12:05 UTC (rev 3934)
@@ -0,0 +1,21 @@
+package My::Schema::Post;
+
+use warnings;
+use strict;
+
+use base qw/DBIx::Class/;
+
+__PACKAGE__->load_components(qw/Core/);
+__PACKAGE__->table('post');
+__PACKAGE__->add_columns(
+ id => { data_type => 'integer' },
+ user_id => { data_type => 'integer' },
+ title => { data_type => 'text' },
+ content => { data_type => 'text' }
+);
+__PACKAGE__->set_primary_key('id');
+
+__PACKAGE__->belongs_to( 'user',
+ 'UserIdent' => { 'foreign.id' => 'self.user_id' } );
+
+1;
Modified: DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/User.pm
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/User.pm 2008-01-12 19:51:36 UTC (rev 3933)
+++ DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/User.pm 2008-01-14 03:12:05 UTC (rev 3934)
@@ -7,15 +7,23 @@
__PACKAGE__->load_components(qw/Partitioned Core/);
__PACKAGE__->table('user');
-__PACKAGE__->partitions(qw/inactive_user active_user/);
-__PACKAGE__->add_column(
- id => { is_auto_increment => 1, data_type => 'integer' } );
+__PACKAGE__->add_column( id => { data_type => 'integer' } );
__PACKAGE__->add_columns(qw/foo is_expired/);
__PACKAGE__->set_primary_key('id');
-__PACKAGE__->select_partition_via('is_active');
-sub is_active {
+__PACKAGE__->has_one(
+ 'user_ident',
+ 'UserIdent' => { 'foreign.id' => 'self.id' },
+ {
+ proxy => [qw/posts affiliations user_affiliations/],
+ cascade_delete => 0
+ }
+);
+
+__PACKAGE__->partitions(qw/inactive_user active_user/);
+
+sub _partition_name {
return shift->is_expired ? 'inactive_user' : 'active_user';
}
Added: DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/UserAffiliation.pm
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/UserAffiliation.pm (rev 0)
+++ DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/UserAffiliation.pm 2008-01-14 03:12:05 UTC (rev 3934)
@@ -0,0 +1,24 @@
+package My::Schema::UserAffiliation;
+
+use warnings;
+use strict;
+
+use base qw/DBIx::Class/;
+
+__PACKAGE__->load_components(qw/Core/);
+__PACKAGE__->table('user_affiliation');
+__PACKAGE__->add_columns(
+ user_id => { data_type => 'integer' },
+ affiliation_id => { data_type => 'integer' }
+);
+
+__PACKAGE__->belongs_to(
+ user => 'UserIdent',
+ { 'foreign.id' => 'self.user_id' }
+);
+__PACKAGE__->belongs_to(
+ affiliation => 'Affiliation',
+ { 'foreign.id' => 'self.affiliation_id' }
+);
+
+1;
Added: DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/UserIdent.pm
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/UserIdent.pm (rev 0)
+++ DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/UserIdent.pm 2008-01-14 03:12:05 UTC (rev 3934)
@@ -0,0 +1,22 @@
+package My::Schema::UserIdent;
+
+use warnings;
+use strict;
+
+use base qw/DBIx::Class/;
+
+__PACKAGE__->load_components(qw/Core/);
+__PACKAGE__->table('user_ident');
+__PACKAGE__->add_columns( id => { data_type => 'integer' } );
+__PACKAGE__->set_primary_key('id');
+
+__PACKAGE__->has_one( 'user', 'User' => { 'foreign.id' => 'self.id' } );
+
+__PACKAGE__->has_many( 'posts', 'Post' => { 'foreign.user_id' => 'self.id' } );
+
+__PACKAGE__->has_many( 'user_affiliations',
+ 'UserAffiliation' => { 'foreign.user_id' => 'self.id' } );
+
+__PACKAGE__->many_to_many( 'affiliations', 'user_affiliations', 'affiliation' );
+
+1;
Modified: DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema.pm
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema.pm 2008-01-12 19:51:36 UTC (rev 3933)
+++ DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema.pm 2008-01-14 03:12:05 UTC (rev 3934)
@@ -6,7 +6,8 @@
use base qw/DBIx::Class::Schema/;
__PACKAGE__->load_classes();
-#__PACKAGE__->load_classes(qw/ActiveUser InactiveUser/);
+#__PACKAGE__->load_classes(
+# qw/UserIdent ActiveUser InactiveUser UserAffiliation Affiliation Post/);
sub setup_test_data {
my ($self) = @_;
Modified: DBIx-Class-Partitioned/1.000/trunk/t/test.db
===================================================================
(Binary files differ)
More information about the Bast-commits
mailing list