| 1 | use warnings; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | Jifty::Upgrade - Superclass for schema/data upgrades to Jifty applications | ||
| 7 | |||
| 8 | =head1 SYNOPSIS | ||
| 9 | |||
| 10 | package MyApp::Upgrade; | ||
| 11 | |||
| 12 | use base qw/ Jifty::Upgrade /; | ||
| 13 | use Jifty::Upgrade qw/ since rename /; | ||
| 14 | |||
| 15 | since '0.7.4' => sub { | ||
| 16 | # Rename a column | ||
| 17 | rename table => 'cthulus', name => 'description', | ||
| 18 | to => 'mind_numbingly_horrible_word_picture'; | ||
| 19 | }; | ||
| 20 | |||
| 21 | since '0.6.1' => sub { | ||
| 22 | my @sizes = ('Huge', 'Gigantic', 'Monstrous', 'Really Big'); | ||
| 23 | my @appearances = ('Horrible', 'Disgusting', 'Frightening', 'Evil'); | ||
| 24 | |||
| 25 | # populate new columns with some random stuff | ||
| 26 | my $cthulus = MyApp::Model::CthuluCollection->new; | ||
| 27 | while (my $cthulu = $cthulus->next) { | ||
| 28 | $cthulu->set_size($sizes[ int(rand(@sizes)) ]); | ||
| 29 | $cthulu->set_appearance($appearances[ int(rand(@appearances)) ]); | ||
| 30 | } | ||
| 31 | }; | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | C<Jifty::Upgrade> is an abstract base class to use to customize schema | ||
| 36 | and data upgrades that happen. | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | package Jifty::Upgrade; | ||
| 41 | |||
| 42 | use base qw/Jifty::Object Exporter Class::Data::Inheritable/; | ||
| 43 | use vars qw/%UPGRADES @EXPORT/; | ||
| 44 | @EXPORT = qw/since rename/; | ||
| 45 | |||
| 46 | __PACKAGE__->mk_classdata('just_renamed'); | ||
| 47 | |||
| 48 | =head2 since I<VERSION> I<SUB> | ||
| 49 | |||
| 50 | C<since> is meant to be called by subclasses of C<Jifty::Upgrade>. | ||
| 51 | Calling it signifies that I<SUB> should be run when upgrading to | ||
| 52 | version I<VERSION>, after tables and columns are added, but before | ||
| 53 | tables and columns are removed. If multiple subroutines are given for | ||
| 54 | the same version, they are run in order that they were set up. | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | sub since { | ||
| 59 | my ( $version, $sub ) = @_; | ||
| 60 | my $package = (caller)[0]; | ||
| 61 | if ( exists $UPGRADES{$package}{$version} ) { | ||
| 62 | $UPGRADES{$package}{$version} = | ||
| 63 | sub { $UPGRADES{$package}{$version}->(); $sub->(); } | ||
| 64 | } | ||
| 65 | else { | ||
| 66 | $UPGRADES{$package}{$version} = $sub; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | =head2 versions | ||
| 71 | |||
| 72 | Returns the list of versions that have been registered; this is called | ||
| 73 | by the L<Jifty::Script::Schema> tool to determine what to do while | ||
| 74 | upgrading. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub versions { | ||
| 79 | my $class = shift; | ||
| 80 | return sort keys %{ $UPGRADES{$class} || {} }; | ||
| 81 | } | ||
| 82 | |||
| 83 | =head2 upgrade_to I<VERSION> | ||
| 84 | |||
| 85 | Runs the subroutine that has been registered for the given version; if | ||
| 86 | no subroutine was registered, returns a no-op subroutine. | ||
| 87 | |||
| 88 | =cut | ||
| 89 | |||
| 90 | sub upgrade_to { | ||
| 91 | my $class = shift; | ||
| 92 | my $version = shift; | ||
| 93 | return $UPGRADES{$class}{$version} || sub { }; | ||
| 94 | } | ||
| 95 | |||
| 96 | =head2 rename table => CLASS, [column => COLUMN,] to => NAME | ||
| 97 | |||
| 98 | Used in upgrade subroutines, this executes the necessary SQL to rename | ||
| 99 | the table, or column in the table, to a new name. | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | sub rename { | ||
| 104 | my (%args) = @_; | ||
| 105 | |||
| 106 | $args{table} ||= $args{in}; | ||
| 107 | die "Must provide a table to rename" unless $args{table}; | ||
| 108 | |||
| 109 | Jifty::Util->require( $args{table} ); | ||
| 110 | my $table_name = $args{table}->table; | ||
| 111 | |||
| 112 | my $package = (caller)[0]; | ||
| 113 | my $renamed = $package->just_renamed || {}; | ||
| 114 | |||
| 115 | if ( $args{column} ) { | ||
| 116 | |||
| 117 | Jifty->handle->rename_column( %args, table => $table_name ); | ||
| 118 | |||
| 119 | # Mark this table column as renamed | ||
| 120 | $renamed->{ $table_name }{'drop'}{ $args{'column'} } = $args{'to'}; | ||
| 121 | $renamed->{ $table_name }{'add' }{ $args{'to' } } = $args{'column'}; | ||
| 122 | } | ||
| 123 | else { | ||
| 124 | Jifty->handle->rename_table( %args, table => $table_name ); | ||
| 125 | |||
| 126 | # Mark this table as renamed | ||
| 127 | $renamed->{ $table_name }{'drop_table'} = $args{'to'}; | ||
| 128 | $renamed->{ $args{'to'} }{'add_table' } = $table_name; | ||
| 129 | } | ||
| 130 | |||
| 131 | # Remember renames so that adds/drops are canceled | ||
| 132 | $package->just_renamed($renamed); | ||
| 133 | } | ||
| 134 | |||
| 135 | =head1 SEE ALSO | ||
| 136 | |||
| 137 | L<Jifty::Manual::Upgrading> | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | 1; |