[Jifty-commit] r3298 - in jifty/branches/autoversioning: .
lib/Jifty t/TestApp-Autoversioning t/TestApp-Autoversioning/lib
t/TestApp-Autoversioning/lib/TestApp/Autoversioning
t/TestApp-Autoversioning/t t/TestApp/t
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Thu May 24 02:10:28 EDT 2007
Author: jesse
Date: Thu May 24 02:10:27 2007
New Revision: 3298
Added:
jifty/branches/autoversioning/t/TestApp-Autoversioning/
jifty/branches/autoversioning/t/TestApp-Autoversioning/lib/
jifty/branches/autoversioning/t/TestApp-Autoversioning/lib/TestApp/
jifty/branches/autoversioning/t/TestApp-Autoversioning/lib/TestApp/Autoversioning/
jifty/branches/autoversioning/t/TestApp-Autoversioning/lib/TestApp/Autoversioning/Upgrade.pm
jifty/branches/autoversioning/t/TestApp-Autoversioning/t/
jifty/branches/autoversioning/t/TestApp-Autoversioning/t/50-insanity.t
jifty/branches/autoversioning/t/TestApp/t/16-autoversioning.t
Modified:
jifty/branches/autoversioning/ (props changed)
jifty/branches/autoversioning/lib/Jifty/Schema.pm
Log:
r57083 at pinglin: jesse | 2007-05-24 02:10:13 -0400
A first sketch of autoversioning functionality.
Modified: jifty/branches/autoversioning/lib/Jifty/Schema.pm
==============================================================================
--- jifty/branches/autoversioning/lib/Jifty/Schema.pm (original)
+++ jifty/branches/autoversioning/lib/Jifty/Schema.pm Thu May 24 02:10:27 2007
@@ -62,30 +62,57 @@
sub serialize_current_schema {
my $self = shift;
- my @models = $self->model_classes;
+ my @models = grep {$_->isa('Jifty::Record') && $_->table } $self->models;
my $serialized_models = {};
foreach my $model (@models) {
- $serialized_models->{$model->_class_name} = $model->serialize_metadata;
+ $serialized_models->{$model} = $model->serialize_metadata;
}
-
return $serialized_models;
}
+sub _store_current_schema {
+ my $self = shift;
+ Jifty::Model::Metadata->store( current_schema => Jifty::YAML::Dump($self->serialize_current_schema ));
+}
+
+sub _load_stored_schema {
+ my $self = shift;
+ my $schema = Jifty::YAML::Load(Jifty::Model::Metadata->load( 'current_schema'));
+ return $schema;
+}
-sub upgrade_schema {
+
+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);
- # load the database schema version
- # hashref
- my $old_tables = $self->current_db_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;
+ $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);
+
+}
+
+sub compute_schema_diffs{
+ my $self = shift;
+
+ # load the database schema version
+ my $old_tables = shift;
# hashref
- my $new_tables = $self->new_db_schema;
+ my $new_tables = shift;
my $add_tables = {};
my $remove_tables ={};
@@ -99,10 +126,12 @@
next;
}
- foreach my $column ( @{ $old_tables->{$table}->columns } ) {
+ foreach my $column_name ( keys %{ $old_tables->{$table}->{columns} } ) {
+ my $column = $old_tables->{$table}->{columns}->{$column_name};
+
# if the column isn't in the new table as well, then mark it for deletion
- unless ( $new_tables->{$table}->column($column) ) {
+ unless ( $new_tables->{$table}->{columns}->{$column_name} ) {
push @{ $remove_columns->{$table} }, $column;
}
@@ -117,32 +146,21 @@
next;
}
- foreach my $column ( @{ $new_tables->{$table}->columns } ) {
+ foreach my $column_name ( keys %{$new_tables->{$table}->{columns}} ) {
+ my $column = $new_tables->{$table}->{columns}->{$column_name};
- # if the column isn't in the old table as well, then mark it for addition
- unless ( $old_tables->{$table}->column($column) ) {
+ # if the column isn't in the old table as well, then mark it for addition
+ unless ( $old_tables->{$table}->{columns}->{$column_name} ) {
push @{ $add_columns->{$table} }, $column;
}
- # XXX TODO: compare the column definitions and alter them if necessary
+ # XXX TODO: compare the column definitions and alter them if necessary
}
}
- # 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;
- $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);
-
-
-}
-
+ return ($add_tables, $add_columns, $remove_tables, $remove_columns );
+}
sub _add_tables {
my $self = shift;
Added: jifty/branches/autoversioning/t/TestApp-Autoversioning/lib/TestApp/Autoversioning/Upgrade.pm
==============================================================================
--- (empty file)
+++ jifty/branches/autoversioning/t/TestApp-Autoversioning/lib/TestApp/Autoversioning/Upgrade.pm Thu May 24 02:10:27 2007
@@ -0,0 +1,11 @@
+package TestApp::Autoversioning::Upgrade;
+
+use base 'Jifty::Upgrade';
+
+
+rename model 'User' vo 'Participant';
+
+
+
+1;
+
Added: jifty/branches/autoversioning/t/TestApp-Autoversioning/t/50-insanity.t
==============================================================================
--- (empty file)
+++ jifty/branches/autoversioning/t/TestApp-Autoversioning/t/50-insanity.t Thu May 24 02:10:27 2007
@@ -0,0 +1,54 @@
+=head1 DESCRIPTION
+
+
+State 1:
+
+
+ User
+ Name, Rank, FileNo
+
+ Group
+ Name
+
+
+
+State 2:
+
+
+
+ User
+ Name, Department
+
+ News
+ Summary, Body
+
+ Participant
+ Name, Rank, SerialNo
+
+
+
+
+Upgrade rules:
+
+ Rename User to Participants
+ Rename Participants.FileNo to Participants.SerialNo
+
+
+ Create table
+
+ Remove table Group
+
+
+
+
+
+
+ Upgrade:
+
+ Remove Group
+ Add User
+ Add News
+ Rename Users to Participants
+ Rename Participants FileNo to SerialNo
+
+
Added: jifty/branches/autoversioning/t/TestApp/t/16-autoversioning.t
==============================================================================
--- (empty file)
+++ jifty/branches/autoversioning/t/TestApp/t/16-autoversioning.t Thu May 24 02:10:27 2007
@@ -0,0 +1,62 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+
+=head1 DESCRIPTION
+
+This is a template for your own tests. Copy it and modify it.
+
+=cut
+
+use lib 't/lib';
+use Jifty::SubTest;
+
+use Jifty::Test tests => 1;
+
+ok(1, "Loaded the test script");
+
+Jifty->new;
+my $schema = Jifty::Schema->new();
+isa_ok($schema, 'Jifty::Schema');
+can_ok($schema, 'models');
+can_ok($schema, 'serialize_current_schema');
+
+my $serialized = $schema->serialize_current_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 )
+ = $schema->compute_schema_diffs( $stored, $serialized);
+
+is_deeply($add_tables, {});
+is_deeply($add_columns, {});
+is_deeply($remove_tables, {});
+is_deeply($remove_columns, {});
+}
+
+
+# remove a column, make sure it is picked up
+delete $serialized->{'TestApp::Model::User'}->{columns}->{tasty};
+{
+ my ( $add_tables, $add_columns, $remove_tables, $remove_columns )
+ = $schema->compute_schema_diffs( $stored, $serialized);
+
+is_deeply($add_tables, {});
+is_deeply($add_columns, {});
+is_deeply($remove_tables, {});
+is_deeply([keys %$remove_columns], ['TestApp::Model::User' ]);
+is_deeply($remove_columns->{'TestApp::Model::User'}[0]->{'name'}, 'tasty');
+}
+
+
+
+
+# remove a table, make sure it is picked up
+# add a column, make sure it is picked up
+# add a table, make sure it is picked up
+
+
+1;
+
More information about the Jifty-commit
mailing list