[Jifty-commit] r3595 - in jifty/branches/virtual-models: . lib/Jifty t/TestApp-DatabaseBackedModels/t

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Sun Jul 1 23:56:58 EDT 2007


Author: sterling
Date: Sun Jul  1 23:56:58 2007
New Revision: 3595

Modified:
   jifty/branches/virtual-models/   (props changed)
   jifty/branches/virtual-models/lib/Jifty/Handle.pm
   jifty/branches/virtual-models/lib/Jifty/Record.pm
   jifty/branches/virtual-models/lib/Jifty/Script/Database.pm
   jifty/branches/virtual-models/t/TestApp-DatabaseBackedModels/t/dump_load.t

Log:
 r7914 at dynpc145:  andrew | 2007-07-01 22:40:17 -0500
 Added delete() to Jifty::Handle to cope with removal of UUIDs to prevent future duplicates, especially on SQLite.


Modified: jifty/branches/virtual-models/lib/Jifty/Handle.pm
==============================================================================
--- jifty/branches/virtual-models/lib/Jifty/Handle.pm	(original)
+++ jifty/branches/virtual-models/lib/Jifty/Handle.pm	Sun Jul  1 23:56:58 2007
@@ -225,6 +225,47 @@
     }
 }
 
+=head2 delete
+
+Delete UUIDs on successful delete.
+
+=cut
+
+sub delete {
+    my $self = shift;
+    my ($table, @pairs) = @_;
+
+    # XXX TODO FIXME This is expensive, but since an ID might be reused
+    # (especially on SQLite) and cause things to blow up, we have to do this.
+    my @bind = ();
+    my $where = 'WHERE ';
+    while (my $key = shift @pairs) {
+        $where .= $key . "=?" . " AND ";
+        push( @bind, shift(@pairs) );
+    }
+
+    $where =~ s/AND $//;
+    my $query_string = "SELECT id FROM " . $table . ' ' . $where;
+    my $sth = $self->simple_query($query_string, @bind);
+
+    my @delete_ids = ();
+    while (my ($id) = $sth->fetchrow_array) {
+        push @delete_ids, $id;
+    }
+
+    my $ret;
+    if ($ret = $self->SUPER::delete(@_)) {
+        for my $id (@delete_ids) {
+            $self->SUPER::delete('_jifty_uuids',
+                row_table => $table,
+                row_id    => $id,
+            );
+        }
+    }
+
+    return $ret;
+}
+
 =head2 insert
 
 Assign an UUID for each successfully inserted rows.

Modified: jifty/branches/virtual-models/lib/Jifty/Record.pm
==============================================================================
--- jifty/branches/virtual-models/lib/Jifty/Record.pm	(original)
+++ jifty/branches/virtual-models/lib/Jifty/Record.pm	Sun Jul  1 23:56:58 2007
@@ -73,6 +73,7 @@
     unless ( $self->check_create_rights(@_) ) {
         $self->log->error( $self->current_user->id . " tried to create a ",
             ref $self, " without permission" );
+        Test::More::diag(1);
         wantarray ? return ( 0, _('Permission denied') ) : return (0);
     }
 
@@ -100,8 +101,10 @@
             else {
                 $self->log->error("The UUID for $column_name was not found in the database.");
                 if ($class) {
+        Test::More::diag(2);
                     return($self);
                 } else {
+        Test::More::diag(3);
                     return (0, "UUID for $column_name was not found in the database.");
                 }
             }
@@ -122,8 +125,10 @@
         if ( not $val ) {
             $self->log->error("There was a validation error for $key");
             if ($class) {
+        Test::More::diag(4);
                 return ($self);
             } else {
+        Test::More::diag(5);
                 return ( $val, $msg );
             }
         }
@@ -140,13 +145,17 @@
     if ( ref($msg) ) {
 
         # It's a Class::ReturnValue
+        Test::More::diag(6);
         return $msg;
     }
     my ( $id, $status ) = $msg;
     $self->load_by_cols( id => $id ) if ($id);
     if ($class) {
+        Test::More::diag(7);
         return $self;
     } else {
+        use Data::Dumper;
+        Test::More::diag("8 ".Dumper($msg));
         return wantarray ? ( $id, $status ) : $id;
     }
 }

Modified: jifty/branches/virtual-models/lib/Jifty/Script/Database.pm
==============================================================================
--- jifty/branches/virtual-models/lib/Jifty/Script/Database.pm	(original)
+++ jifty/branches/virtual-models/lib/Jifty/Script/Database.pm	Sun Jul  1 23:56:58 2007
@@ -202,7 +202,7 @@
     while ( my $table = $current_tables->next ) {
         $self->log->debug("Thinking about upgrading table ".$table->name . "(".$table->__uuid .")");
         if ( my $new_table = delete $new_tables->{ $table->__uuid } ) {
-            $self->log->debug("It has the same uuid as tne proposed replacement");
+            $self->log->debug("It has the same uuid as the proposed replacement");
 
             # we have the same table in the db and the dump
             # let's sync its attributes from the dump then sync its columns
@@ -257,20 +257,23 @@
 
         # now we only have tables that were not yet in the database;
     }
-        $self->_upgrade_create_new_tables( $new_tables => $columns );
+    $self->_upgrade_create_new_tables( $new_tables => $columns );
 }
 
 sub _upgrade_create_new_tables {
     my $self       = shift;
     my $new_tables = shift;
     my $columns    = shift;
+    use Data::Dumper;
+    Test::More::diag('_upgrade_create_new_table: '.Dumper($self, $new_tables, $columns));
     foreach my $table_id ( keys %$new_tables ) {
         my $table = $new_tables->{$table_id};
         $self->log->debug("Creating a new table: ".$table->{name});
         delete $table->{id};
         my $class = Jifty::Model::ModelClass->new();
         my ( $val, $msg ) = $class->create( %{$table}, __uuid => $table_id );
-        die $msg unless ($val) ;
+        Test::More::diag('CREATE: '.Dumper($val, $msg, $class, $table, $table_id));
+        die ($msg||'Unknown error during create.') unless ($val) ;
         # Now that we have a brand new model, let's find all its columns
         my @cols = grep { $_->{model_class} = $table->{__uuid} } values %$columns;
         foreach my $col (@cols) {

Modified: jifty/branches/virtual-models/t/TestApp-DatabaseBackedModels/t/dump_load.t
==============================================================================
--- jifty/branches/virtual-models/t/TestApp-DatabaseBackedModels/t/dump_load.t	(original)
+++ jifty/branches/virtual-models/t/TestApp-DatabaseBackedModels/t/dump_load.t	Sun Jul  1 23:56:58 2007
@@ -45,6 +45,10 @@
 }
 );
 
+# Make sure we don't modify the schema, if these are missing, the whacked
+$new_widgets{'Jifty::Model::ModelClass'} = $dumped_bootstrap->{'Jifty::Model::ModelClass'};
+$new_widgets{'Jifty::Model::ModelClassColumn'} = $dumped_bootstrap->{'Jifty::Model::ModelClass'};
+
 
 
 $DATABASE->_load_data(\%new_widgets);


More information about the Jifty-commit mailing list