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

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Fri Feb 9 14:51:41 EST 2007


Author: jesse
Date: Fri Feb  9 14:51:41 2007
New Revision: 2774

Modified:
   jifty/branches/virtual-models/   (props changed)
   jifty/branches/virtual-models/lib/Jifty/Model/ModelClass.pm
   jifty/branches/virtual-models/lib/Jifty/Record.pm
   jifty/branches/virtual-models/lib/Jifty/Script.pm
   jifty/branches/virtual-models/lib/Jifty/Script/Database.pm
   jifty/branches/virtual-models/t/TestApp-DatabaseBackedModels/etc/config.yml
   jifty/branches/virtual-models/t/TestApp-DatabaseBackedModels/t/dump_load.t

Log:
 r21935 at hualien:  jesse | 2007-02-09 14:49:44 -0500
  * starting support for dropping non-matching tables and columns


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	Fri Feb  9 14:51:41 2007
@@ -73,6 +73,14 @@
 }
 
 
+sub delete {
+    my $self = shift;
+    # XXX TODO: remove all columns here.
+    $self->qualified_class->drop_table_in_db();
+    return $self->SUPER::delete(@_);
+}
+
+
 =head2 qualified_class
 
 Returns the fully qualified class name of the model class described the current ModelClass object.

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	Fri Feb  9 14:51:41 2007
@@ -447,6 +447,15 @@
 
 }
 
+sub drop_table_in_db {
+    my $class = shift;
+
+    my $statement = "DROP TABLE ".$class->table;
+        my $ret = Jifty->handle->simple_query($statement);
+        $ret or die "error creating table $class: " . $ret->error_message;
+
+}
+
 sub _make_schema { 
         my $class = shift;
 

Modified: jifty/branches/virtual-models/lib/Jifty/Script.pm
==============================================================================
--- jifty/branches/virtual-models/lib/Jifty/Script.pm	(original)
+++ jifty/branches/virtual-models/lib/Jifty/Script.pm	Fri Feb  9 14:51:41 2007
@@ -1,6 +1,6 @@
 package Jifty::Script;
 use App::CLI;
-use base qw/App::CLI App::CLI::Command/;
+use base qw/App::CLI App::CLI::Command Jifty::Object/;
 
 =head2 prepare
 

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:51:41 2007
@@ -2,7 +2,7 @@
 use strict;
 
 package Jifty::Script::Database;
-use base qw/App::CLI::Command/;
+use base qw/Jifty::Script/;
 
 
 use File::Path ();
@@ -49,7 +49,14 @@
     my $self = shift;
     my @content = <STDIN>;
     my $content = Jifty::YAML::Load(join('', at content));
-    print Jifty::YAML::Dump($content)."\n";
+    #print Jifty::YAML::Dump($content)."\n";
+
+    $self->_load_data($content);
+}
+
+sub _load_data {
+    my $self = shift;
+    my $content = shift;
 
     $self->_load_data($content);
 }
@@ -80,7 +87,7 @@
     local $@;
     eval {Jifty::Util->require($class)};
 
-        if ($@)  { Jifty->logger->log->fatal(
+        if ($@)  { $self->log->fatal(
         "There's no locally defined class called $class. Without that, we can't insert records into it: $@"
         );
     }
@@ -96,9 +103,9 @@
 
         my ( $val, $msg ) = $class->create( %{ $content->{$id} } );
         if ($val) {
-            Jifty->logger->log->info("Inserting $id into $class: $val");
+            $self->log->info("Inserting $id into $class: $val");
         } else {
-            Jifty->logger->log->fatal(
+            $self->log->fatal(
                 "Failed to insert $id into $class: $val");
 
         }
@@ -116,11 +123,14 @@
     my $current_tables = Jifty::Model::ModelClassCollection->new();
     $current_tables->unlimit();
     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");
 
             # we have the same table in the db and the dump
             # let's sync its attributes from the dump then sync its columns
             foreach my $key ( keys %$new_table ) {
+                $self->log->debug("Considering updating table attribute $key");
                 unless ( $table->$key() eq $new_table->{$key} ) {
                     my $method = "set_" . $key;
                     $table->$method( $new_table->{$key} );
@@ -161,6 +171,7 @@
             }
 
         } else {
+            $self->log->debug("The new datamodel doesn't have this table anymore. Deleting");
 
             # we don't have the table anymore. That means we should delete it.
             # XXX TODO: this automatically deletes all the columns
@@ -177,6 +188,7 @@
     my $new_tables = shift;
     my $columns    = shift;
     foreach my $table ( values %$new_tables ) {
+        $self->log->debug("Creating a new table: ".$table->{name});
         delete $table->{id};
         my $class = Jifty::Model::ModelClass->new();
         my ( $val, $msg ) = $class->create( %{$table} );

Modified: jifty/branches/virtual-models/t/TestApp-DatabaseBackedModels/etc/config.yml
==============================================================================
--- jifty/branches/virtual-models/t/TestApp-DatabaseBackedModels/etc/config.yml	(original)
+++ jifty/branches/virtual-models/t/TestApp-DatabaseBackedModels/etc/config.yml	Fri Feb  9 14:51:41 2007
@@ -15,7 +15,7 @@
   DevelMode: 1
   L10N: 
     PoDir: share/po
-  LogLevel: INFO
+  LogLevel: DEBUG
   Mailer: Sendmail
   MailerArgs: []
 

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	Fri Feb  9 14:51:41 2007
@@ -46,22 +46,58 @@
 }
 );
 
+
+
 $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');
+
+my $obj = TestApp::DatabaseBackedModels::Model::Customer->create();
+ok($obj->id, "Created a customer.");
+}
 
-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'} 
 
-    }
+{
+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' }
+    },
+    'Jifty::Model::ModelClassColumn' => {
+            'cb05ac8c-afa5-11db-b33d-6f303ef246ef' => 
+                { name => 'name', type => 'text', model_class => 'cb05ac8c-afa5-11db-b33d-6f303ef246eb', default_value => 'Cogswell Cogs' }
+    } 
 );
 $DATABASE->_load_data(\%new_tables);
 can_ok('TestApp::DatabaseBackedModels::Model::Customer', 'new');
 can_ok('TestApp::DatabaseBackedModels::Model::Customer', 'create');
+can_ok('TestApp::DatabaseBackedModels::Model::Customer', 'name');
+can_ok('TestApp::DatabaseBackedModels::Model::Customer', 'set_name');
 isa_ok(TestApp::DatabaseBackedModels::Model::Customer->new(), 'Jifty::DBI::Record');
+my $customers = TestApp::DatabaseBackedModels::Model::CustomerCollection->new;
+$customers->unlimit();
+is($customers->count(),1);
+is($customers->first->name, 'Cogswell Cogs');
+can_ok('TestApp::DatabaseBackedModels::Model::Vendor', 'new');
+can_ok('TestApp::DatabaseBackedModels::Model::Vendor', 'create');
+ok(!can('TestApp::DatabaseBackedModels::Model::Vendor', 'name'));
 
 
+}


More information about the Jifty-commit mailing list