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

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Mon Jan 29 02:52:05 EST 2007


Author: jesse
Date: Mon Jan 29 02:52:03 2007
New Revision: 2703

Added:
   jifty/branches/virtual-models/t/TestApp-DatabaseBackedModels/t/upgrades.t
Modified:
   jifty/branches/virtual-models/   (props changed)
   jifty/branches/virtual-models/lib/Jifty/Model/ModelClass.pm
   jifty/branches/virtual-models/lib/Jifty/Model/ModelClassColumn.pm
   jifty/branches/virtual-models/lib/Jifty/Script/Schema.pm
   jifty/branches/virtual-models/t/TestApp-DatabaseBackedModels/lib/TestApp/DatabaseBackedModels/Bootstrap.pm
   jifty/branches/virtual-models/t/TestApp-DatabaseBackedModels/t/01-insert.t

Log:
 r21650 at hualien:  jesse | 2007-01-29 15:51:42 +0800
 * switch database and column creation to "immediate"
 


Modified: jifty/branches/virtual-models/lib/Jifty/Model/ModelClass.pm
==============================================================================
--- jifty/branches/virtual-models/lib/Jifty/Model/ModelClass.pm	(original)
+++ jifty/branches/virtual-models/lib/Jifty/Model/ModelClass.pm	Mon Jan 29 02:52:03 2007
@@ -45,6 +45,23 @@
 sub since {'0.70127'}
 
 
+=head2 after_create
+
+After create, instantiate our new class and create its database schema.
+
+=cut
+
+
+sub after_create {
+    my $self = shift;
+    my $idref = shift;
+    $self->load_by_cols(id=>$$idref);
+    $self->instantiate();
+    $self->qualified_class->create_table_in_db();
+
+}
+
+
 =head2 qualified_class
 
 Returns the fully qualified class name of the model class described the current ModelClass object.
@@ -72,20 +89,21 @@
     $self->qualified_class->_init_columns();
     my $cols = $self->included_columns;
     while (my $col = $cols->next) {
-        my $column = Jifty::DBI::Column->new({ name => $col->name } );
-        $self->qualified_class->COLUMNS->{$col->name} = $column;
-        for (qw(readable writable hints indexed max_length render_as mandatory)) {
-            $column->$_( $col->$_());
-        }
-        $column->type($col->storage_type);
-
-
-        $self->qualified_class->_init_methods_for_column($column) 
+        $self->add_column($col);
     }
-
     return 1;
 }
 
+sub add_column {
+    my $self = shift;
+    my $col = shift;
+    my $column =$self->qualified_class->add_column($col->name);
+    for (qw(readable writable hints indexed max_length render_as mandatory)) {
+        $column->$_( $col->$_() );
+    }
+    $column->type( $col->storage_type );
+    $self->qualified_class->_init_methods_for_column($column);
+}
 
 
 sub _instantiate_stub_class {

Modified: jifty/branches/virtual-models/lib/Jifty/Model/ModelClassColumn.pm
==============================================================================
--- jifty/branches/virtual-models/lib/Jifty/Model/ModelClassColumn.pm	(original)
+++ jifty/branches/virtual-models/lib/Jifty/Model/ModelClassColumn.pm	Mon Jan 29 02:52:03 2007
@@ -47,6 +47,15 @@
 };
 
 
+sub after_create {
+    my $self = shift;
+    my $idref = shift;
+    $self->load_by_cols(id => $$idref);
+    $self->model_class->add_column($self);
+
+    my $ret = Jifty->handle->simple_query( $self->model_class->qualified_class->add_column_sql( $self->name ) );
+    $ret or die "error updating a table: " . $ret->error_message;
+}
 
 =head2 table
 

Modified: jifty/branches/virtual-models/lib/Jifty/Script/Schema.pm
==============================================================================
--- jifty/branches/virtual-models/lib/Jifty/Script/Schema.pm	(original)
+++ jifty/branches/virtual-models/lib/Jifty/Script/Schema.pm	Mon Jan 29 02:52:03 2007
@@ -216,17 +216,11 @@
         my $bootstrapper = Jifty->app_class("Bootstrap");
         Jifty::Util->require($bootstrapper);
         $bootstrapper->run() if $bootstrapper->can('run');
-        my $virtual_classes = Jifty::Model::ModelClassCollection->new();
-        $virtual_classes->unlimit();
-        $virtual_classes->instantiate();
-        $self->create_tables_for_models(map {$_->qualified_class} @{$virtual_classes->items_array_ref});
 
     };
     die $@ if $@;
-
     # Commit it all
     Jifty->handle->commit;
-
     Jifty::Util->require('IPC::PubSub');
     IPC::PubSub->new(
         JiftyDBI => (

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	Mon Jan 29 02:52:03 2007
@@ -10,8 +10,8 @@
 
     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);
 
 };
 

