[Jifty-commit] r2843 - in jifty/branches/template-declare: . lib lib/Jifty/Manual lib/Jifty/Model 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
Sun Feb 25 17:15:21 EST 2007


Author: jesse
Date: Sun Feb 25 17:15:20 2007
New Revision: 2843

Added:
   jifty/branches/template-declare/t/TestApp/lib/TestApp/Upgrade.pm
   jifty/branches/template-declare/t/TestApp/t/upgrade.t   (contents, props changed)
Modified:
   jifty/branches/template-declare/   (props changed)
   jifty/branches/template-declare/AUTHORS
   jifty/branches/template-declare/lib/Jifty.pm
   jifty/branches/template-declare/lib/Jifty/Handler.pm
   jifty/branches/template-declare/lib/Jifty/Logger.pm
   jifty/branches/template-declare/lib/Jifty/Manual/Tutorial.pod
   jifty/branches/template-declare/lib/Jifty/Model/Metadata.pm
   jifty/branches/template-declare/lib/Jifty/Notification.pm
   jifty/branches/template-declare/lib/Jifty/Record.pm
   jifty/branches/template-declare/lib/Jifty/Script/Schema.pm
   jifty/branches/template-declare/lib/Jifty/Upgrade.pm
   jifty/branches/template-declare/t/TestApp/lib/TestApp/Model/User.pm

Log:
 r48495 at 121 (orig r2825):  sterling | 2007-02-23 10:29:31 -0500
 tidied a couple of spots and fixed a typo
 r48500 at 121 (orig r2828):  sterling | 2007-02-23 15:33:41 -0500
  * Added support for schema_version() in records
  * Updated the schema upgrade process to handle renames more nicely
  * Added a simple test for upgrading
 r48518 at 121 (orig r2836):  falcone | 2007-02-25 16:35:24 -0500
  r16886 at ketch:  falcone | 2007-02-25 14:27:22 -0500
  * we use config.yml not jifty.yml
 
 r48519 at 121 (orig r2837):  falcone | 2007-02-25 16:35:32 -0500
  r16887 at ketch:  falcone | 2007-02-25 14:27:35 -0500
  * add myself
 
 r48520 at 121 (orig r2838):  falcone | 2007-02-25 16:35:46 -0500
  r16888 at ketch:  falcone | 2007-02-25 16:32:09 -0500
  * quiet a warning when id was undef in testing
 
 r48521 at 121 (orig r2839):  falcone | 2007-02-25 16:36:13 -0500
  r16889 at ketch:  falcone | 2007-02-25 16:33:57 -0500
  * DBD::pg passes postgres' warnings up, so try to convert their various
    logging levels back to Log4Perl levels.
    Completely heuristic, probably wants more guarding so it doesn't
    reach out and bite someone.
  
  * This quiets some of the most annoting warns revealed when I removed
    the log-level downing in Script/Schema.pm
 
 r48523 at 121 (orig r2840):  jesse | 2007-02-25 17:04:09 -0500
  r48514 at 121:  jesse | 2007-02-25 15:20:15 -0500
  * Plugin templates should override the core, but not application templates
 
 r48524 at 121 (orig r2841):  jesse | 2007-02-25 17:04:57 -0500
  r48515 at 121:  jesse | 2007-02-25 16:14:59 -0500
  * Since we use the schema tool to manage jifty's database, it's important that it be able to be called from running code (including a DESTROY block). As it was, we were initializing (reinitializing) Jifty from within the DESTROY. That doesn't work so well.
  
 
 r48525 at 121 (orig r2842):  jesse | 2007-02-25 17:06:31 -0500
  r48516 at 121:  jesse | 2007-02-25 16:17:30 -0500
  * Better test for "jifty already initted"
 


Modified: jifty/branches/template-declare/AUTHORS
==============================================================================
--- jifty/branches/template-declare/AUTHORS	(original)
+++ jifty/branches/template-declare/AUTHORS	Sun Feb 25 17:15:20 2007
@@ -21,6 +21,7 @@
 Yves Agostini <agostini at univ-metz.fr>
 Agent Zhang <agentzh at gmail.com>
 Pawel Murias <pmurias at woobling.org>
