use strict;
use warnings;
our ( $DEBUG, $WARN );
-our $VERSION = '1.59';
+our $VERSION = '1.60';
$DEBUG = 0 unless defined $DEBUG;
use base qw(SQL::Translator::Producer);
# Geometry
#
if (my @geometry_columns = grep { is_geometry($_) } $table->get_fields) {
- $create_statement .= ";";
- $create_statement .= join("\n", '', map{ drop_geometry_column($_) } @geometry_columns) if $options->{add_drop_table};
- $create_statement .= join("\n", '', map{ add_geometry_column($_) } @geometry_columns);
+ $create_statement .= join(";\n", '', map{ drop_geometry_column($_, $options) } @geometry_columns) if $options->{add_drop_table};
+ $create_statement .= join(";\n", '', map{ add_geometry_column($_, $options) } @geometry_columns);
}
return $create_statement, \@fks;
# Geometry constraints
#
if (is_geometry($field)) {
- foreach ( create_geometry_constraints($field) ) {
- my ($cdefs, $fks) = create_constraint($_, {
- generator => $generator,
- });
+ foreach ( create_geometry_constraints($field, $options) ) {
+ my ($cdefs, $fks) = create_constraint($_, $options);
push @$constraint_defs, @$cdefs;
push @$fks, @$fks;
}
}
sub create_geometry_constraints {
- my $field = shift;
+ my ($field, $options) = @_;
+ my $fname = _generator($options)->quote($field);
my @constraints;
push @constraints, SQL::Translator::Schema::Constraint->new(
name => "enforce_dims_".$field->name,
- expression => "(ST_NDims($field) = ".$field->extra->{dimensions}.")",
+ expression => "(ST_NDims($fname) = ".$field->extra->{dimensions}.")",
table => $field->table,
type => CHECK_C,
);
push @constraints, SQL::Translator::Schema::Constraint->new(
name => "enforce_srid_".$field->name,
- expression => "(ST_SRID($field) = ".$field->extra->{srid}.")",
+ expression => "(ST_SRID($fname) = ".$field->extra->{srid}.")",
table => $field->table,
type => CHECK_C,
);
push @constraints, SQL::Translator::Schema::Constraint->new(
name => "enforce_geotype_".$field->name,
- expression => "(GeometryType($field) = '".$field->extra->{geometry_type}."'::text OR $field IS NULL)",
+ expression => "(GeometryType($fname) = ". __PACKAGE__->_quote_string($field->extra->{geometry_type}) ."::text OR $fname IS NULL)",
table => $field->table,
type => CHECK_C,
);
$data_type = 'character varying';
}
elsif ( $field->is_auto_increment ) {
- if ( defined $size[0] && $size[0] > 11 ) {
+ if ( (defined $size[0] && $size[0] > 11) or $data_type eq 'bigint' ) {
$data_type = 'bigserial';
}
else {
# drop geometry column and constraints
push @out,
- drop_geometry_column($from_field),
- drop_geometry_constraints($from_field),
+ drop_geometry_column($from_field, $options),
+ drop_geometry_constraints($from_field, $options),
if is_geometry($from_field);
# it's necessary to start with rename column cause this would affect
# ALTER TABLE users ALTER COLUMN column SET DEFAULT ThisIsUnescaped;
if(ref $default_value eq "SCALAR" ) {
$default_value = $$default_value;
- } elsif( defined $default_value && $to_dt =~ /^(character|text)/xsmi ) {
+ } elsif( defined $default_value && $to_dt =~ /^(character|text|timestamp|date)/xsmi ) {
$default_value = __PACKAGE__->_quote_string($default_value);
}
# add geometry column and constraints
push @out,
- add_geometry_column($to_field),
- add_geometry_constraints($to_field)
+ add_geometry_column($to_field, $options),
+ add_geometry_constraints($to_field, $options),
if is_geometry($to_field);
return wantarray ? @out : join(";\n", @out);
my $out = sprintf('ALTER TABLE %s ADD COLUMN %s',
_generator($options)->quote($new_field->table->name),
create_field($new_field, $options));
- $out .= "\n".add_geometry_column($new_field)
- . "\n".add_geometry_constraints($new_field)
+ $out .= ";\n".add_geometry_column($new_field, $options)
+ . ";\n".add_geometry_constraints($new_field, $options)
if is_geometry($new_field);
return $out;
my $out = sprintf('ALTER TABLE %s DROP COLUMN %s',
$generator->quote($old_field->table->name),
$generator->quote($old_field->name));
- $out .= "\n".drop_geometry_column($old_field)
+ $out .= ";\n".drop_geometry_column($old_field, $options)
if is_geometry($old_field);
return $out;
}
my ($field, $options) = @_;
return sprintf(
- "INSERT INTO geometry_columns VALUES ('%s','%s','%s','%s','%s','%s','%s')",
- '',
- $field->table->schema->name,
- $options->{table} ? $options->{table} : $field->table->name,
- $field->name,
- $field->extra->{dimensions},
- $field->extra->{srid},
- $field->extra->{geometry_type},
+ "INSERT INTO geometry_columns VALUES (%s,%s,%s,%s,%s,%s,%s)",
+ map(__PACKAGE__->_quote_string($_),
+ '',
+ $field->table->schema->name,
+ $options->{table} ? $options->{table} : $field->table->name,
+ $field->name,
+ $field->extra->{dimensions},
+ $field->extra->{srid},
+ $field->extra->{geometry_type},
+ ),
);
}
sub drop_geometry_column {
- my $field = shift;
+ my ($field) = @_;
return sprintf(
- "DELETE FROM geometry_columns WHERE f_table_schema = '%s' AND f_table_name = '%s' AND f_geometry_column = '%s'",
- $field->table->schema->name,
- $field->table->name,
- $field->name,
+ "DELETE FROM geometry_columns WHERE f_table_schema = %s AND f_table_name = %s AND f_geometry_column = %s",
+ map(__PACKAGE__->_quote_string($_),
+ $field->table->schema->name,
+ $field->table->name,
+ $field->name,
+ ),
);
}
sub add_geometry_constraints {
my ($field, $options) = @_;
- return join("\n", map { alter_create_constraint($_) }
- create_geometry_constraints($field));
+ return join(";\n", map { alter_create_constraint($_, $options) }
+ create_geometry_constraints($field, $options));
}
sub drop_geometry_constraints {
my ($field, $options) = @_;
- return join("\n", map { alter_drop_constraint($_) }
- create_geometry_constraints($field));
+ return join(";\n", map { alter_drop_constraint($_, $options) }
+ create_geometry_constraints($field, $options));
}
my $out = sprintf('ALTER TABLE %s %s',
$generator->quote($to_table->name),
$options->{alter_table_action});
- $out .= "\n".$options->{geometry_changes} if $options->{geometry_changes};
+ $out .= ";\n".$options->{geometry_changes} if $options->{geometry_changes};
return $out;
}
$options->{alter_table_action} = "RENAME TO " . $generator->quote($new_table);
my @geometry_changes = map {
- drop_geometry_column($_),
- add_geometry_column($_, { table => $new_table }),
+ drop_geometry_column($_, $options),
+ add_geometry_column($_, { %{$options}, table => $new_table }),
} grep { is_geometry($_) } $old_table->get_fields;
- $options->{geometry_changes} = join ("\n",@geometry_changes) if @geometry_changes;
+ $options->{geometry_changes} = join (";\n",@geometry_changes) if @geometry_changes;
return alter_table($old_table, $options);
}
sub alter_create_index {
my ($index, $options) = @_;
my $generator = _generator($options);
- my ($idef, $constraints) = create_index($index, {
- generator => $generator,
- });
+ my ($idef, $constraints) = create_index($index, $options);
return $index->type eq NORMAL ? $idef
: sprintf('ALTER TABLE %s ADD %s',
$generator->quote($index->table->name),
my @geometry_drops = map { drop_geometry_column($_); } grep { is_geometry($_) } $table->get_fields;
- $out .= join("\n", '', @geometry_drops) if @geometry_drops;
+ $out .= join(";\n", '', @geometry_drops) if @geometry_drops;
return $out;
}