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

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Fri Feb 9 14:50:21 EST 2007


Author: jesse
Date: Fri Feb  9 14:50:20 2007
New Revision: 2773

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

Log:
 r21683 at hualien:  jesse | 2007-01-29 09:59:37 -0500
 * actually let you create model classes from loaded dumps


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	Fri Feb  9 14:50:20 2007
@@ -50,27 +50,40 @@
     my @content = <STDIN>;
     my $content = Jifty::YAML::Load(join('', at content));
     print Jifty::YAML::Dump($content)."\n";
+
+    $self->_load_data($content);
+}
+
+sub _load_data {
+    my $self = shift;
+    my $content = shift;
     Jifty->handle->begin_transaction();
     # First the core stuff
+    
+    $self->upgrade_schema($content->{'Jifty::Model::ModelClass'}, $content->{'Jifty::Model::ModelClassColumn'});
+    
     foreach my $class (grep { /^Jifty::Model/ } keys %$content) { 
         next if ($class =~ /^Jifty::Model::ModelClass(?:Column)?/); 
-        $self->load_content($class => $content->{$class});
+        $self->load_content_for_class($class => $content->{$class});
     }
     # Then the user stuff
     foreach my $class (grep {! /^Jifty::Model/ } keys %$content) { 
-        $self->load_content($class => $content->{$class});
+        $self->load_content_for_class($class => $content->{$class});
     }
+    Jifty->handle->commit;
 }
 