+Kevin Falcone <falcone at bestpractical.com>
 Andrew Sterling Hanenkamp <sterling at hanenkamp.com>
 Edgar Whipple <jifty at misterwhipple.com>
 Christian Ternus <ternus at mit.edu>

Modified: jifty/branches/template-declare/lib/Jifty.pm
==============================================================================
--- jifty/branches/template-declare/lib/Jifty.pm	(original)
+++ jifty/branches/template-declare/lib/Jifty.pm	Sun Feb 25 17:15:20 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/branches/template-declare/lib/Jifty/Handler.pm
==============================================================================
--- jifty/branches/template-declare/lib/Jifty/Handler.pm	(original)
+++ jifty/branches/template-declare/lib/Jifty/Handler.pm	Sun Feb 25 17:15:20 2007
@@ -145,6 +145,8 @@
     }
     push @{$config{comp_root}}, [jifty => Jifty->config->framework('Web')->{'DefaultTemplateRoot'}];
 
+    push @{ $config{comp_root} }, [jifty => Jifty->config->framework('Web')->{'DefaultTemplateRoot'}];
+
     # In developer mode, we want halos, refreshing and all that other good stuff. 
     if (Jifty->config->framework('DevelMode') ) {
         push @{$config{'plugins'}}, 'Jifty::Mason::Halo';

Modified: jifty/branches/template-declare/lib/Jifty/Logger.pm
==============================================================================
--- jifty/branches/template-declare/lib/Jifty/Logger.pm	(original)
+++ jifty/branches/template-declare/lib/Jifty/Logger.pm	Sun Feb 25 17:15:20 2007
@@ -118,10 +118,11 @@
         # If the logger has been taken apart by global destruction,
         # don't try to use it to log warnings
         if (Log::Log4perl->initialized) {
+            my $action = $self->_warning_action(@_);
             # @_ often has read-only scalars, so we need to break
             # the aliasing so we can remove trailing newlines
             my @lines = map {"$_"} @_;
-            $logger->warn(map {chomp; $_} @lines);
+            $logger->$action(map {chomp; $_} @lines);
         }
         elsif ($previous_warning_handler) {
             # Fallback to the old handler
@@ -162,6 +163,44 @@
   }
 }
 
+=head2 _warning_action
+
+change the Log4Perl action from warn to error|info|etc based 
+on the content of the warning.  
+
+Added because DBD::Pg throws up NOTICE and other messages
+as warns, and we really want those to be info (or error, depending
+on the code).  List based on Postgres documentation
+
+TODO: needs to be smarter than just string matching
+
+returns a valid Log::Log4Perl action, if nothing matches
+will return the default of warn since we're in a __WARN__ handler
+
+=cut
+
+sub _warning_action {
+    my $self = shift;
+    my $warnings = join('', at _);
+
+    my %pg_notices = ('DEBUG\d+' => 'debug',
+                      'INFO'     => 'info',
+                      'NOTICE'   => 'info',
+                      'WARNING'  => 'warn',
+                      'DBD::Pg.+ERROR'    => 'error',
+                      'LOG'      => 'warn',
+                      'FATAL'    => 'fatal',
+                      'PANIC'    => 'fatal' );
+
+    foreach my $notice (keys %pg_notices) {
+        if ($warnings =~ /^$notice:/) {
+            return $pg_notices{$notice};
+        }
+    }
+
+    return 'warn';
+}
+
 =head1 AUTHOR
 
 Various folks at Best Practical Solutions, LLC.

Modified: jifty/branches/template-declare/lib/Jifty/Manual/Tutorial.pod
==============================================================================
--- jifty/branches/template-declare/lib/Jifty/Manual/Tutorial.pod	(original)
+++ jifty/branches/template-declare/lib/Jifty/Manual/Tutorial.pod	Sun Feb 25 17:15:20 2007
@@ -239,7 +239,7 @@
 
 Ok. It's time to initialize MyWeblog's database. By default, Jifty sets up your
 application with the SQLite database engine.  If you'd rather use PostgreSQL or
