[Bast-commits] r3935 - in DBIx-Class-Partitioned/1.000/trunk:
lib/DBIx/Class t t/lib/My/Schema
edenc at dev.catalyst.perl.org
edenc at dev.catalyst.perl.org
Mon Jan 14 13:25:00 GMT 2008
Author: edenc
Date: 2008-01-14 13:25:00 +0000 (Mon, 14 Jan 2008)
New Revision: 3935
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/ActiveUser.pm
DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/InactiveUser.pm
DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Post.pm
DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/User.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
Log:
auto-creating ident rows
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-14 03:12:05 UTC (rev 3934)
+++ DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/Partitioned.pm 2008-01-14 13:25:00 UTC (rev 3935)
@@ -40,6 +40,7 @@
=cut
__PACKAGE__->mk_classdata('_partitions');
+__PACKAGE__->mk_classdata('ident_rel');
sub partitions {
my ( $class, @parts ) = @_;
@@ -87,7 +88,17 @@
}
sub new {
- my $self = shift->next::method(@_);
+ my $class = shift;
+ my $self = $class->next::method(@_);
+ my $ident_source_name =
+ $self->relationship_info( $self->ident_rel )->{source};
+ my $ident_source = $self->result_source->schema->source($ident_source_name);
+ my $ident_row =
+ $self->create_related( $self->ident_rel,
+ { map { $_ => undef } $ident_source->columns } );
+ $ident_row->discard_changes;
+ my %pks = $ident_row->get_columns;
+ $self->$_( $pks{$_} ) for keys %pks;
$self->set_partition_source;
return $self;
}
Modified: DBIx-Class-Partitioned/1.000/trunk/t/01-basic.t
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/t/01-basic.t 2008-01-14 03:12:05 UTC (rev 3934)
+++ DBIx-Class-Partitioned/1.000/trunk/t/01-basic.t 2008-01-14 13:25:00 UTC (rev 3935)
@@ -6,18 +6,22 @@
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 = { 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' } );
+ my $data = { foo => 'bar', is_expired => 0 };
+ isa_ok( my $user = $user_rs->create($data), 'My::Schema::User' );
+ $user->add_to_affiliations( { name => 'foo' } );
+ $user->add_to_posts( { title => 'foo', content => 'bar' } );
row_fields_equal(
- $user_rs->search($data)->single,
- my $user = $schema->resultset('ActiveUser')->search($data)->single,
+ my $puser = $user_rs->search($data)->single,
+ $user = $schema->resultset('ActiveUser')->search($data)->single,
[qw/id foo is_expired/]
);
is( $puser->affiliations->first->name, 'foo' );
+ is( $user->affiliations->first->name, 'foo' );
+ is( $puser->posts->first->title, 'foo' );
+ is( $puser->posts->first->content, 'bar' );
+ is( $user->posts->first->title, 'foo' );
+ is( $user->posts->first->content, 'bar' );
$data->{is_expired} = 1;
isa_ok( $user_rs->create($data), 'My::Schema::User' );
@@ -26,6 +30,11 @@
$schema->resultset('InactiveUser')->search($data)->single,
[qw/id foo is_expired/]
);
+ $user->add_to_affiliations( $schema->resultset('Affiliation')->first );
+ $user->add_to_posts( { title => 'foo', content => 'bar' } );
+ is( $user->affiliations->first->name, 'foo' );
+ is( $user->posts->first->title, 'foo' );
+ is( $user->posts->first->content, 'bar' );
$user->is_expired(0);
$user->update;
ok( $schema->resultset('ActiveUser')->search( { $user->get_columns } )
@@ -33,6 +42,8 @@
ok( !$schema->resultset('InactiveUser')->search( { $user->get_columns } )
->count );
is( $user->affiliations->first->name, 'foo' );
+ is( $user->posts->first->title, 'foo' );
+ is( $user->posts->first->content, 'bar' );
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-14 03:12:05 UTC (rev 3934)
+++ DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/ActiveUser.pm 2008-01-14 13:25:00 UTC (rev 3935)
@@ -13,7 +13,11 @@
__PACKAGE__->add_columns(qw/foo is_expired/);
__PACKAGE__->set_primary_key('id');
-__PACKAGE__->has_many( 'affiliations',
+__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/InactiveUser.pm
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/InactiveUser.pm 2008-01-14 03:12:05 UTC (rev 3934)
+++ DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/InactiveUser.pm 2008-01-14 13:25:00 UTC (rev 3935)
@@ -13,4 +13,11 @@
__PACKAGE__->add_columns(qw/foo is_expired/);
__PACKAGE__->set_primary_key('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/Post.pm
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Post.pm 2008-01-14 03:12:05 UTC (rev 3934)
+++ DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Post.pm 2008-01-14 13:25:00 UTC (rev 3935)
@@ -15,7 +15,6 @@
);
__PACKAGE__->set_primary_key('id');
-__PACKAGE__->belongs_to( 'user',
- 'UserIdent' => { 'foreign.id' => 'self.user_id' } );
+__PACKAGE__->belongs_to( 'user', 'User' => { '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-14 03:12:05 UTC (rev 3934)
+++ DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/User.pm 2008-01-14 13:25:00 UTC (rev 3935)
@@ -15,13 +15,25 @@
__PACKAGE__->has_one(
'user_ident',
'UserIdent' => { 'foreign.id' => 'self.id' },
- {
- proxy => [qw/posts affiliations user_affiliations/],
- cascade_delete => 0
- }
+ { cascade_delete => 0 }
);
+__PACKAGE__->has_many(
+ 'posts',
+ 'Post' => { 'foreign.user_id' => 'self.id' },
+ { cascade_delete => 0 }
+);
+
+__PACKAGE__->has_many(
+ 'user_affiliations',
+ 'UserAffiliation' => { 'foreign.user_id' => 'self.id' },
+ { cascade_delete => 0 }
+);
+
+__PACKAGE__->many_to_many( 'affiliations', 'user_affiliations', 'affiliation' );
+
__PACKAGE__->partitions(qw/inactive_user active_user/);
+__PACKAGE__->ident_rel('user_ident');
sub _partition_name {
return shift->is_expired ? 'inactive_user' : 'active_user';
Modified: DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/UserAffiliation.pm
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/UserAffiliation.pm 2008-01-14 03:12:05 UTC (rev 3934)
+++ DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/UserAffiliation.pm 2008-01-14 13:25:00 UTC (rev 3935)
@@ -13,7 +13,7 @@
);
__PACKAGE__->belongs_to(
- user => 'UserIdent',
+ user => 'User',
{ 'foreign.id' => 'self.user_id' }
);
__PACKAGE__->belongs_to(
Modified: DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/UserIdent.pm
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/UserIdent.pm 2008-01-14 03:12:05 UTC (rev 3934)
+++ DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/UserIdent.pm 2008-01-14 13:25:00 UTC (rev 3935)
@@ -12,11 +12,4 @@
__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;
More information about the Bast-commits
mailing list