--quote-field-names Qjuote all field names in statements
--no-comments Don't include comments in SQL output
+ PostgreSQL Producer Options:
+
+ --postgres-version PostgreSQL server version
+
Diagram Producer Options:
--imap-file Filename to put image map data
my $producer_dsn; # db_user "
my $add_truncate;
my $mysql_parser_version; # MySQL parser arg for /*! comments
+my $postgres_version; # PostgreSQL version
GetOptions(
'add-drop-table' => \$add_drop_table,
'use-same-auth' => \$use_same_auth,
'version' => \$show_version,
'mysql-parser-version=i' => \$mysql_parser_version,
+ 'postgres-version=f' => \$postgres_version,
) or pod2usage(2);
if ($use_same_auth) {
prefix => $prefix,
indent => $indent,
newlines => $newlines,
+ postgres_version => $postgres_version,
package_name => $package_name,
},
);
my $no_comments = $translator->no_comments;
my $add_drop_table = $translator->add_drop_table;
my $schema = $translator->schema;
+ my $pargs = $translator->producer_args;
+
+ my $postgres_version = $pargs->{postgres_version} || 0;
my $qt = '';
$qt = '"' if ($translator->quote_table_names);
{ quote_table_names => $qt,
quote_field_names => $qf,
no_comments => $no_comments,
+ postgres_version => $postgres_version,
add_drop_table => $add_drop_table,});
push @table_defs, $table_def;
push @fks, @$fks;
my $qf = $options->{quote_field_names} || '';
my $no_comments = $options->{no_comments} || 0;
my $add_drop_table = $options->{add_drop_table} || 0;
+ my $postgres_version = $options->{postgres_version} || 0;
my $table_name = $table->name or next;
$table_name = mk_name( $table_name, '', undef, 1 );
$table->name($table_name_ur);
# print STDERR "$table_name table_name\n";
- my ( @comments, @field_defs, @sequence_defs, @constraint_defs, @fks );
+ my ( @comments, @field_defs, @sequence_defs, @constraint_defs, @type_defs, @type_drops, @fks );
push @comments, "--\n-- Table: $table_name_ur\n--\n" unless $no_comments;
push @field_defs, create_field($field, { quote_table_names => $qt,
quote_field_names => $qf,
table_name => $table_name_ur,
+ postgres_version => $postgres_version,
+ type_defs => \@type_defs,
+ type_drops => \@type_drops,
constraint_defs => \@constraint_defs,});
}
my $create_statement;
$create_statement = join("\n", @comments);
- $create_statement .= qq[DROP TABLE $qt$table_name_ur$qt CASCADE;\n]
- if $add_drop_table;
+ if ($add_drop_table) {
+ if ($postgres_version >= 8.2) {
+ $create_statement .= qq[DROP TABLE IF EXISTS $qt$table_name_ur$qt CASCADE;\n];
+ $create_statement .= join ("\n", @type_drops) . "\n"
+ if $postgres_version >= 8.3;
+ } else {
+ $create_statement .= qq[DROP TABLE $qt$table_name_ur$qt CASCADE;\n];
+ }
+ }
+ $create_statement .= join("\n", @type_defs) . "\n"
+ if $postgres_version >= 8.3;
$create_statement .= qq[CREATE TABLE $qt$table_name_ur$qt (\n].
join( ",\n", map { " $_" } @field_defs, @constraint_defs ).
"\n);"
my $qf = $options->{quote_field_names} || '';
my $table_name = $field->table->name;
my $constraint_defs = $options->{constraint_defs} || [];
+ my $postgres_version = $options->{postgres_version} || 0;
+ my $type_defs = $options->{type_defs} || [];
+ my $type_drops = $options->{type_drops} || [];
$field_name_scope{$table_name} ||= {};
my $field_name = mk_name(
my $commalist = join( ', ', map { qq['$_'] } @$list );
my $seq_name;
- $field_def .= ' '. convert_datatype($field);
+ if ($postgres_version >= 8.3 && $field->data_type eq 'enum') {
+ my $type_name = $field->table->name . '_' . $field->name . '_type';
+ $field_def .= ' '. $type_name;
+ push @$type_defs, "CREATE TYPE $type_name AS ENUM ($commalist);";
+ push @$type_drops, "DROP TYPE IF EXISTS $type_name;";
+ } else {
+ $field_def .= ' '. convert_datatype($field);
+ }
#
# Default value -- disallow for timestamps
$def .= ' ON UPDATE '.join( ' ', $c->on_update );
}
+ if ( $c->deferrable ) {
+ $def .= ' DEFERRABLE';
+ }
+
push @fks, "$def;";
}