[Jifty-commit] r2720 - in jifty/branches/virtual-models: .

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Mon Jan 29 06:49:16 EST 2007


Author: jesse
Date: Mon Jan 29 06:48:58 2007
New Revision: 2720

Modified:
   jifty/branches/virtual-models/   (props changed)
   jifty/branches/virtual-models/lib/Jifty/Script/Database.pm

Log:
 r21672 at hualien:  jesse | 2007-01-29 19:37:25 +0800
 * When you write it a second time after thinking about it for 5 minutes, your code gets cleaner. Shocking!


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	Mon Jan 29 06:48:58 2007
@@ -102,7 +102,7 @@
     my $current_tables = Jifty::Model::ModelClassCollection->new();
     $current_tables->unlimit();
     while ( my $table = $current_tables->next ) {
-        if ( my $new_table = $new_tables->{ $table->uuid } ) {
+        if ( my $new_table = delete $new_tables->{ $table->uuid } ) {
 
             # we have the same table in the db and the dump
             # let's sync its attributes from the dump then sync its columns
@@ -113,43 +113,42 @@
                 }
             }
 
-            my $existing_cols = {};
-            map { $existing_cols->{ $_->uuid } = $_ }
-                @{ $table->included_columns };
-
-            # Now that we've taken care of that, let's sync up our columns.
-            foreach my $col ( grep { $_->{model_class} = $table->{uuid} }
-                values %$columns ) {
+            my $current_columns = $table->included_columns;
+            my $new_columns     = {};
+            map {
                 delete
-                    $col->{id}; # the id is only important on the first system
-
-                # if we already have such a column, update it.
-                if ( my $existing_col = $existing_cols->{ $col->{uuid} } ) {
-                    foreach my $key ( keys %$col ) {
-                        unless ( $existing_col->$key() eq $col->{$key} ) {
+                    $_->{id};   # the id is only important on the first system
+                $new_columns->{ $_->uuid } = $_
+                } grep {
+                $_->{model_class} = $table->{uuid}
+                } values %$columns;
+
+            while ( my $column = $current_columns->next ) {
+
+                # First, update ones we know about
+                if ( my $new_column = delete $new_columns->{ $column->uuid } )
+                {
+                    foreach my $key ( keys %$new_column ) {
+                        unless ( $column->$key() eq $new_column->{$key} ) {
                             my $method = "set_" . $key;
-                            $existing_col->$method( $col->{$key} );
+                            $column->$method( $new_column->{$key} );
                         }
                     }
 
-                    delete $existing_cols->{ $col->{uuid} };
-
                 }
 
-                #otherwise we need to add it
+                # Second, delete columns that aren't in the dump file
                 else {
-                    Jifty::Model::ModelClassColumn->create(%$col);
+                    $column->delete();
                 }
-            }
 
-            # now that we've added all the columns we care about and
-            # modded the ones we know, everything left is something to delete;
+                # Third, add columns that are only in the dumpfile
+            }
 
-            for ( values %$existing_cols ) {
-                $_->delete;
+            foreach my $col ( values %$new_columns ) {
+                Jifty::Model::ModelClassColumn->create(%$col);
             }
 
-            delete $new_tables->{ $table->uuid };
         } else {
 
             # we don't have the table anymore. That means we should delete it.
@@ -162,7 +161,6 @@
     }
 }
 
-
 sub _upgrade_create_new_tables {
     my $self       = shift;
     my $new_tables = shift;


More information about the Jifty-commit mailing list