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

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Fri Feb 23 18:02:13 EST 2007


Author: sterling
Date: Fri Feb 23 18:02:13 2007
New Revision: 2834

Added:
   jifty/branches/virtual-models/t/TestApp/lib/TestApp/Upgrade.pm
   jifty/branches/virtual-models/t/TestApp/t/upgrade.t   (contents, props changed)
Modified:
   jifty/branches/virtual-models/   (props changed)
   jifty/branches/virtual-models/AUTHORS
   jifty/branches/virtual-models/Makefile.PL
   jifty/branches/virtual-models/lib/Jifty.pm
   jifty/branches/virtual-models/lib/Jifty/Model/Metadata.pm
   jifty/branches/virtual-models/lib/Jifty/Record.pm
   jifty/branches/virtual-models/lib/Jifty/Script/Schema.pm
   jifty/branches/virtual-models/lib/Jifty/Upgrade.pm
   jifty/branches/virtual-models/t/TestApp/lib/TestApp/Model/User.pm

Log:
merged latest trunk into v-m; no new problems in testing

Modified: jifty/branches/virtual-models/AUTHORS
==============================================================================
--- jifty/branches/virtual-models/AUTHORS	(original)
+++ jifty/branches/virtual-models/AUTHORS	Fri Feb 23 18:02:13 2007
@@ -22,3 +22,5 @@
 Agent Zhang <agentzh at gmail.com>
 Pawel Murias <pmurias at woobling.org>
 Andrew Sterling Hanenkamp <sterling at hanenkamp.com>
+Edgar Whipple <jifty at misterwhipple.com>
+Christian Ternus <ternus at mit.edu>

Modified: jifty/branches/virtual-models/Makefile.PL
==============================================================================
--- jifty/branches/virtual-models/Makefile.PL	(original)
+++ jifty/branches/virtual-models/Makefile.PL	Fri Feb 23 18:02:13 2007
@@ -73,6 +73,9 @@
 if (can_cc()) {
     # Always require the Syck bindings if a C compiler is available
     requires('YAML::Syck' => 0.82);
+    requires('YAML' => 0.35); 	# Use YAML::Dump for the moment since YAML.pm segfaults on
+				# reading stupidly long (~20K characters) double-quoted
+				# strings, and we need to produce YAML.pm-readable output.
     requires('JSON::Syck' => 0.15);
 }
 else {

Modified: jifty/branches/virtual-models/lib/Jifty.pm
==============================================================================
--- jifty/branches/virtual-models/lib/Jifty.pm	(original)
+++ jifty/branches/virtual-models/lib/Jifty.pm	Fri Feb 23 18:02:13 2007
@@ -5,9 +5,14 @@
 use IPC::PubSub 0.22;
 use Data::UUID;
 use encoding 'utf8';
-# Work around the fact that Time::Local caches thing on first require
-BEGIN { local $ENV{'TZ'} = "GMT";  require Time::Local;}
-$Jifty::VERSION = '0.70129';
+BEGIN { 
+    # Work around the fact that Time::Local caches TZ on first require
+    local $ENV{'TZ'} = "GMT";
+    require Time::Local;
+
+    # Declare early to make sure Jifty::Record::schema_version works
+    $Jifty::VERSION = '0.70129';
+}
 
 =head1 NAME
 

Modified: jifty/branches/virtual-models/lib/Jifty/Model/Metadata.pm
==============================================================================
--- jifty/branches/virtual-models/lib/Jifty/Model/Metadata.pm	(original)
+++ jifty/branches/virtual-models/lib/Jifty/Model/Metadata.pm	Fri Feb 23 18:02:13 2007
@@ -14,7 +14,7 @@
 
 Every Jifty application automatically inherits this table, which
 describes information about the Jifty database.  It uses this
-information to smartly upgrade between application versions, as well
+information to smartly upgrade between application schema versions, as well
 as versions of Jifty itself, for instance.
 
 =cut

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 23 18:02:13 2007
@@ -3,6 +3,8 @@
 
 package Jifty::Record;
 
+use Jifty::Config;
+
 =head1 NAME
 
 Jifty::Record - Represents a Jifty object that lives in the database.
@@ -406,7 +408,7 @@
 
 =head2 since
  
-By default, all models exist since C<undef>, the ur-time when the application was created. Please override it for your midel class.
+By default, all models exist since C<undef>, the ur-time when the application was created. Please override it for your model class.
  
 =cut
  
@@ -466,14 +468,14 @@
 }
 
 sub _make_schema { 
-        my $class = shift;
+    my $class = shift;
 
     my $schema_gen = Jifty::DBI::SchemaGenerator->new( Jifty->handle )
         or die "Can't make Jifty::DBI::SchemaGenerator";
     my $ret = $schema_gen->add_model( $class->new );
     $ret or die "couldn't add model $class: " . $ret->error_message;
 
-        return $schema_gen;
+    return $schema_gen;
 }
 
 =head2 add_column_sql column_name
