[Bast-commits] r4722 - in DBIx-Class-Preview/1.000/trunk:
lib/DBIx/Class lib/DBIx/Class/ResultSource/Table
lib/DBIx/Class/Schema t t/lib t/lib/DBICTest
t/lib/DBICTest/Schema t/var
lukes at dev.catalyst.perl.org
lukes at dev.catalyst.perl.org
Sat Aug 2 11:45:31 BST 2008
Author: lukes
Date: 2008-08-02 11:45:30 +0100 (Sat, 02 Aug 2008)
New Revision: 4722
Added:
DBIx-Class-Preview/1.000/trunk/t/lib/sqlite_preview.sql
DBIx-Class-Preview/1.000/trunk/t/publish.t
Modified:
DBIx-Class-Preview/1.000/trunk/lib/DBIx/Class/Preview.pm
DBIx-Class-Preview/1.000/trunk/lib/DBIx/Class/ResultSource/Table/Previewed.pm
DBIx-Class-Preview/1.000/trunk/lib/DBIx/Class/Schema/Preview.pm
DBIx-Class-Preview/1.000/trunk/t/lib/DBICTest.pm
DBIx-Class-Preview/1.000/trunk/t/lib/DBICTest/Schema.pm
DBIx-Class-Preview/1.000/trunk/t/lib/DBICTest/Schema/Artist.pm
DBIx-Class-Preview/1.000/trunk/t/lib/sqlite.sql
DBIx-Class-Preview/1.000/trunk/t/var/DBIxClass.db
Log:
added insert support and a test
Modified: DBIx-Class-Preview/1.000/trunk/lib/DBIx/Class/Preview.pm
===================================================================
--- DBIx-Class-Preview/1.000/trunk/lib/DBIx/Class/Preview.pm 2008-08-01 19:55:12 UTC (rev 4721)
+++ DBIx-Class-Preview/1.000/trunk/lib/DBIx/Class/Preview.pm 2008-08-02 10:45:30 UTC (rev 4722)
@@ -40,6 +40,17 @@
return $self->next::method(@_);
}
+sub insert {
+ my $self = shift;
+
+ # mark row as dirty
+ if ($self->result_source->schema->preview_active() && $self->result_source->can('is_preview_source')) {
+ $self->set_column('dirty', 1);
+ }
+
+ return $self->next::method(@_);
+}
+
# dirty col set here by default value on the table def
sub new {
my $self = shift;
Modified: DBIx-Class-Preview/1.000/trunk/lib/DBIx/Class/ResultSource/Table/Previewed.pm
===================================================================
--- DBIx-Class-Preview/1.000/trunk/lib/DBIx/Class/ResultSource/Table/Previewed.pm 2008-08-01 19:55:12 UTC (rev 4721)
+++ DBIx-Class-Preview/1.000/trunk/lib/DBIx/Class/ResultSource/Table/Previewed.pm 2008-08-02 10:45:30 UTC (rev 4722)
@@ -7,35 +7,34 @@
use base qw/DBIx::Class::ResultSource::Table/;
sub schema {
- my $self = shift;
+ my $self = shift;
- if ( @_ && !$self->schema ) { # only fire if we're getting schema set for first time
- my ($schema) = @_;
+ if ( @_ && !$self->schema ) { # only fire if we're getting schema set for first time
+ my ($schema) = @_;
- my $new_source = DBIx::Class::ResultSource::Table::Preview->new({
+ my $new_source = DBIx::Class::ResultSource::Table::Preview->new({
%$self,
name => 'top_100_batch_preview',
_relationships => Storable::dclone( $self->_relationships ),
});
- $new_source->add_column('dirty' => { data_type => 'integer', default_value => 1 });
- $new_source->preview_table($self->from . '_preview');
- $new_source->relationship_info($_)->{attrs}{cascade_delete} = 0
+ $new_source->add_column('dirty' => { data_type => 'integer', default_value => 1 });
+ $new_source->preview_table($self->from . '_preview');
+ $new_source->relationship_info($_)->{attrs}{cascade_delete} = 0
for $new_source->relationships;
- my $new_source_name =
- $self->source_name . '->preview(top_100_batch_preview)';
- $schema->register_extra_source( $new_source_name => $new_source );
+ my $new_source_name =
+ $self->source_name . '->preview';
+ $schema->register_extra_source( $new_source_name => $new_source );
}
-
- return $self->next::method(@_);
+ return $self->next::method(@_);
}
sub previewed {
- my ( $self ) = @_;
+ my ( $self ) = @_;
- my $schema = $self->schema || die "No schema";
- my $partition =
- $schema->source( $self->source_name . '->preview(top_100_batch_preview)' );
- return $partition;
+ my $schema = $self->schema || die "No schema";
+ my $partition =
+ $schema->source( $self->source_name . '->preview' );
+ return $partition;
}
Modified: DBIx-Class-Preview/1.000/trunk/lib/DBIx/Class/Schema/Preview.pm
===================================================================
--- DBIx-Class-Preview/1.000/trunk/lib/DBIx/Class/Schema/Preview.pm 2008-08-01 19:55:12 UTC (rev 4721)
+++ DBIx-Class-Preview/1.000/trunk/lib/DBIx/Class/Schema/Preview.pm 2008-08-02 10:45:30 UTC (rev 4722)
@@ -32,7 +32,7 @@
$clone->storage->set_schema($clone) if $clone->storage;
return $clone;
}
-
+
# call this to move all dirty rows to the main table
sub publish {
my $self = shift;
@@ -50,12 +50,13 @@
if ($source->can('previewed')) {
my $original_rs = $schema->resultset($source->source_name);
my $previewed_rs = $self->resultset($source->source_name);
-
+
my $dirty_previewed_rs = $previewed_rs->search({ dirty => 1 });
while (my $dirty_row = $dirty_previewed_rs->next) {
my $original_row = $original_rs->find($dirty_row->id);
my %dirty_cols = $dirty_row->get_columns;
delete $dirty_cols{dirty};
+
if ($original_row) {
$original_row->update(\%dirty_cols);
} else {
Modified: DBIx-Class-Preview/1.000/trunk/t/lib/DBICTest/Schema/Artist.pm
===================================================================
--- DBIx-Class-Preview/1.000/trunk/t/lib/DBICTest/Schema/Artist.pm 2008-08-01 19:55:12 UTC (rev 4721)
+++ DBIx-Class-Preview/1.000/trunk/t/lib/DBICTest/Schema/Artist.pm 2008-08-02 10:45:30 UTC (rev 4722)
@@ -2,6 +2,7 @@
DBICTest::Schema::Artist;
use base 'DBIx::Class::Core';
+__PACKAGE__->load_components('Preview');
__PACKAGE__->table('artist');
__PACKAGE__->add_columns(
Modified: DBIx-Class-Preview/1.000/trunk/t/lib/DBICTest/Schema.pm
===================================================================
--- DBIx-Class-Preview/1.000/trunk/t/lib/DBICTest/Schema.pm 2008-08-01 19:55:12 UTC (rev 4721)
+++ DBIx-Class-Preview/1.000/trunk/t/lib/DBICTest/Schema.pm 2008-08-02 10:45:30 UTC (rev 4722)
@@ -6,5 +6,6 @@
no warnings qw/qw/;
__PACKAGE__->load_classes(qw/Artist CD Track Tag Producer CD_to_Producer/);
+__PACKAGE__->load_components(qw/Schema::Preview/);
1;
Modified: DBIx-Class-Preview/1.000/trunk/t/lib/DBICTest.pm
===================================================================
--- DBIx-Class-Preview/1.000/trunk/t/lib/DBICTest.pm 2008-08-01 19:55:12 UTC (rev 4721)
+++ DBIx-Class-Preview/1.000/trunk/t/lib/DBICTest.pm 2008-08-02 10:45:30 UTC (rev 4722)
@@ -70,8 +70,9 @@
}
if ( !$args{no_deploy} ) {
- __PACKAGE__->deploy_schema( $schema );
+ __PACKAGE__->apply_sql( $schema, $self->get_ddl_file($schema) );
__PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} );
+ __PACKAGE__->apply_sql( $schema, $self->get_preview_file($schema) );
}
return $schema;
}
@@ -84,17 +85,25 @@
return 't/lib/' . lc($schema->storage->dbh->{Driver}->{Name}) . '.sql';
}
+sub get_preview_file {
+ my $self = shift;
+ my $schema = shift;
+
+ return 't/lib/' . lc($schema->storage->dbh->{Driver}->{Name}) . '_preview.sql';
+}
+
=head2 deploy_schema
DBICTest->deploy_schema( $schema );
=cut
-sub deploy_schema {
+sub apply_sql {
my $self = shift;
my $schema = shift;
- my $file = shift || $self->get_ddl_file($schema);
+ my $file = shift;
+ warn $file;
open IN, $file;
my $sql;
{ local $/ = undef; $sql = <IN>; }
Modified: DBIx-Class-Preview/1.000/trunk/t/lib/sqlite.sql
===================================================================
--- DBIx-Class-Preview/1.000/trunk/t/lib/sqlite.sql 2008-08-01 19:55:12 UTC (rev 4721)
+++ DBIx-Class-Preview/1.000/trunk/t/lib/sqlite.sql 2008-08-02 10:45:30 UTC (rev 4722)
@@ -21,7 +21,6 @@
name varchar(100)
);
-
--
-- Table: cd
--
Added: DBIx-Class-Preview/1.000/trunk/t/lib/sqlite_preview.sql
===================================================================
--- DBIx-Class-Preview/1.000/trunk/t/lib/sqlite_preview.sql (rev 0)
+++ DBIx-Class-Preview/1.000/trunk/t/lib/sqlite_preview.sql 2008-08-02 10:45:30 UTC (rev 4722)
@@ -0,0 +1,10 @@
+--
+-- Table: artist
+--
+CREATE TABLE artist_preview (
+ artistid INTEGER PRIMARY KEY NOT NULL,
+ name varchar(100),
+ dirty INTEGER DEFAULT '0',
+ deleted INTEGER DEFAULT '0'
+);
+insert into artist_preview select *, 0, 0 from artist;
\ No newline at end of file
Added: DBIx-Class-Preview/1.000/trunk/t/publish.t
===================================================================
--- DBIx-Class-Preview/1.000/trunk/t/publish.t (rev 0)
+++ DBIx-Class-Preview/1.000/trunk/t/publish.t 2008-08-02 10:45:30 UTC (rev 4722)
@@ -0,0 +1,50 @@
+use strict;
+use warnings;
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+use DBICTest;
+use Test::More 'no_plan';
+
+my $schema = DBICTest->init_schema;
+$schema->preview_active(1);
+my $unpreviewed_schema = $schema->unpreviewed;
+my $row = $schema->resultset('Artist')->first;
+ok($row, 'got an artist row');
+my $orig_row = $unpreviewed_schema->resultset('Artist')->find($row->id);
+
+my %orig_cols = $row->get_columns;
+$row->update({name => 7});
+$orig_row->discard_changes;
+$row->discard_changes;
+
+is($row->name, 7, 'set changed on preview row');
+is($orig_row->name, $orig_cols{name}, 'set unchanged on original row');
+
+$row->name(4);
+$row->update;
+
+$orig_row->discard_changes;
+$row->discard_changes;
+
+is($row->name, 4, 'set changed on preview row');
+is($orig_row->name, $orig_cols{name}, 'set unchanged on original row');
+
+#use Data::Dumper; warn Dumper({ $row->get_columns }, { $orig_row->get_columns });
+
+my $new_row = $schema->resultset('Artist')->create({ name => 15 });
+$new_row->discard_changes;
+
+my $unpreviewed_new_row = $unpreviewed_schema->resultset('Artist')->find($new_row->id);
+is($unpreviewed_new_row, undef, 'new row not present in original table');
+
+$schema->publish;
+
+$orig_row->discard_changes;
+is($orig_row->name, $row->name, 'set changed on original row');
+ok($unpreviewed_schema->resultset('Artist')->find($new_row->id), 'new row copied over');
+
+ok($unpreviewed_schema->source('DBICTest::Schema::Artist'), 'got source using full name');
+
+is($schema->resultset('DBICTest::Schema::Artist')->search({ dirty => 1 })->count, 0, 'dirty rows cleared');
Modified: DBIx-Class-Preview/1.000/trunk/t/var/DBIxClass.db
===================================================================
(Binary files differ)
More information about the Bast-commits
mailing list