From: Rafael Kitover Date: Tue, 11 Oct 2011 17:33:22 +0000 (-0400) Subject: make MySQL FK handling more robust X-Git-Tag: 0.07011~36 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2fa86d8bd9376b6978fc964dabffbb8bff14d697;p=dbsrgits%2FDBIx-Class-Schema-Loader.git make MySQL FK handling more robust Now handles FKs to schemas not listed in db_schema, and to tables that don't exist at all. --- diff --git a/lib/DBIx/Class/Schema/Loader/DBI/SQLAnywhere.pm b/lib/DBIx/Class/Schema/Loader/DBI/SQLAnywhere.pm index b353861..e92cfe6 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/SQLAnywhere.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/SQLAnywhere.pm @@ -9,6 +9,7 @@ use base qw/ use mro 'c3'; use List::MoreUtils 'any'; use namespace::clean; +use DBIx::Class::Schema::Loader::Table (); our $VERSION = '0.07010'; diff --git a/lib/DBIx/Class/Schema/Loader/DBI/mysql.pm b/lib/DBIx/Class/Schema/Loader/DBI/mysql.pm index 8131ee2..f62162d 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/mysql.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/mysql.pm @@ -9,6 +9,7 @@ use List::Util 'first'; use List::MoreUtils 'any'; use Try::Tiny; use namespace::clean; +use DBIx::Class::Schema::Loader::Table (); our $VERSION = '0.07010'; @@ -88,10 +89,28 @@ sub _table_fk_info { my @f_cols = map { s/$qt//g; $self->_lc($_) } split(/$qt?\s*$qt?,$qt?\s*$qt?/, $f_cols); - my $remote_table = first { - lc($_->name) eq lc($f_table) - && ((not $f_schema) || lc($_->schema) eq lc($f_schema)) - } $self->_tables_list; + my $remote_table = do { + # Get ->tables_list to return tables from the remote schema, in case it is not in the db_schema list. + local $self->{db_schema} = [ $f_schema ] if $f_schema; + + first { + lc($_->name) eq lc($f_table) + && ((not $f_schema) || lc($_->schema) eq lc($f_schema)) + } $self->_tables_list; + }; + + # The table may not be in any database, or it may not have been found by the previous code block for whatever reason. + if (not $remote_table) { + my $remote_schema = $f_schema || $self->db_schema && @{ $self->db_schema } == 1 && $self->db_schema->[0]; + + $remote_table = DBIx::Class::Schema::Loader::Table->new( + loader => $self, + name => $f_table, + ($remote_schema ? ( + schema => $remote_schema, + ) : ()), + ); + } push(@rels, { local_columns => \@cols, diff --git a/t/10_02mysql_common.t b/t/10_02mysql_common.t index 2df8a4b..65292e6 100644 --- a/t/10_02mysql_common.t +++ b/t/10_02mysql_common.t @@ -261,6 +261,22 @@ EOF FOREIGN KEY (mysql_loader_test7_id) REFERENCES `dbicsl.test`.mysql_loader_test7 (id) ) $innodb EOF + # Test dumping a rel to a table that's not part of the dump. + $dbh->do('CREATE DATABASE `dbicsl_test_ignored`'); + $dbh->do(<<"EOF"); + CREATE TABLE `dbicsl_test_ignored`.mysql_loader_test9 ( + id INT AUTO_INCREMENT PRIMARY KEY, + value VARCHAR(100) + ) $innodb +EOF + $dbh->do(<<"EOF"); + CREATE TABLE `dbicsl-test`.mysql_loader_test10 ( + id INT AUTO_INCREMENT PRIMARY KEY, + value VARCHAR(100), + mysql_loader_test9_id INTEGER, + FOREIGN KEY (mysql_loader_test9_id) REFERENCES `dbicsl_test_ignored`.mysql_loader_test9 (id) + ) $innodb +EOF $databases_created = 1; @@ -436,7 +452,9 @@ else { END { if (not $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}) { if ($databases_created && (my $dbh = try { $schema->storage->dbh })) { - foreach my $table ('`dbicsl-test`.mysql_loader_test8', + foreach my $table ('`dbicsl-test`.mysql_loader_test10', + 'dbicsl_test_ignored.mysql_loader_test9', + '`dbicsl-test`.mysql_loader_test8', '`dbicsl.test`.mysql_loader_test7', '`dbicsl.test`.mysql_loader_test6', '`dbicsl-test`.mysql_loader_test5', @@ -449,7 +467,7 @@ END { }; } - foreach my $db (qw/dbicsl-test dbicsl.test/) { + foreach my $db (qw/dbicsl-test dbicsl.test dbicsl_test_ignored/) { try { $dbh->do("DROP DATABASE `$db`"); }