From: Rafael Kitover Date: Wed, 2 Jun 2010 02:16:48 +0000 (+0000) Subject: patch from abraxxa (Alexander Hartmaier) to truncate unique constraint names that... X-Git-Tag: v0.11008~47 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=3b9249fb0058ac9896a10c9ca8b87ddd40f6c074;p=dbsrgits%2FSQL-Translator.git patch from abraxxa (Alexander Hartmaier) to truncate unique constraint names that are too long --- diff --git a/Changes b/Changes index cd768b0..9c06b9b 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,5 @@ +* Fix Producer::Oracle generation of too long unique constraint names +* Producer::Oracle tests now use Test::Differences * Prettify output of SQLite producer (less bogus newlines) * Augment SQLite and Pg producers to accept a perl-formatted (%d.%03d%03d) *_version producer args (to facilitate direct diff --git a/lib/SQL/Translator/Producer/Oracle.pm b/lib/SQL/Translator/Producer/Oracle.pm index edf1bab..f8cbe5f 100644 --- a/lib/SQL/Translator/Producer/Oracle.pm +++ b/lib/SQL/Translator/Producer/Oracle.pm @@ -349,7 +349,7 @@ sub create_table { if ($name) { # Force prepend of table_name as ORACLE doesn't allow duplicate # CONSTRAINT names even for different tables (ORA-02264) - $name = "${table_name}_$name" unless $name =~ /^$table_name/; + $name = mk_name( "${table_name}_$name", 'u' ) unless $name =~ /^$table_name/; } else { $name = mk_name( $table_name, 'u' ); diff --git a/t/16xml-parser.t b/t/16xml-parser.t index ae96dd4..2fe15f3 100644 --- a/t/16xml-parser.t +++ b/t/16xml-parser.t @@ -27,7 +27,7 @@ use constant DEBUG => (exists $opt{d} ? 1 : 0); #============================================================================= BEGIN { - maybe_plan(224, 'SQL::Translator::Parser::XML::SQLFairy'); + maybe_plan(238, 'SQL::Translator::Parser::XML::SQLFairy'); } my $testschema = "$Bin/data/xml/schema.xml"; @@ -89,6 +89,7 @@ schema_ok( $scma, { is_nullable => 0, default_value => "hello", size => 100, + is_unique => 1, }, { name => "description", @@ -162,6 +163,11 @@ schema_ok( $scma, { fields => ["email"], }, { + name => 'very_long_index_name_on_title_field_which_should_be_truncated_for_various_rdbms', + type => UNIQUE, + fields => ["title"], + }, + { type => FOREIGN_KEY, fields => ["another_id"], reference_table => "Another", diff --git a/t/18ttschema-producer.t b/t/18ttschema-producer.t index e28a616..909a015 100644 --- a/t/18ttschema-producer.t +++ b/t/18ttschema-producer.t @@ -115,7 +115,7 @@ Fields is_nullable: 0 default_value: hello is_primary_key: 0 - is_unique: 0 + is_unique: 1 is_auto_increment: 0 is_foreign_key: 0 foreign_key_reference: @@ -258,6 +258,19 @@ Constraints options: is_valid: 1 + very_long_index_name_on_title_field_which_should_be_truncated_for_various_rdbms + type: UNIQUE + fields: title + expression: + match_type: + reference_fields: + reference_table: + deferrable: 1 + on_delete: + on_update: + options: + is_valid: 1 + ? type: FOREIGN KEY fields: another_id diff --git a/t/43xml-to-db2.t b/t/43xml-to-db2.t index a14a665..933ee69 100644 --- a/t/43xml-to-db2.t +++ b/t/43xml-to-db2.t @@ -46,7 +46,8 @@ CREATE TABLE Basic ( another_id INTEGER DEFAULT 2, timest TIMESTAMP, PRIMARY KEY (id), - CONSTRAINT emailuniqueindex UNIQUE (email) + CONSTRAINT emailuniqueindex UNIQUE (email), + CONSTRAINT very_long_index_name_on_title_field_which_should_be_truncated_for_various_rdbms UNIQUE (title) ); DROP TABLE Another; diff --git a/t/44-xml-to-db2-array.t b/t/44-xml-to-db2-array.t index d4f5a54..6a872ca 100644 --- a/t/44-xml-to-db2-array.t +++ b/t/44-xml-to-db2-array.t @@ -44,7 +44,8 @@ q|CREATE TABLE Basic ( another_id INTEGER DEFAULT 2, timest TIMESTAMP, PRIMARY KEY (id), - CONSTRAINT emailuniqueindex UNIQUE (email) + CONSTRAINT emailuniqueindex UNIQUE (email), + CONSTRAINT very_long_index_name_on_title_field_which_should_be_truncated_for_various_rdbms UNIQUE (title) );|, 'DROP TABLE Another;', diff --git a/t/46xml-to-pg.t b/t/46xml-to-pg.t index e1796df..519e806 100644 --- a/t/46xml-to-pg.t +++ b/t/46xml-to-pg.t @@ -47,7 +47,8 @@ CREATE TABLE "Basic" ( "another_id" integer DEFAULT '2', "timest" timestamp, PRIMARY KEY ("id"), - CONSTRAINT "emailuniqueindex" UNIQUE ("email") + CONSTRAINT "emailuniqueindex" UNIQUE ("email"), + CONSTRAINT "very_long_index_name_on_title_field_which_should_be_truncated_for_various_rdbms" UNIQUE ("title") ); CREATE INDEX "titleindex" on "Basic" ("title"); diff --git a/t/48xml-to-sqlite.t b/t/48xml-to-sqlite.t index 7226723..ef14da4 100644 --- a/t/48xml-to-sqlite.t +++ b/t/48xml-to-sqlite.t @@ -55,6 +55,8 @@ CREATE INDEX titleindex ON Basic (title); CREATE UNIQUE INDEX emailuniqueindex ON Basic (email); +CREATE UNIQUE INDEX very_long_index_name_on_title_field_which_should_be_truncated_for_various_rdbms ON Basic (title); + DROP TABLE Another; CREATE TABLE Another ( @@ -107,6 +109,7 @@ eq_or_diff(\@sql, )', 'CREATE INDEX titleindex ON Basic (title)', 'CREATE UNIQUE INDEX emailuniqueindex ON Basic (email)', + 'CREATE UNIQUE INDEX very_long_index_name_on_title_field_which_should_be_truncated_for_various_rdbms ON Basic (title)', 'DROP TABLE Another', 'CREATE TABLE Another ( id INTEGER PRIMARY KEY NOT NULL, diff --git a/t/51-xml-to-oracle.t b/t/51-xml-to-oracle.t index 06776a9..c72ea4c 100644 --- a/t/51-xml-to-oracle.t +++ b/t/51-xml-to-oracle.t @@ -5,6 +5,7 @@ use FindBin qw/$Bin/; use Test::More; use Test::SQL::Translator; use Test::Exception; +use Test::Differences; use Data::Dumper; use SQL::Translator; use SQL::Translator::Schema::Constants; @@ -54,7 +55,8 @@ my $want = [ another_id number(10) DEFAULT \'2\', timest date, PRIMARY KEY (id), - CONSTRAINT Basic_emailuniqueindex UNIQUE (email) + CONSTRAINT u_Basic_emailuniqueindex UNIQUE (email), + CONSTRAINT u_Basic_very_long_index_name_o UNIQUE (title) )', 'DROP TABLE Another CASCADE CONSTRAINTS', 'DROP SEQUENCE sq_Another_id', @@ -101,7 +103,7 @@ END; is_deeply(\@sql, $want, 'Got correct Oracle statements in list context'); -is($sql_string, q|DROP TABLE Basic CASCADE CONSTRAINTS; +eq_or_diff($sql_string, q|DROP TABLE Basic CASCADE CONSTRAINTS; DROP SEQUENCE sq_Basic_id01; @@ -118,7 +120,8 @@ CREATE TABLE Basic ( another_id number(10) DEFAULT '2', timest date, PRIMARY KEY (id), - CONSTRAINT Basic_emailuniqueindex UNIQUE (email) + CONSTRAINT u_Basic_emailuniqueindex01 UNIQUE (email), + CONSTRAINT u_Basic_very_long_index_name01 UNIQUE (title) ); DROP TABLE Another CASCADE CONSTRAINTS; diff --git a/t/51-xml-to-oracle_quoted.t b/t/51-xml-to-oracle_quoted.t index 292882d..be7f7b4 100644 --- a/t/51-xml-to-oracle_quoted.t +++ b/t/51-xml-to-oracle_quoted.t @@ -5,6 +5,7 @@ use FindBin qw/$Bin/; use Test::More; use Test::SQL::Translator; use Test::Exception; +use Test::Differences; use Data::Dumper; use SQL::Translator; use SQL::Translator::Schema::Constants; @@ -54,7 +55,8 @@ my $want = [ "another_id" number(10) DEFAULT \'2\', "timest" date, PRIMARY KEY ("id"), - CONSTRAINT "Basic_emailuniqueindex" UNIQUE ("email") + CONSTRAINT "u_Basic_emailuniqueindex" UNIQUE ("email"), + CONSTRAINT "u_Basic_very_long_index_name_o" UNIQUE ("title") )', 'DROP TABLE "Another" CASCADE CONSTRAINTS', 'DROP SEQUENCE "sq_Another_id"', @@ -101,7 +103,7 @@ END; is_deeply(\@sql, $want, 'Got correct Oracle statements in list context'); -is($sql_string, q|DROP TABLE "Basic" CASCADE CONSTRAINTS; +eq_or_diff($sql_string, q|DROP TABLE "Basic" CASCADE CONSTRAINTS; DROP SEQUENCE "sq_Basic_id01"; @@ -118,7 +120,8 @@ CREATE TABLE "Basic" ( "another_id" number(10) DEFAULT '2', "timest" date, PRIMARY KEY ("id"), - CONSTRAINT "Basic_emailuniqueindex" UNIQUE ("email") + CONSTRAINT "u_Basic_emailuniqueindex01" UNIQUE ("email"), + CONSTRAINT "u_Basic_very_long_index_name01" UNIQUE ("title") ); DROP TABLE "Another" CASCADE CONSTRAINTS; diff --git a/t/data/xml/schema.xml b/t/data/xml/schema.xml index 685ceff..b57a4de 100644 --- a/t/data/xml/schema.xml +++ b/t/data/xml/schema.xml @@ -54,6 +54,7 @@ Created on Fri Aug 15 15:08:18 2003 +