Modified: jifty/branches/virtual-models/t/TestApp-DatabaseBackedModels/t/01-insert.t
==============================================================================
--- jifty/branches/virtual-models/t/TestApp-DatabaseBackedModels/t/01-insert.t	(original)
+++ jifty/branches/virtual-models/t/TestApp-DatabaseBackedModels/t/01-insert.t	Mon Jan 29 02:52:03 2007
@@ -12,7 +12,7 @@
 use lib 't/lib';
 use Jifty::SubTest;
 
-use Jifty::Test tests => 24;
+use Jifty::Test tests => 23;
 
 ok(1, "Loaded the test script");
 
@@ -55,15 +55,6 @@
 
 ok($col3->id, "Got column ".$col2->id);
 
-ok($model->instantiate);
-use Jifty::DBI::SchemaGenerator;
-foreach my $statement ( Jifty::DBI::SchemaGenerator->new( Jifty->handle )
-    ->_db_schema_table_from_model( $model->qualified_class )
-    ->sql_create_table( Jifty->handle->dbh ) ) {
-    my $ret = Jifty->handle->simple_query($statement);
-    $ret or die "error creating a table: " . $ret->error_message;
-
-}
 {
 my $object = TestApp::DatabaseBackedModels::Model::Object->new(current_user => $u);
 isa_ok($object, 'TestApp::DatabaseBackedModels::Model::Object');

Added: jifty/branches/virtual-models/t/TestApp-DatabaseBackedModels/t/upgrades.t
==============================================================================
--- (empty file)
+++ jifty/branches/virtual-models/t/TestApp-DatabaseBackedModels/t/upgrades.t	Mon Jan 29 02:52:03 2007
@@ -0,0 +1,125 @@
+#!/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 => 23;
+
+ok(1, "Loaded the test script");
+
+
+my $u = Jifty::CurrentUser->new(_bootstrap => 1);
+{
+eval { my $model = TestApp::DatabaseBackedModels::Model::Object->new(current_user => $u); };
+my $result = $@;
+ok($result, "Failed to instantiate an 'object' model before running our tests");
+}
+my $model = Jifty::Model::ModelClass->new(current_user => $u);
+$model->create(name => 'Object', description =>'You know. like widgets');
+ok($model->id);
+
+my $col = Jifty::Model::ModelClassColumn->new(current_user => $u);
+$col->create(name => 'name', 
+            model_class => $model,
+            storage_type => 'text',
+            label => 'Object name',
+            hints => q{What's it called, yo?});
+
+ok($col->id);
+my $col2 = Jifty::Model::ModelClassColumn->new(current_user => $u);
+$col2->create(name => 'inventory', 
+            model_class => $model,
+            storage_type => 'int',
+            label => 'Volume on hand',
+            hints => q{How many you gots?});
+
+ok($col2->id, "Got column ".$col2->id);
+
+my $col3 = Jifty::Model::ModelClassColumn->new(current_user => $u);
+$col3->create(name => 'password', 
+            model_class => $model,
+            storage_type => 'text',
+            render_as => 'Password',
+            readable => 0,
+            writable =>1
+            );
+
+ok($col3->id, "Got column ".$col2->id);
+
+
+{
+my $object = TestApp::DatabaseBackedModels::Model::Object->new(current_user => $u);
+isa_ok($object, 'TestApp::DatabaseBackedModels::Model::Object');
+can_ok($object, 'id');
+can_ok($object, 'create');
+can_ok($object, 'name');
+can_ok($object, 'set_name');
+can_ok($object, 'inventory');
+can_ok($object, 'password');
+can_ok($object, 'set_password');
+
+my $id = $object->create( name => 'Widget', inventory => '30', password => 'secret');
+ok($id, "Created id ".$id);
+is($id,$object->id);
+my $object_clone = TestApp::DatabaseBackedModels::Model::Object->new(current_user => $u);
+
+$object_clone->load($id);
+is($object_clone->name, 'Widget');
+is($object_clone->inventory, '30');
+is($object_clone->password,'');
+is($object_clone->__value('password'), 'secret');
+$object_clone->set_password('foo!');
+
+is($object_clone->__value('password'), 'foo!');
+
+$object_clone->set_inventory(40);
+is($object_clone->inventory, 40);
+$object_clone->set_name('Thingy');
+is($object_clone->name, 'Thingy');
+
+
+}
+
+
+
+# upgrade tests
+#
+# test that our bootstrapped models exist
+#
+# add a column to a bootstrapped model class (widgets)
+# can our widget objects now see the new column?
+# does the widgets table have the new column?
+# is the table's default correct?
+# do live widget objects magically have the new default?
+#
+# remove a column
+# do live objects lose that column?
+# do freshly loaded objects lose that column
+# do the column methods for the objects fail on the fly?
+
+
+
+# create a new modelclass
+# it instantiates the class
+# it autocreates the table in the database
+
+# add a column to the table
+# it updates the table in the database
+# it updates the model class
+
+# delete the new modelclass
+# it deletes the table in the database
+# it deletes the model class from memory
+# existing objects are ????
+
+1;
+


More information about the Jifty-commit mailing list