From: Rafael Kitover Date: Tue, 1 Jun 2010 16:27:05 +0000 (+0000) Subject: normalize SQLite and Postgres version numbers X-Git-Tag: v0.11008~51 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7b4b17aaedbb1c9395bf201e42fe16cd04d67401;p=dbsrgits%2FSQL-Translator.git normalize SQLite and Postgres version numbers --- diff --git a/lib/SQL/Translator/Producer/PostgreSQL.pm b/lib/SQL/Translator/Producer/PostgreSQL.pm index b89c0d0..dd96e9d 100644 --- a/lib/SQL/Translator/Producer/PostgreSQL.pm +++ b/lib/SQL/Translator/Producer/PostgreSQL.pm @@ -42,7 +42,7 @@ $DEBUG = 0 unless defined $DEBUG; 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 ); @@ -181,7 +181,9 @@ sub produce { 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{}; @@ -399,16 +401,16 @@ sub create_table 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)" @@ -491,7 +493,7 @@ sub create_view { # 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)"; diff --git a/lib/SQL/Translator/Producer/SQLite.pm b/lib/SQL/Translator/Producer/SQLite.pm index 40acc57..cc8c44c 100644 --- a/lib/SQL/Translator/Producer/SQLite.pm +++ b/lib/SQL/Translator/Producer/SQLite.pm @@ -39,7 +39,7 @@ use strict; 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 ]; @@ -58,7 +58,9 @@ sub produce { 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"); @@ -169,7 +171,7 @@ sub create_table # # 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; diff --git a/lib/SQL/Translator/Utils.pm b/lib/SQL/Translator/Utils.pm index 27ae216..6930506 100644 --- a/lib/SQL/Translator/Utils.pm +++ b/lib/SQL/Translator/Utils.pm @@ -27,7 +27,8 @@ use Exporter; $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; @@ -217,6 +218,46 @@ sub parse_mysql_version { } } +#--------------------------------------------------------------------- +# 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;