[Jifty-commit] r2848 - in jifty/branches/virtual-models: . lib/Jifty lib/Jifty/Upgrade

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Mon Feb 26 18:14:29 EST 2007


Author: sterling
Date: Mon Feb 26 18:14:28 2007
New Revision: 2848

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

Log:
 * added UUID generation on upgrade
 * added experimental --dump --as-perl feature to database script

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	Mon Feb 26 18:14:28 2007
@@ -243,12 +243,15 @@
                 id => undef,
                 uuid => undef,
                 @_);
-           my $uuid =  ($args{uuid} || Jifty::Util->generate_uuid);
-        # Generate a UUID on the sideband: $table - $rv - UUID.
-        $self->dbh->do(
-            qq[ INSERT INTO _jifty_uuids VALUES (?, ?, ?) ], {},
-           $uuid , $args{table}, $args{id}
-        );
+
+    my $uuid =  ($args{uuid} || Jifty::Util->generate_uuid);
+
+    # Generate a UUID on the sideband: $table - $rv - UUID.
+    $self->dbh->do(
+        qq[ INSERT INTO _jifty_uuids VALUES (?, ?, ?) ], {},
+        $uuid , $args{table}, $args{id}
+    );
+
     return $uuid; 
 }
 
@@ -279,6 +282,37 @@
     ]);
 }
 
+=head2 insert_uuids_for_existing_rows 
+
+Called by the upgrade script to create UUIDs for existing rows. Could be called at any time to create UUIDs for any rows that don't already have them.
+
+=cut
+
+sub insert_uuids_for_existing_rows {
+    my $self = shift;
+
+    # Iterate over all models
+    for my $model (Jifty->class_loader->models) {
+        # If the model isn't a record, ignore it
+        next unless $model->isa('Jifty::Record');
+
+        # Load the collection for the model
+        my $collection = $model . 'Collection';
+        $collection->require;
+
+        # Create an unlimited instance of the collection
+        my $records = $collection->new;
+        $records->unlimit;
+
+        # Iterate over all the records in the collection
+        while (my $record = $records->next) {
+
+            # Lookup UUID will create one if it doesn't exist
+            my $uuid = $self->lookup_uuid($model->table, $record->id);
+        }
+    }    
+}
+
 =head2 lookup_uuid($table, $id)
 
 Look up the UUID for a given row.
@@ -287,11 +321,18 @@
 
 sub lookup_uuid {
     my ($self, $table, $id) = @_;
+
+    # No UUIDs unless RecordUUIDs is turned on (lazy or active will do)
     return undef unless ( Jifty->config->framework('Database')->{'RecordUUIDs'} ne 'off');
+
+    # Look for an existing UUID first
     my ($uuid) = $self->fetch_result(qq[ SELECT uuid FROM _jifty_uuids WHERE row_table = ? AND row_id = ?  ], $table, $id);
+
+    # If none found, create it
     unless ($uuid) {
         $uuid = $self->_insert_uuid( table => $table, id => $id);
     }
+
     return $uuid;
 }
 

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 Feb 26 18:14:28 2007
@@ -10,12 +10,11 @@
 
 =head1 NAME
 
-Jifty::Script::Database 
+Jifty::Script::Database - script for loading/dumping data from Jifty
 
 =head1 DESCRIPTION
 
-When you're getting started with Jifty, this is the server you
-want. It's lightweight and easy to work with.
+This script performs database dumps/loads on the database. This is particularly useful if part of your schema is stored in the database.
 
 =head1 API
 
@@ -28,7 +27,8 @@
     (
      'dump'       => 'dump',
      'load'       => 'load',
-     'replace'  => 'replace',
+     'replace'    => 'replace',
+     'as-perl'    => 'as_perl'
     )
 }
 
@@ -103,7 +103,7 @@
         "There's no locally defined class called $class. Without that, we can't insert records into it: $@"
         );
     }
-    my $current_user = Jifty::CurrentUser->new( _bootstrap => 1 );
+    my $current_user = Jifty->app_class('CurrentUser')->new( _bootstrap => 1 );
     foreach my $id ( sort keys %$content ) {
         my $obj = $class->new( current_user => $current_user );
         if ( $self->{'replace'} ) {
@@ -233,7 +233,13 @@
 sub dump {
     my $self = shift;
     my $content = $self->_models_to_hash();
-    print Jifty::YAML::Dump($content)."\n";
+
+    if ($self->{as_perl}) {
+        $self->dump_as_perl($content);
+    }
+    else {
+        print Jifty::YAML::Dump($content)."\n";
+    }
 }
 
 sub _models_to_hash {
@@ -241,11 +247,12 @@
         my $content = {};
         foreach my $model (Jifty->class_loader->models, qw(Jifty::Model::Metadata Jifty::Model::ModelClass Jifty::Model::ModelClassColumn)) {
 
+        my $current_user = Jifty->app_class('CurrentUser')->new( _bootstrap => 1 );
 
         next unless $model->isa('Jifty::Record');
         my $collection = $model."Collection";
         Jifty::Util->require($collection);
-        my $records = $collection->new;
+        my $records = $collection->new( current_user => $current_user );
         $records->unlimit();
 
         foreach my $item ( @{ $records->items_array_ref } ) {
@@ -274,4 +281,35 @@
     return $content;
 }
 
+=head2 dump_as_perl
+
+Outputs the data into a Jifty-ized Perl format. This is great for building operations to fill a test database from one you've already built up.
+
+=cut
+
+sub dump_as_perl {
+    my ($self, $content) = @_;
+
+    print 'my $record;',"\n";
+    for my $model (keys %$content) {
+        print "\$record = $model\->new;\n\n";
+
+        my $records = $content->{$model};
+        for my $uuid (keys %$records) {
+            my $columns = $records->{$uuid};
+
+            print "\$record->create(\n";
+            print "    __uuid => '$uuid',\n";
+
+            for my $column (%$columns) {
+                if (defined $columns->{$column}) {
+                    print "    $column => '$columns->{$column}',\n";
+                }
+            }
+
+            print ");\n\n";
+        }
+    }
+}
+
 1;

Modified: jifty/branches/virtual-models/lib/Jifty/Upgrade/Internal.pm
==============================================================================
--- jifty/branches/virtual-models/lib/Jifty/Upgrade/Internal.pm	(original)
+++ jifty/branches/virtual-models/lib/Jifty/Upgrade/Internal.pm	Mon Feb 26 18:14:28 2007
@@ -60,7 +60,10 @@
     Jifty->handle->begin_transaction;
     Jifty->handle->bootstrap_uuid_table;
 
-    # XXX - Generate UUID for _existing_ rows
+    # Generate UUID for _existing_ rows
+    if (Jifty->config->framework('Database')->{'RecordUUIDs'} !~ /^(?:lazy|off)$/i) {
+        Jifty->handle->insert_uuids_for_existing_rows;
+    }
 
     Jifty->handle->commit;
 };


More information about the Jifty-commit mailing list