[Jifty-commit] r3713 - in jifty/branches/autoversioning: lib/Jifty
lib/Jifty/Model lib/Jifty/Module lib/Jifty/Script t/TestApp/t
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Sun Jul 22 21:17:11 EDT 2007
Author: jesse
Date: Sun Jul 22 21:17:11 2007
New Revision: 3713
Modified:
jifty/branches/autoversioning/ (props changed)
jifty/branches/autoversioning/Makefile.PL
jifty/branches/autoversioning/lib/Jifty/Handle.pm
jifty/branches/autoversioning/lib/Jifty/Model/Metadata.pm
jifty/branches/autoversioning/lib/Jifty/Module/Pluggable.pm
jifty/branches/autoversioning/lib/Jifty/Record.pm
jifty/branches/autoversioning/lib/Jifty/Schema.pm
jifty/branches/autoversioning/lib/Jifty/Script/Schema.pm
jifty/branches/autoversioning/lib/Jifty/Upgrade.pm
jifty/branches/autoversioning/t/TestApp/t/16-autoversioning.t
Log:
r61125 at 134: jesse | 2007-07-22 15:27:12 -0700
* Wired up adding tables, removing tables, adding columns, removing columns on run.
* Record metadata about the current schema when we run.
* We're not yet handling changed columns
* I bet we want to unwire the removes and add a special jifty schema --flag for it
Modified: jifty/branches/autoversioning/Makefile.PL
==============================================================================
--- jifty/branches/autoversioning/Makefile.PL (original)
+++ jifty/branches/autoversioning/Makefile.PL Sun Jul 22 21:17:11 2007
@@ -26,6 +26,7 @@
requires('Email::MIME::Creator');
requires('Email::MIME::ContentType');
requires('Email::MIME::CreateHTML');
+requires('Email::MIME::Modifier');
requires('Email::Send' => '1.99_01'); # Email::Send::Jifty::Test
requires('Email::Simple');
requires('Email::Simple::Creator');
Modified: jifty/branches/autoversioning/lib/Jifty/Handle.pm
==============================================================================
--- jifty/branches/autoversioning/lib/Jifty/Handle.pm (original)
+++ jifty/branches/autoversioning/lib/Jifty/Handle.pm Sun Jul 22 21:17:11 2007
@@ -114,10 +114,55 @@
=cut
sub check_schema_version {
+ my $self = shift;
require Jifty::Model::Metadata;
# Application db version check
- {
+
+ $self->_check_app_version_in_db();
+ $self->_check_jifty_version_in_db();
+
+ # If there isn't a jifty version, the user needs to init their db
+
+ # If there isn't an application schema, then we know we came from a pre-versioned time.
+ # The user needs to run jifty schema --setup to update the database to the last numbered version they have
+ # That will write out an application schema.
+ #
+ # If there IS an application schema in the database, we can skip the stupid "run your own upgrade" bit and just call
+ # autoupgrade
+ require Jifty::Schema; # Require it now. we want lazy-load here
+ my $appschema = Jifty::Schema->new();
+ unless ($appschema->load_stored_schema) {
+ die "It looks like you haven't used this application since Jifty introduced automatic upgrades. Just this once, you'll need to run\n".
+ " jifty schema --setup\n".
+ "to load your application's current schema definition into the database.\n"
+ }
+ $appschema->autoupgrade_schema();
+
+
+
+
+
+}
+
+sub _check_jifty_version_in_db {
+ my $self =shift;
+ # If we got here, the application had a version (somehow) so
+ # this is an upgrade. If $dbv is undef, it's because it's
+ # from before when the _jifty_metadata table existed.
+ my $dbv
+ = version->new( Jifty::Model::Metadata->load("jifty_db_version")
+ || '0.60426' );
+ my $appv = version->new($Jifty::VERSION);
+ die
+ "Internal jifty schema version in database ($dbv) doesn't match running jifty version ($appv)\n"
+ . "Please run `bin/jifty schema --setup` to upgrade the database.\n"
+ unless $appv == $dbv;
+ }
+
+
+sub _check_app_version_in_db {
+ my $self = shift;
my $dbv = Jifty::Model::Metadata->load("application_db_version");
my $appv = Jifty->config->framework('Database')->{'Version'};
@@ -126,8 +171,7 @@
my @v;
eval {
local $SIG{__WARN__} = sub { };
- @v = Jifty->handle->fetch_result(
- "SELECT major, minor, rev FROM _db_version");
+ @v = Jifty->handle->fetch_result( "SELECT major, minor, rev FROM _db_version");
};
$dbv = join( ".", @v ) if @v == 3;
}
@@ -135,8 +179,7 @@
# It was also called the 'key' column, not the data_key column
eval {
local $SIG{__WARN__} = sub { };
- $dbv = Jifty->handle->fetch_result(
- "SELECT value FROM _jifty_metadata WHERE key = 'application_db_version'");
+ $dbv = Jifty->handle->fetch_result( "SELECT value FROM _jifty_metadata WHERE key = 'application_db_version'");
} or undef($dbv);
}
@@ -151,25 +194,6 @@
unless version->new($appv) == version->new($dbv);
}
- # Jifty db version check
- {
-
- # If we got here, the application had a version (somehow) so
- # this is an upgrade. If $dbv is undef, it's because it's
- # from before when the _jifty_metadata table existed.
- my $dbv
- = version->new( Jifty::Model::Metadata->load("jifty_db_version")
- || '0.60426' );
- my $appv = version->new($Jifty::VERSION);
- die
- "Internal jifty schema version in database ($dbv) doesn't match running jifty version ($appv)\n"
- . "Please run `bin/jifty schema --setup` to upgrade the database.\n"
- unless $appv == $dbv;
- }
-
-}
-
-
=head2 create_database MODE
C<MODE> is either "print" or "execute".
@@ -221,7 +245,7 @@
=head1 AUTHOR
-Various folks at BestPractical Solutions, LLC.
+Various folks at Best Practical Solutions, LLC.
=cut
Modified: jifty/branches/autoversioning/lib/Jifty/Model/Metadata.pm
==============================================================================
--- jifty/branches/autoversioning/lib/Jifty/Model/Metadata.pm (original)
+++ jifty/branches/autoversioning/lib/Jifty/Model/Metadata.pm Sun Jul 22 21:17:11 2007
@@ -83,8 +83,7 @@
sub store {
my $self = shift;
- $self = $self->new( current_user => Jifty::CurrentUser->superuser )
- unless ref $self;
+ $self = $self->new( current_user => Jifty::CurrentUser->superuser ) unless ref $self;
my ( $key, $value ) = @_;
$self->load_by_cols( data_key => $key );
Modified: jifty/branches/autoversioning/lib/Jifty/Module/Pluggable.pm
==============================================================================
--- jifty/branches/autoversioning/lib/Jifty/Module/Pluggable.pm (original)
+++ jifty/branches/autoversioning/lib/Jifty/Module/Pluggable.pm Sun Jul 22 21:17:11 2007
@@ -1,8 +1,11 @@
-package Jifty::Module::Pluggable;
use strict;
use warnings;
+
+package Jifty::Module::Pluggable;
use base qw/Module::Pluggable/;
+our $VERSION = '0.01';
+
=head1 NAME
Jifty::Module::Pluggable - Jifty-specific helper for Module::Pluggable
Modified: jifty/branches/autoversioning/lib/Jifty/Record.pm
==============================================================================
--- jifty/branches/autoversioning/lib/Jifty/Record.pm (original)
+++ jifty/branches/autoversioning/lib/Jifty/Record.pm Sun Jul 22 21:17:11 2007
@@ -2,7 +2,7 @@
use strict;
package Jifty::Record;
-
+use Jifty::DBI::SchemaGenerator;
use Jifty::Config;
=head1 NAME
@@ -601,19 +601,20 @@
}
-=head2 drop_table_in_db
+=head2 drop_table_in_db [TABLENAME]
When called, this method will generate the SQL to remove this model's
table in the database and execute it in the application's currently
open database. This method can destroy a lot of data. Be sure you
-know what you're doing.
+know what you're doing. It takes an optional table name.
=cut
sub drop_table_in_db {
my $self = shift;
- my $ret = Jifty->handle->simple_query( 'DROP TABLE ' . $self->table );
+ my $table = shift || $self->table;
+ my $ret = Jifty->handle->simple_query( 'DROP TABLE ' . $table );
$ret or die "error removing table $self: " . $ret->error_message;
}
@@ -668,7 +669,8 @@
my $column_name = shift;
my $col = $self->column($column_name);
- return "ALTER TABLE " . $self->table . " DROP COLUMN " . $col->name;
+ my $name = $col? $col->name : $column_name;
+ return "ALTER TABLE " . $self->table . " DROP COLUMN " . $name;
}
Modified: jifty/branches/autoversioning/lib/Jifty/Schema.pm
==============================================================================
--- jifty/branches/autoversioning/lib/Jifty/Schema.pm (original)
+++ jifty/branches/autoversioning/lib/Jifty/Schema.pm Sun Jul 22 21:17:11 2007
@@ -83,13 +83,13 @@
}
-sub _store_current_schema {
+sub store_current_schema {
my $self = shift;
Jifty::Model::Metadata->store( current_schema => Jifty::YAML::Dump($self->serialize_current_schema ));
+}
-
-sub _load_stored_schema {
+sub load_stored_schema {
my $self = shift;
my $schema = Jifty::YAML::Load(Jifty::Model::Metadata->load( 'current_schema'));
return $schema;
@@ -106,24 +106,23 @@
sub autoupgrade_schema {
my $self = shift;
- my ( $add_tables, $add_columns, $remove_tables, $remove_columns )
- = $self->compute_schema_diffs( $self->_load_stored_schema, $self->serialize_current_schema);
-
+ my ( $add_tables, $add_columns, $remove_tables, $remove_columns, $column_deltas )
+ = $self->compute_schema_diffs( $self->load_stored_schema, $self->serialize_current_schema);
# Run all "Rename" rules
$self->run_upgrade_rules('before_all_renames');
- my $table_renames = Jifty->upgrade->table_renames;
- my $column_renames = Jifty->upgrade->column_renames;
+ #my $table_renames = Jifty->upgrade->table_renames;
+ #my $column_renames = Jifty->upgrade->column_renames;
$self->run_upgrade_rules('after_column_renames');
$self->_add_tables($add_tables);
$self->_add_columns($add_columns);
$self->_drop_tables($remove_tables);
$self->_drop_columns($remove_columns);
-
+ $self->store_current_schema;
}
-sub compute_schema_diffs{
+sub compute_schema_diffs {
my $self = shift;
# load the database schema version
@@ -131,6 +130,7 @@
# hashref
my $new_tables = shift;
+
my ($remove_tables,$remove_columns) = $self->_columns_and_tables_removed_between($old_tables => $new_tables);
my ($add_tables,$add_columns) = $self->_columns_and_tables_removed_between($new_tables => $old_tables);
my $column_deltas = $self->_column_changes_between($old_tables => $new_tables);
@@ -144,7 +144,7 @@
my $from_tables = shift;
my $to_tables = shift;
- my $col_changes;
+ my $col_changes = ();
# diff the current schema version and the database schema version
foreach my $table ( keys %$from_tables ) {
@@ -215,9 +215,9 @@
# add all new tables
$self->run_upgrade_rules('before_table_adds');
- foreach my $table ( values %$add_tables ) {
+ foreach my $table ( keys %$add_tables ) {
$self->run_upgrade_rules( 'before_add_table_' . $table );
- $add_tables->{$table}->create_table_in_db();
+ $table->new->create_table_in_db();
$self->run_upgrade_rules( 'after_add_table_' . $table );
}
$self->run_upgrade_rules('after_table_adds');
@@ -227,15 +227,14 @@
sub _add_columns {
my $self = shift;
my $add_columns = shift;
-
$self->run_upgrade_rules('before_column_adds');
- foreach my $table ( values %$add_columns ) {
+ foreach my $table ( keys %$add_columns ) {
$self->run_upgrade_rules( 'before_add_columns_to_table_' . $table );
- my @cols = @{ $add_columns->{$table} };
+ my @cols = @{ $add_columns->{$table} ||[]};
foreach my $col (@cols) {
- $self->run_upgrade_rules( 'before_add_column_' . $col->name . '_to_table_' . $table );
- $add_columns->{$table}->add_column_in_db($col);
- $self->run_upgrade_rules( 'after_add_column_' . $col->name . '_to_table_' . $table );
+ $self->run_upgrade_rules( 'before_add_column_' . $col->{name} . '_to_table_' . $table );
+ $table->new->add_column_in_db($col->{name});
+ $self->run_upgrade_rules( 'after_add_column_' . $col->{name} . '_to_table_' . $table );
}
$self->run_upgrade_rules( 'after_add_columns_to_table_' . $table );
}
@@ -253,10 +252,10 @@
$self->run_upgrade_rules('before_drop_tables');
- foreach my $table ( values %$remove_tables ) {
- $self->run_upgrade_rules( 'before_drop_table_' . $table );
- $remove_tables->{$table}->drop_table_in_db();
- $self->run_upgrade_rules( 'after_drop_table_' . $table );
+ foreach my $class ( keys %$remove_tables ) {
+ $self->run_upgrade_rules( 'before_drop_table_' . $class );
+ Jifty->app_class('Record')->new->drop_table_in_db($remove_tables->{$class}->{table});
+ $self->run_upgrade_rules( 'after_drop_table_' . $class );
}
$self->run_upgrade_rules('after_drop_tables');
@@ -268,13 +267,13 @@
$self->run_upgrade_rules('before_drop_columns');
- foreach my $table ( values %$remove_columns ) {
+ foreach my $table ( keys %$remove_columns ) {
$self->run_upgrade_rules( 'before_drop_columns_from_' . $table );
- my @cols = @{ $remove_columns->{$table} };
+ my @cols = @{ $remove_columns->{$table} ||[] };
foreach my $col (@cols) {
- $self->run_upgrade_rules( 'before_drop_column' . $col->name . '_from_' . $table );
- $remove_columns->{$table}->drop_column_in_db($col);
- $self->run_upgrade_rules( 'after_drop_column_' . $col->name . '_from_' . $table );
+ $self->run_upgrade_rules( 'before_drop_column' . $col->{'name'} . '_from_' . $table );
+ $table->new->drop_column_in_db($col->{name});
+ $self->run_upgrade_rules( 'after_drop_column_' . $col->{'name'} . '_from_' . $table );
}
$self->run_upgrade_rules( 'after_drop_columns_from_' . $table );
}
Modified: jifty/branches/autoversioning/lib/Jifty/Script/Schema.pm
==============================================================================
--- jifty/branches/autoversioning/lib/Jifty/Script/Schema.pm (original)
+++ jifty/branches/autoversioning/lib/Jifty/Script/Schema.pm Sun Jul 22 21:17:11 2007
@@ -47,10 +47,15 @@
$self->manage_database_existence();
if ( $self->{create_all_tables} ) {
$self->create_all_tables();
+ { local $SIG{__WARN__} = sub {}; # horrible evil hack to avoid idiotic warnings on sqlite init
+ Jifty::Schema->new()->store_current_schema()
+ };
} elsif ( $self->{'setup_tables'} ) {
$self->upgrade_jifty_tables();
$self->upgrade_application_tables();
$self->upgrade_plugin_tables();
+ Jifty::Schema->new()->store_current_schema();
+
} else {
print "Done.\n";
}
Modified: jifty/branches/autoversioning/lib/Jifty/Upgrade.pm
==============================================================================
--- jifty/branches/autoversioning/lib/Jifty/Upgrade.pm (original)
+++ jifty/branches/autoversioning/lib/Jifty/Upgrade.pm Sun Jul 22 21:17:11 2007
@@ -14,7 +14,7 @@
package Jifty::Upgrade;
-use base qw/Jifty::Object Exporter Class::Data::Inheritable/;
+use base qw/Jifty::Object Exporter Class::Data::Inheritable Jifty::DBI::Class::Trigger/;
use vars qw/%UPGRADES @EXPORT/;
@EXPORT = qw/since rename/;
Modified: jifty/branches/autoversioning/t/TestApp/t/16-autoversioning.t
==============================================================================
--- jifty/branches/autoversioning/t/TestApp/t/16-autoversioning.t (original)
+++ jifty/branches/autoversioning/t/TestApp/t/16-autoversioning.t Sun Jul 22 21:17:11 2007
@@ -24,8 +24,8 @@
can_ok($schema, 'serialize_current_schema');
my $serialized = $schema->serialize_current_schema;
-ok($schema->_store_current_schema);;
-my $stored = $schema->_load_stored_schema();
+ok($schema->store_current_schema);;
+my $stored = $schema->load_stored_schema();
is_deeply($stored,$serialized);
{
my ( $add_tables, $add_columns, $remove_tables, $remove_columns )
More information about the Jifty-commit
mailing list