[Jifty-commit] r2828 - in jifty/trunk: . lib lib/Jifty lib/Jifty/Script t/TestApp/lib/TestApp t/TestApp/lib/TestApp/Model t/TestApp/t

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Fri Feb 23 15:33:42 EST 2007


Author: sterling
Date: Fri Feb 23 15:33:41 2007
New Revision: 2828

Added:
   jifty/trunk/t/TestApp/lib/TestApp/Upgrade.pm
   jifty/trunk/t/TestApp/t/upgrade.t   (contents, props changed)
Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty.pm
   jifty/trunk/lib/Jifty/Model/Metadata.pm
   jifty/trunk/lib/Jifty/Record.pm
   jifty/trunk/lib/Jifty/Script/Schema.pm
   jifty/trunk/lib/Jifty/Upgrade.pm
   jifty/trunk/t/TestApp/lib/TestApp/Model/User.pm

Log:
 * Added support for schema_version() in records
 * Updated the schema upgrade process to handle renames more nicely
 * Added a simple test for upgrading

Modified: jifty/trunk/lib/Jifty.pm
==============================================================================
--- jifty/trunk/lib/Jifty.pm	(original)
+++ jifty/trunk/lib/Jifty.pm	Fri Feb 23 15:33:41 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.70117';
+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.70117'; 
+}
 
 =head1 NAME
 

Modified: jifty/trunk/lib/Jifty/Model/Metadata.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Model/Metadata.pm	(original)
+++ jifty/trunk/lib/Jifty/Model/Metadata.pm	Fri Feb 23 15:33:41 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/trunk/lib/Jifty/Record.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Record.pm	(original)
+++ jifty/trunk/lib/Jifty/Record.pm	Fri Feb 23 15:33:41 2007
@@ -3,6 +3,8 @@
 
 package Jifty::Record;
 
+use Jifty::Config;
+
 =head1 NAME
 
 Jifty::Record - Represents a Jifty object that lives in the database.
@@ -485,5 +487,48 @@
     return "ALTER TABLE " . $self->table . " DROP COLUMN " . $col->name;
 }
 
+=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/trunk/lib/Jifty/Script/Schema.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Script/Schema.pm	(original)
+++ jifty/trunk/lib/Jifty/Script/Schema.pm	Fri Feb 23 15:33:41 2007
@@ -372,13 +372,31 @@
             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 ($model->can( 'since' ) and defined $col->since and $appv >= $col->since and $col->since >$dbv ) {
-                    unshift @{ $UPGRADES{ $col->since } }, $model->add_column_sql($col->name);
+                    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 };
+                    };
                 }
             }
         }

Modified: jifty/trunk/lib/Jifty/Upgrade.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Upgrade.pm	(original)
+++ jifty/trunk/lib/Jifty/Upgrade.pm	Fri Feb 23 15:33:41 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/trunk/t/TestApp/lib/TestApp/Model/User.pm
==============================================================================
--- jifty/trunk/t/TestApp/lib/TestApp/Model/User.pm	(original)
+++ jifty/trunk/t/TestApp/lib/TestApp/Model/User.pm	Fri Feb 23 15:33:41 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/trunk/t/TestApp/lib/TestApp/Upgrade.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/t/TestApp/lib/TestApp/Upgrade.pm	Fri Feb 23 15:33:41 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/trunk/t/TestApp/t/upgrade.t
==============================================================================
--- (empty file)
+++ jifty/trunk/t/TestApp/t/upgrade.t	Fri Feb 23 15:33:41 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