@@ -518,5 +520,48 @@
     return $self->_handle->lookup_uuid($table, $id);
 }
 
+=head2 schema_version
+
+This method is used by L<Jifty::DBI::Record> to determine which schema version is in use. It returns the current database version stored in the configuration.
+
+Jifty's notion of the schema version is currently broken into two:
+
+=over
+
+=item 1.
+
+The Jifty version is the first. In the case of models defined by Jifty itself, these use the version found in C<$Jifty::VERSION>.
+
+=item 2.
+
+Any model defined by your application use the database version declared in the configuration. In F<etc/config.yml>, this is lcoated at:
+
+  framework:
+    Database:
+      Version: 0.0.1
+
+=back
+
+A model is considered to be defined by Jifty if it the package name starts with "Jifty::". Otherwise, it is assumed to be an application model.
+
+=cut
+
+sub schema_version {
+    my $class = shift;
+    
+    # Return the Jifty schema version
+    if ($class =~ /^Jifty::Model::/) {
+        return $Jifty::VERSION;
+    }
+
+    # TODO need to consider Jifty plugin versions?
+
+    # Return the application schema version
+    else {
+        my $config = Jifty::Config->new;
+        return $config->framework('Database')->{'Version'};
+    }
+}
+
 1;
 

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	Fri Feb 23 18:02:13 2007
@@ -356,7 +356,7 @@
     eval {
         $UPGRADES{$_} = [ $upgradeclass->upgrade_to($_) ]
             for grep { $appv >= version->new($_) and $dbv < version->new($_) }
-            $upgradeclass->versions();
+                     $upgradeclass->versions();
     };
 
     for my $model_class ( grep {/^\Q$baseclass\E::Model::/} __PACKAGE__->models ) {
@@ -368,20 +368,38 @@
         my $model = $model_class->new;
 
         # If this whole table is new Create it
-        if (defined $model->since and  $appv >= $model->since and $model->since >$dbv ) {
+        if ($model->can( 'since' ) and defined $model->since and  $appv >= $model->since and $model->since >$dbv ) {
             unshift @{ $UPGRADES{ $model->since } }, $model->printable_table_schema();
         } else {
             # Go through the columns
             for my $col  (grep {not $_->virtual} $model->columns ) {
 
                 # If they're old, drop them
-               if ( defined $col->till and $appv >= $col->till and $ $col->till > $dbv ) {
-                   push @{ $UPGRADES{ $col->till } }, $model->drop_column_sql($col->name);
+                if ( defined $col->till and $appv >= $col->till and $col->till > $dbv ) {
+                    push @{ $UPGRADES{ $col->till } }, sub {
+                        my $renamed = $upgradeclass->just_renamed || {};
+
+                        # skip it if this was dropped by a rename
+                        $model->drop_column_sql($col->name)
+                            unless defined $renamed
+                                ->{ $model->table }
+                                ->{'drop'}
+                                ->{ $col->name };
+                    };
                 }
 
                 # If they're new, add them
-                if (defined $col->since and $appv >= $col->since and $col->since >$dbv ) {
-                    unshift @{ $UPGRADES{ $col->since } }, $model->add_column_sql($col->name);
+                if ($model->can( 'since' ) and defined $col->since and $appv >= $col->since and $col->since >$dbv ) {
+                    unshift @{ $UPGRADES{ $col->since } }, sub {
+                        my $renamed = $upgradeclass->just_renamed || {};
+
+                        # skip it if this was added by a rename
+                        $model->add_column_sql($col->name)
+                            unless defined $renamed
+                                ->{ $model->table }
+                                ->{'add'}
+                                ->{ $col->name };
+                    };
                 }
             }
         }
@@ -391,9 +409,11 @@
         $self->_print_upgrades(%UPGRADES);
 
     } else {
-       eval { $self->_execute_upgrades(%UPGRADES);
-        $log->info("Upgraded to version $appv");
-    }; die $@ if $@;
+        eval { 
+            $self->_execute_upgrades(%UPGRADES);
+            $log->info("Upgraded to version $appv");
+        }; 
+        die $@ if $@;
     }
     return 1;
 }

Modified: jifty/branches/virtual-models/lib/Jifty/Upgrade.pm
==============================================================================
--- jifty/branches/virtual-models/lib/Jifty/Upgrade.pm	(original)
+++ jifty/branches/virtual-models/lib/Jifty/Upgrade.pm	Fri Feb 23 18:02:13 2007
@@ -14,10 +14,12 @@
 
 package Jifty::Upgrade;
 
-use base qw/Jifty::Object Exporter/;
+use base qw/Jifty::Object Exporter Class::Data::Inheritable/;
 use vars qw/%UPGRADES @EXPORT/;
 @EXPORT = qw/since rename/;
 
+__PACKAGE__->mk_classdata('just_renamed');
+
 =head2 since I<VERSION> I<SUB>
 
 C<since> is meant to be called by subclasses of C<Jifty::Upgrade>.
@@ -82,6 +84,9 @@
     Jifty::Util->require( $args{table} );
     my $table_name = $args{table}->table;
 
+    my $package = (caller)[0];
+    my $renamed = $package->just_renamed || {};
+
     if ( $args{column} ) {
         my $driver = Jifty->config->framework('Database')->{'Driver'};
         if ( $driver =~ /SQLite/ ) {
@@ -130,10 +135,21 @@
         else {
             Jifty->handle->simple_query("ALTER TABLE $table_name RENAME $args{column} TO $args{to}");
         }
+
+        # Mark this table column as renamed
+        $renamed->{ $table_name }{'drop'}{ $args{'column'} } = $args{'to'};
+        $renamed->{ $table_name }{'add' }{ $args{'to'    } } = $args{'column'};
     }
     else {
         Jifty->handle->simple_query("ALTER TABLE $table_name RENAME TO $args{to}");
+
+        # Mark this table as renamed
+        $renamed->{ $table_name }{'drop_table'} = $args{'to'};
+        $renamed->{ $args{'to'} }{'add_table' } = $table_name;
     }
+
+    # Remember renames so that adds/drops are canceled
+    $package->just_renamed($renamed);
 }
 
 1;

Modified: jifty/branches/virtual-models/t/TestApp/lib/TestApp/Model/User.pm
==============================================================================
--- jifty/branches/virtual-models/t/TestApp/lib/TestApp/Model/User.pm	(original)
+++ jifty/branches/virtual-models/t/TestApp/lib/TestApp/Model/User.pm	Fri Feb 23 18:02:13 2007
@@ -14,9 +14,14 @@
 column 'email' =>
   type is 'text',
   is mandatory;
+column 'really_tasty' =>
+  type is 'boolean',
+  is immutable,
+  since '0.0.2';
 column 'tasty' =>
   type is 'boolean',
-  is immutable;
+  is immutable,
+  till '0.0.2';
 column 'password' =>
   type is 'text',
   render_as 'Password',

Added: jifty/branches/virtual-models/t/TestApp/lib/TestApp/Upgrade.pm
==============================================================================
--- (empty file)
+++ jifty/branches/virtual-models/t/TestApp/lib/TestApp/Upgrade.pm	Fri Feb 23 18:02:13 2007
@@ -0,0 +1,16 @@
+use strict;
+use warnings;
+
+package TestApp::Upgrade;
+
+use base qw/ Jifty::Upgrade /;
+use Jifty::Upgrade;
+
+since '0.0.2' => sub {
+    rename
+        table  => 'TestApp::Model::User',
+        column => 'tasty',
+        to     => 'really_tasty';
+};
+
+1;

Added: jifty/branches/virtual-models/t/TestApp/t/upgrade.t
==============================================================================
--- (empty file)
+++ jifty/branches/virtual-models/t/TestApp/t/upgrade.t	Fri Feb 23 18:02:13 2007
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+use lib 't/lib';
+
+use Jifty::SubTest;
+use Log::Log4perl;
+use Jifty::Test tests => 1;
+
+my $config = Jifty::YAML::LoadFile($ENV{JIFTY_TEST_CONFIG});
+$config->{'framework'}->{'Database'}->{'Version'} = '0.0.2';
+Jifty::YAML::DumpFile($ENV{JIFTY_TEST_CONFIG}, $config);
+
+my $logger = Log::Log4perl->get_logger('SchemaTool');
+$logger->add_appender(
+    my $test_appender = Log::Log4perl::Appender->new(
+        'Log::Log4perl::Appender::String',
+        name      => 'Test',
+        min_level => 'WARN',
+        layout    => 'Log::Log4perl::Layout::SimpleLayout',
+    )
+);
+
+my $schema = Jifty::Script::Schema->new;
+$schema->{setup_tables} = 1;
+$schema->run;
+
+my $failed_messages = $test_appender->string;
+ok(!$failed_messages, 'no warnings or worse');


More information about the Jifty-commit mailing list