[Jifty-commit] r2276 - jifty/trunk/lib/Jifty
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Sun Dec 3 00:03:01 EST 2006
Author: audreyt
Date: Sun Dec 3 00:03:01 2006
New Revision: 2276
Modified:
jifty/trunk/lib/Jifty/Upgrade.pm
Log:
* Jifty::Upgrade - Purely cosmetic tidy-up recommenting change
Modified: jifty/trunk/lib/Jifty/Upgrade.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Upgrade.pm (original)
+++ jifty/trunk/lib/Jifty/Upgrade.pm Sun Dec 3 00:03:01 2006
@@ -29,11 +29,13 @@
=cut
sub since {
- my ($version, $sub) = @_;
+ my ( $version, $sub ) = @_;
my $package = (caller)[0];
- if (exists $UPGRADES{$package}{$version}) {
- $UPGRADES{$package}{$version} = sub { $UPGRADES{$package}{$version}->(); $sub->(); }
- } else {
+ if ( exists $UPGRADES{$package}{$version} ) {
+ $UPGRADES{$package}{$version} =
+ sub { $UPGRADES{$package}{$version}->(); $sub->(); }
+ }
+ else {
$UPGRADES{$package}{$version} = $sub;
}
}
@@ -48,7 +50,7 @@
sub versions {
my $class = shift;
- return sort keys %{$UPGRADES{$class} || {}};
+ return sort keys %{ $UPGRADES{$class} || {} };
}
=head2 upgrade_to I<VERSION>
@@ -59,9 +61,9 @@
=cut
sub upgrade_to {
- my $class = shift;
+ my $class = shift;
my $version = shift;
- return $UPGRADES{$class}{$version} || sub {};
+ return $UPGRADES{$class}{$version} || sub { };
}
=head2 rename table => CLASS, [column => COLUMN,] to => NAME
@@ -77,53 +79,59 @@
$args{table} ||= $args{in};
die "Must provide a table to rename" unless $args{table};
- Jifty::Util->require($args{table});
+ Jifty::Util->require( $args{table} );
my $table_name = $args{table}->table;
- if ($args{column}) {
+ if ( $args{column} ) {
my $driver = Jifty->config->framework('Database')->{'Driver'};
- if ($driver eq "SQLite") {
- # It's possible to work around this, but it's a PITA that
- # I haven't figured out all of the details of. It
- # involves creating a temporary table that's a duplicate
- # of the current table, copying the data over, dropping
- # the original table, recreating it with the column
- # renamed, transferring the data back, and then dropping
- # the temporary table. Painful enough for ya?
+ if ( $driver eq "SQLite" ) {
+
+ # Convert columns
my ($schema) = Jifty->handle->fetch_result("SELECT sql FROM sqlite_master WHERE tbl_name = '$table_name' AND type = 'table'");
- my $sth = Jifty->handle->simple_query("SELECT sql FROM sqlite_master WHERE tbl_name = '$table_name' AND type = 'index'");
- my @indice;
- while (my ($index) = $sth->fetchrow_array) {
- $index =~ s/^(.*\(.*)\b\Q$args{column}\E\b/$1$args{to}/i;
- push @indice, $index;
- }
$schema =~ s/(.*create\s+table\s+)\S+(.*?\(\s*)//i;
- my $new_table_name = join('_', $table_name, 'new' ,$$);
+ my $new_table_name = join( '_', $table_name, 'new', $$ );
my $new_create_clause = "$1$new_table_name$2";
- my @column_info = (split /,/, $schema);
+ my @column_info = ( split /,/, $schema );
my @column_names = map { /^\s*(\S+)/ ? $1 : () } @column_info;
s/^(\s*)\Q$args{column}\E/$1$args{to}/i for @column_info;
- my $new_schema = $new_create_clause . join(',', @column_info);
- my $copy_columns = join(', ', map {
- (lc($_) eq lc($args{column})) ? "$_ AS $args{to}" : $_
- } @column_names);
+ my $new_schema = $new_create_clause . join( ',', @column_info );
+ my $copy_columns = join(
+ ', ',
+ map {
+ ( lc($_) eq lc( $args{column} ) )
+ ? "$_ AS $args{to}"
+ : $_
+ } @column_names
+ );
+
+ # Convert indices
+ my $indice_sth = Jifty->handle->simple_query("SELECT sql FROM sqlite_master WHERE tbl_name = '$table_name' AND type = 'index'");
+ my @indice_sql;
+ while ( my ($index) = $indice_sth->fetchrow_array ) {
+ $index =~ s/^(.*\(.*)\b\Q$args{column}\E\b/$1$args{to}/i;
+ push @indice_sql, $index;
+ }
+ $indice_sth->finish;
+ # Run the conversion SQLs
Jifty->handle->begin_transaction;
Jifty->handle->simple_query($new_schema);
Jifty->handle->simple_query("INSERT INTO $new_table_name SELECT $copy_columns FROM $table_name");
Jifty->handle->simple_query("DROP TABLE $table_name");
Jifty->handle->simple_query("ALTER TABLE $new_table_name RENAME TO $table_name");
- Jifty->handle->simple_query($_) for @indice;
+ Jifty->handle->simple_query($_) for @indice_sql;
Jifty->handle->commit;
- } else {
+ }
+ else {
Jifty->handle->simple_query("ALTER TABLE $table_name RENAME $args{column} TO $args{to}");
}
- } else {
+ }
+ else {
Jifty->handle->simple_query("ALTER TABLE $table_name RENAME TO $args{to}");
}
}
More information about the Jifty-commit
mailing list