-MySQL, you need to add some content to F<etc/jifty.yml>. (See L<Jifty::Config>
+MySQL, you need to add some content to F<etc/config.yml>. (See L<Jifty::Config>
 for a bit more information).
 
   # jifty schema --setup

Modified: jifty/branches/template-declare/lib/Jifty/Model/Metadata.pm
==============================================================================
--- jifty/branches/template-declare/lib/Jifty/Model/Metadata.pm	(original)
+++ jifty/branches/template-declare/lib/Jifty/Model/Metadata.pm	Sun Feb 25 17:15:20 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/template-declare/lib/Jifty/Notification.pm
==============================================================================
--- jifty/branches/template-declare/lib/Jifty/Notification.pm	(original)
+++ jifty/branches/template-declare/lib/Jifty/Notification.pm	Sun Feb 25 17:15:20 2007
@@ -207,6 +207,7 @@
     for my $to ( grep {defined} ($self->to, $self->to_list) ) {
         if ($to->can('id')) {
         next if     $currentuser_object_class->can("nobody")
+                and $currentuser_object_class->nobody->id
                 and $to->id == $currentuser_object_class->nobody->id;
                 
         next if $to->id == $currentuser_object_class->superuser->id;

Modified: jifty/branches/template-declare/lib/Jifty/Record.pm
==============================================================================
--- jifty/branches/template-declare/lib/Jifty/Record.pm	(original)
+++ jifty/branches/template-declare/lib/Jifty/Record.pm	Sun Feb 25 17:15:20 2007
@@ -3,6 +3,8 @@
 
 package Jifty::Record;
 
+use Jifty::Config;
+
 =head1 NAME
 
 Jifty::Record - Represents a Jifty object that lives in the database.
@@ -404,7 +406,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
  
@@ -446,14 +448,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
@@ -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/branches/template-declare/lib/Jifty/Script/Schema.pm
==============================================================================
--- jifty/branches/template-declare/lib/Jifty/Script/Schema.pm	(original)
+++ jifty/branches/template-declare/lib/Jifty/Script/Schema.pm	Sun Feb 25 17:15:20 2007
@@ -89,6 +89,7 @@
     # Import Jifty
     Jifty::Util->require("Jifty");
     Jifty::Util->require("Jifty::Model::Metadata");
+    Jifty->new( no_handle        => 1, logger_component => 'SchemaTool',) unless (Jifty->config);
 }
 
 =head2 print_help
@@ -148,10 +149,7 @@
 
     # Now try to connect.  We trap expected errors and deal with them.
     eval {
-        Jifty->new(
-            no_handle        => $no_handle,
-            logger_component => 'SchemaTool',
-        );
+        Jifty->setup_database_connection( no_handle        => $no_handle, logger_component => 'SchemaTool',);
     };
 
     if ( $@ =~ /doesn't match (application schema|running jifty) version/i ) {
@@ -353,7 +351,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 ) {
@@ -365,20 +363,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 };
+                    };
                 }
             }
         }
@@ -388,9 +404,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/template-declare/lib/Jifty/Upgrade.pm
==============================================================================
--- jifty/branches/template-declare/lib/Jifty/Upgrade.pm	(original)
+++ jifty/branches/template-declare/lib/Jifty/Upgrade.pm	Sun Feb 25 17:15:20 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/template-declare/t/TestApp/lib/TestApp/Model/User.pm
==============================================================================
--- jifty/branches/template-declare/t/TestApp/lib/TestApp/Model/User.pm	(original)
+++ jifty/branches/template-declare/t/TestApp/lib/TestApp/Model/User.pm	Sun Feb 25 17:15:20 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/template-declare/t/TestApp/lib/TestApp/Upgrade.pm
==============================================================================
--- (empty file)
+++ jifty/branches/template-declare/t/TestApp/lib/TestApp/Upgrade.pm	Sun Feb 25 17:15:20 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/template-declare/t/TestApp/t/upgrade.t
==============================================================================
--- (empty file)
+++ jifty/branches/template-declare/t/TestApp/t/upgrade.t	Sun Feb 25 17:15:20 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