Skip Menu |

This queue is for tickets about the DBIx-DBSchema CPAN distribution.

Report information
The Basics
Id: 27896
Status: resolved
Priority: 0/
Queue: DBIx-DBSchema

People
Owner: Nobody in particular
Requestors: SREZIC [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 0.33
Fixed in: (no value)



Subject: Drop column implementation etc.
The attached patch fixes the following issues: - naive implementation of DROP COLUMN (not taking into account any index issues) - PostgreSQL is very picky about quotes around literals, especially quotes around numbers are not permitted. This is solved by calling a new method _column_value_needs_quoting Regards, Slaven
Subject: dbix-dbschema-dropcolumn.patch
# # # To apply this patch: # STEP 1: Chdir to the source directory. # STEP 2: Run the 'applypatch' program with this patch file as input. # # If you do not have 'applypatch', it is part of the 'makepatch' package # that you can fetch from the Comprehensive Perl Archive Network: # http://www.perl.com/CPAN/authors/Johan_Vromans/makepatch-x.y.tar.gz # In the above URL, 'x' should be 2 or higher. # # To apply this patch without the use of 'applypatch': # STEP 1: Chdir to the source directory. # STEP 2: Run the 'patch' program with this file as input. # #### End of Preamble #### #### Patch data follows #### diff -u '/home/slavenr/work2/DBIx-DBSchema/DBSchema/Column.pm' 'DBSchema/Column.pm' Index: ./DBSchema/Column.pm --- ./DBSchema/Column.pm Mon Jul 2 17:24:47 2007 +++ ./DBSchema/Column.pm Mon Jul 2 17:34:21 2007 @@ -248,17 +248,23 @@ my($self, $dbh) = ( shift, _dbh(@_) ); my $driver = $dbh ? _load_driver($dbh) : ''; + my $driver_class = "DBIx::DBSchema::DBD::${driver}"; my %typemap; - %typemap = eval "\%DBIx::DBSchema::DBD::${driver}::typemap" if $driver; + %typemap = eval "\%${driver_class}::typemap" if $driver; my $type = defined( $typemap{uc($self->type)} ) ? $typemap{uc($self->type)} : $self->type; my $null = $self->null; - my $default; - if ( defined($self->default) && !ref($self->default) && $self->default ne '' + if ( $driver_class->can("_column_value_needs_quoting") ) { + if ($driver_class->_column_value_needs_quoting($self)) { + $default = $dbh->quote($self->default); + } else { + $default = ref($self->default) ? ${$self->default} : $self->default; + } + } elsif ( defined($self->default) && !ref($self->default) && $self->default ne '' && ref($dbh) # false laziness: nicked from FS::Record::_quote && ( $self->default !~ /^\-?\d+(\.\d+)?$/ @@ -512,6 +518,15 @@ } +sub sql_drop_column { + my( $self, $dbh ) = ( shift, _dbh(@_) ); + + my $table = $self->table_name; + my $name = $self->name; + + ("ALTER TABLE $table DROP COLUMN $name"); # XXX what about indexes??? +} + =back =head1 AUTHOR @@ -543,3 +558,7 @@ 1; +# Local Variables: +# mode: cperl +# cperl-indent-level: 2 +# End: diff -u '/home/slavenr/work2/DBIx-DBSchema/DBSchema/DBD/Pg.pm' 'DBSchema/DBD/Pg.pm' Index: ./DBSchema/DBD/Pg.pm --- ./DBSchema/DBD/Pg.pm Mon Jul 2 17:24:47 2007 +++ ./DBSchema/DBD/Pg.pm Mon Jul 2 17:34:21 2007 @@ -154,6 +154,23 @@ $row->{'indisunique'}; } +sub _column_value_needs_quoting { + my($proto, $col) = @_; + $col->type !~ m{^( + int(?:2|4|8)? + | smallint + | integer + | bigint + | (?:numeric|decimal)(?:\(\d+(?:\s*\,\s*\d+\))?)? + | real + | double\s+precision + | float(?:\(\d+\))? + | serial(?:4|8)? + | bigserial + )$}x; +} + + =head1 AUTHOR Ivan Kohler <ivan-dbix-dbschema@420.am> @@ -180,3 +197,7 @@ 1; +# Local Variables: +# mode: cperl +# cperl-indent-level: 2 +# End: diff -u '/home/slavenr/work2/DBIx-DBSchema/DBSchema/Table.pm' 'DBSchema/Table.pm' Index: ./DBSchema/Table.pm --- ./DBSchema/Table.pm Mon Jul 2 17:24:47 2007 +++ ./DBSchema/Table.pm Mon Jul 2 17:38:51 2007 @@ -610,9 +610,23 @@ } } - - #should eventually drop columns not in $new... + #should eventually check & create missing indices ( & delete ones not in $new) + + #drop columns not in $new + foreach my $column ( $self->columns ) { + + if ( !$new->column($column) ) { + + warn "column $table.$column should be dropped.\n" if $DEBUG; + + push @r, + $self->column($column)->sql_drop_column( $dbh ); + + } + + } + ### # indices ### @@ -737,3 +751,7 @@ 1; +# Local Variables: +# mode: cperl +# cperl-indent-level: 2 +# End: #### End of Patch data #### #### ApplyPatch data follows #### # Data version : 1.0 # Date generated : Mon Jul 2 17:49:53 2007 # Generated by : makepatch 2.00_12* # Recurse directories : Yes # Excluded files : (\A|/).*\~\Z # (\A|/).*\.a\Z # (\A|/).*\.bak\Z # (\A|/).*\.BAK\Z # (\A|/).*\.elc\Z # (\A|/).*\.exe\Z # (\A|/).*\.gz\Z # (\A|/).*\.ln\Z # (\A|/).*\.o\Z # (\A|/).*\.obj\Z # (\A|/).*\.olb\Z # (\A|/).*\.old\Z # (\A|/).*\.orig\Z # (\A|/).*\.rej\Z # (\A|/).*\.so\Z # (\A|/).*\.Z\Z # (\A|/)\.del\-.*\Z # (\A|/)\.make\.state\Z # (\A|/)\.nse_depinfo\Z # (\A|/)core\Z # (\A|/)tags\Z # (\A|/)TAGS\Z # p 'DBSchema/Column.pm' 13549 1183390461 0100655 # p 'DBSchema/DBD/Pg.pm' 4601 1183390461 0100655 # p 'DBSchema/Table.pm' 19956 1183390731 0100655 #### End of ApplyPatch data #### #### End of Patch kit [created: Mon Jul 2 17:49:53 2007] #### #### Patch checksum: 165 4483 44409 #### #### Checksum: 183 5108 30457 ####
drop column portion of this patch applied, will be in 0.34. if you could send in the _column_value_needs_quoting patch on its own without the applypatch or cperl noise, that would be helpful.
drop column portion of this patch applied, will be in 0.34. if you could send in the _column_value_needs_quoting patch on its own without the applypatch or cperl noise, that would be helpful. On Mon Jul 02 11:52:39 2007, SREZIC wrote: Show quoted text
> The attached patch fixes the following issues: > - naive implementation of DROP COLUMN (not taking into account any
index Show quoted text
> issues) > - PostgreSQL is very picky about quotes around literals, especially > quotes around numbers are not permitted. This is solved by calling a
new Show quoted text
> method _column_value_needs_quoting > > Regards, > Slaven
On Thu Dec 13 21:00:33 2007, IVAN wrote: Show quoted text
> drop column portion of this patch applied, will be in 0.34. > > if you could send in the _column_value_needs_quoting patch on its own > without the applypatch or cperl noise, that would be helpful. > > > On Mon Jul 02 11:52:39 2007, SREZIC wrote:
> > The attached patch fixes the following issues: > > - naive implementation of DROP COLUMN (not taking into account any
> index
> > issues) > > - PostgreSQL is very picky about quotes around literals, especially > > quotes around numbers are not permitted. This is solved by calling a
> new
> > method _column_value_needs_quoting > > > > Regards, > > Slaven
> >
Revisited patch without emacs variables and with a pure "cvs diff" attached. But note that makepatch actually produces patches which work fine with the normal patch programm as is, and that I prefer having the emacs stuff because of the non-standard indentation in your source files. Regards, Slaven
? Makefile ? blib ? pm_to_blib Index: DBSchema/Column.pm =================================================================== RCS file: /home/cvs/cvsroot/DBIx-DBSchema/DBSchema/Column.pm,v retrieving revision 1.28 diff -u -p -r1.28 Column.pm --- DBSchema/Column.pm 13 Dec 2007 22:44:06 -0000 1.28 +++ DBSchema/Column.pm 3 Jan 2008 09:31:45 -0000 @@ -248,13 +248,14 @@ sub line { my($self, $dbh) = ( shift, _dbh(@_) ); my $driver = $dbh ? _load_driver($dbh) : ''; + my $driver_class = "DBIx::DBSchema::DBD::${driver}"; ## # type mapping ## my %typemap; - %typemap = eval "\%DBIx::DBSchema::DBD::${driver}::typemap" if $driver; + %typemap = eval "\%${driver_class}::typemap" if $driver; my $type = defined( $typemap{uc($self->type)} ) ? $typemap{uc($self->type)} : $self->type; @@ -265,7 +266,13 @@ sub line { my $default; my $orig_default = $self->default; - if ( defined($self->default) && !ref($self->default) && $self->default ne '' + if ( $driver_class->can("_column_value_needs_quoting") ) { + if ($driver_class->_column_value_needs_quoting($self)) { + $default = $dbh->quote($self->default); + } else { + $default = ref($self->default) ? ${$self->default} : $self->default; + } + } elsif ( defined($self->default) && !ref($self->default) && $self->default ne '' && ref($dbh) # false laziness: nicked from FS::Record::_quote && ( $self->default !~ /^\-?\d+(\.\d+)?$/ Index: DBSchema/Table.pm =================================================================== RCS file: /home/cvs/cvsroot/DBIx-DBSchema/DBSchema/Table.pm,v retrieving revision 1.36 diff -u -p -r1.36 Table.pm --- DBSchema/Table.pm 7 Nov 2007 21:06:43 -0000 1.36 +++ DBSchema/Table.pm 3 Jan 2008 09:31:45 -0000 @@ -650,9 +650,23 @@ sub sql_alter_table { } } - - #should eventually drop columns not in $new... + #should eventually check & create missing indices ( & delete ones not in $new) + + #drop columns not in $new + foreach my $column ( $self->columns ) { + + if ( !$new->column($column) ) { + + warn "column $table.$column should be dropped.\n" if $DEBUG; + + push @r, + $self->column($column)->sql_drop_column( $dbh ); + + } + + } + ### # indices ### Index: DBSchema/DBD/Pg.pm =================================================================== RCS file: /home/cvs/cvsroot/DBIx-DBSchema/DBSchema/DBD/Pg.pm,v retrieving revision 1.15 diff -u -p -r1.15 Pg.pm --- DBSchema/DBD/Pg.pm 25 Oct 2007 08:30:46 -0000 1.15 +++ DBSchema/DBD/Pg.pm 3 Jan 2008 09:31:45 -0000 @@ -263,6 +263,23 @@ sub alter_column_callback { } +sub _column_value_needs_quoting { + my($proto, $col) = @_; + $col->type !~ m{^( + int(?:2|4|8)? + | smallint + | integer + | bigint + | (?:numeric|decimal)(?:\(\d+(?:\s*\,\s*\d+\))?)? + | real + | double\s+precision + | float(?:\(\d+\))? + | serial(?:4|8)? + | bigserial + )$}x; +} + + =head1 AUTHOR Ivan Kohler <ivan-dbix-dbschema@420.am>
applied and 0.38 released, thanks!