-sub load_content {
+sub load_content_for_class {
     my $self    = shift;
     my $class   = shift;
     my $content = shift;
-    Jifty::Util->require($class)
-        || Jifty->logger->log->fatal(
-        "There's no locally defined class called $class. Without that, we can't insert records into it"
-        );
+    local $@;
+    eval {Jifty::Util->require($class)};
 
+        if ($@)  { Jifty->logger->log->fatal(
+        "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 );
     foreach my $id ( sort keys %$content ) {
         my $obj = $class->new( current_user => $current_user );
@@ -99,6 +112,7 @@
     my $self           = shift;
     my $new_tables     = shift;
     my $columns        = shift;
+
     my $current_tables = Jifty::Model::ModelClassCollection->new();
     $current_tables->unlimit();
     while ( my $table = $current_tables->next ) {
@@ -154,8 +168,8 @@
         }
 
         # 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 {
@@ -166,11 +180,12 @@
         delete $table->{id};
         my $class = Jifty::Model::ModelClass->new();
         my ( $val, $msg ) = $class->create( %{$table} );
-
+        die $msg 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) {
             delete $col->{id};
+            $col->{model_class} = $class;
             Jifty::Model::ModelClassColumn->create(%$col);
         }
     }
@@ -181,7 +196,15 @@
 
 sub dump {
     my $self = shift;
-    my $content = {};
+    my $content = $self->_models_to_hash();
+    
+    print Jifty::YAML::Dump($content)."\n";
+    
+}
+
+sub _models_to_hash {
+        my $self = shift;
+        my $content = {};
  foreach my $model (Jifty->class_loader->models, qw(Jifty::Model::Metadata Jifty::Model::ModelClass Jifty::Model::ModelClassColumn)) {
         next unless $model->isa('Jifty::Record');
         my $collection = $model."Collection";
@@ -212,8 +235,7 @@
 
 
     }
-    print Jifty::YAML::Dump($content)."\n";
-    
+    return $content;
 }
 
 1;

Modified: jifty/branches/virtual-models/t/TestApp-DatabaseBackedModels/lib/TestApp/DatabaseBackedModels/Bootstrap.pm
==============================================================================
--- jifty/branches/virtual-models/t/TestApp-DatabaseBackedModels/lib/TestApp/DatabaseBackedModels/Bootstrap.pm	(original)
+++ jifty/branches/virtual-models/t/TestApp-DatabaseBackedModels/lib/TestApp/DatabaseBackedModels/Bootstrap.pm	Fri Feb  9 14:50:20 2007
@@ -11,8 +11,11 @@
 
     my $col = Jifty::Model::ModelClassColumn->new(current_user => $user);
     $col->create( name => 'name', label => 'Name', storage_type => 'text', hints => 'This is the widget name', model_class => $modelclass);
-    #   my $col2 = Jifty::Model::ModelClassColumn->new(current_user => $user);
-    #$col2->create( name => 'inventory', label => 'Inventory', storage_type => 'int', hints => 'How many do we have on hand?', model_class => $modelclass);
+       my $col2 = Jifty::Model::ModelClassColumn->new(current_user => $user);
+    $col2->create( name => 'inventory', label => 'Inventory', storage_type => 'int', hints => 'How many do we have on hand?', model_class => $modelclass);
+
+    my $widget = TestApp::DatabaseBackedModels::Model::Widget->new(current_user => $user);
+    $widget->create(name => 'Weeble', inventory => 6);
 
 };
 

Added: jifty/branches/virtual-models/t/TestApp-DatabaseBackedModels/t/dump_load.t
==============================================================================
--- (empty file)
+++ jifty/branches/virtual-models/t/TestApp-DatabaseBackedModels/t/dump_load.t	Fri Feb  9 14:50:20 2007
@@ -0,0 +1,67 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+
+=head1 DESCRIPTION
+
+This is a template for your own tests. Copy it and modify it.
+
+=cut
+
+use lib 't/lib';
+use Jifty::SubTest;
+
+use Jifty::Test tests => 10;
+
+
+
+ok(1, "Loaded the test script");
+Jifty->new();
+use_ok('Jifty::Script::Database');
+can_ok('TestApp::DatabaseBackedModels::Model::Widget', 'new');
+
+my $DATABASE = Jifty::Script::Database->new();
+
+my $dumped_bootstrap = $DATABASE->_models_to_hash();
+
+my @keys = sort keys %$dumped_bootstrap;
+
+is_deeply(\@keys, [qw/Jifty::Model::Metadata 
+                    Jifty::Model::ModelClass 
+                    Jifty::Model::ModelClassColumn
+                    TestApp::DatabaseBackedModels::Model::Widget
+                    /]);
+
+my $dumped_widgets = $dumped_bootstrap->{'TestApp::DatabaseBackedModels::Model::Widget'};
+is (scalar keys %$dumped_widgets, 1);
+my @values = values %$dumped_widgets;
+my $widget = shift @values;
+is ($widget->{'name'}, 'Weeble', "Dumped data is as expected");
+
+
+my %new_widgets = ( 'TestApp::DatabaseBackedModels::Model::Widget' => { 
+    2 => { name => 'Fleeble', inventory => 5 },
+    3 => { name => 'Greeble', inventory => 0 },
+}
+);
+
+$DATABASE->_load_data(\%new_widgets);
+my $widgets = TestApp::DatabaseBackedModels::Model::WidgetCollection->new();
+$widgets->unlimit();
+is ($widgets->count,3, "We've loaded our two new widgets");
+
+
+my %new_tables =  ( 'Jifty::Model::ModelClass' => 
+    {
+        'cb05ac8c-afa5-11db-b33d-6f303ef246eb' => { name => 'Customer', description => 'People who pay us' },
+        'cb05ae08-afa5-11db-b33e-bbf514ba3e02' => { name => 'Vendor', description => 'People we avoid paying'} 
+
+    }
+);
+$DATABASE->_load_data(\%new_tables);
+can_ok('TestApp::DatabaseBackedModels::Model::Customer', 'new');
+can_ok('TestApp::DatabaseBackedModels::Model::Customer', 'create');
+isa_ok(TestApp::DatabaseBackedModels::Model::Customer->new(), 'Jifty::DBI::Record');
+
+


More information about the Jifty-commit mailing list