use base qw(SQL::Translator::Producer);
use SQL::Translator::Schema::Constants;
-use SQL::Translator::Utils qw(debug header_comment);
+use SQL::Translator::Utils qw(debug header_comment parse_dbms_version);
use Data::Dumper;
my ( %translate, %index_name );
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 $postgres_version = parse_dbms_version(
+ $pargs->{postgres_version}, 'perl'
+ );
my $qt = $translator->quote_table_names ? q{"} : q{};
my $qf = $translator->quote_field_names ? q{"} : q{};
my $create_statement;
$create_statement = join("\n", @comments);
if ($add_drop_table) {
- if ($postgres_version >= 8.2) {
+ if ($postgres_version >= 8.002) {
$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 && scalar @type_drops;
+ if $postgres_version >= 8.003 && scalar @type_drops;
} 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 && scalar @type_defs;
+ if $postgres_version >= 8.003 && scalar @type_defs;
$create_statement .= qq[CREATE ${temporary}TABLE $qt$table_name_ur$qt (\n].
join( ",\n", map { " $_" } @field_defs, @constraint_defs ).
"\n)"
# todo deal with embedded quotes
my $commalist = join( ', ', map { qq['$_'] } @$list );
- if ($postgres_version >= 8.3 && $field->data_type eq 'enum') {
+ if ($postgres_version >= 8.003 && $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)";
use warnings;
use Data::Dumper;
use SQL::Translator::Schema::Constants;
-use SQL::Translator::Utils qw(debug header_comment);
+use SQL::Translator::Utils qw(debug header_comment parse_dbms_version);
use vars qw[ $VERSION $DEBUG $WARN ];
my $add_drop_table = $translator->add_drop_table;
my $schema = $translator->schema;
my $producer_args = $translator->producer_args;
- my $sqlite_version = $producer_args->{sqlite_version} || 0;
+ my $sqlite_version = parse_dbms_version(
+ $producer_args->{sqlite_version}, 'perl'
+ );
my $no_txn = $producer_args->{no_transaction};
debug("PKG: Beginning production\n");
#
# Header.
#
- my $exists = ($sqlite_version >= 3.3) ? ' IF EXISTS' : '';
+ my $exists = ($sqlite_version >= 3.003) ? ' IF EXISTS' : '';
my @create;
my ($comment, $create_table) = "";
$comment = "--\n-- Table: $table_name\n--\n" unless $no_comments;
$VERSION = '1.59';
$DEFAULT_COMMENT = '-- ';
@EXPORT_OK = qw(
- debug normalize_name header_comment parse_list_arg truncate_id_uniquely $DEFAULT_COMMENT parse_mysql_version
+ debug normalize_name header_comment parse_list_arg truncate_id_uniquely
+ $DEFAULT_COMMENT parse_mysql_version parse_dbms_version
);
use constant COLLISION_TAG_LENGTH => 8;
}
}
+#---------------------------------------------------------------------
+# parse_dbms_version ( $version_string, $target )
+#
+# Attempts to parse either a native or perl-style version string into
+# a version number format as specified by $target, which can be either
+# 'perl' for a perl-style version number, or 'native' for an X.X.X
+# style version number.
+#---------------------------------------------------------------------
+sub parse_dbms_version {
+ my ($v, $target) = @_;
+
+ return undef unless $v;
+
+ my @vers;
+
+ # X.Y.Z style
+ if ( $v =~ / ^ (\d+) \. (\d{1,3}) (?: \. (\d{1,3}) )? $ /x ) {
+ push @vers, $1, $2, $3;
+ }
+
+ # XX.YYYZZZ (perl) style or simply X
+ elsif ( $v =~ / ^ (\d+) (?: \. (\d{3}) (\d{3}) )? $ /x ) {
+ push @vers, $1, $2, $3;
+ }
+ else {
+ #how do I croak sanely here?
+ die "Unparseable database server version '$v'";
+ }
+
+ if ($target eq 'perl') {
+ return sprintf ('%d.%03d%03d', map { $_ || 0 } (@vers) );
+ }
+ elsif ($target eq 'native') {
+ return join '.' => map 0+$_, grep defined, @vers;
+ }
+ else {
+ #how do I croak sanely here?
+ die "Unknown version target '$target'";
+ }
+}
1;