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 ####