# HG changeset patch # User sergey # Date 1360680212 -14400 # Node ID 2f06250bab5f3f958c90a1b5547c3f97d8121189 # Parent 68d905f8dc433d68aa5c66e66c159f060d9604a5 *IMPL::SQL::MySQL fixed issues with foreign keys and drop table diff -r 68d905f8dc43 -r 2f06250bab5f Lib/IMPL/SQL/Schema/MySQL/Formatter.pm --- a/Lib/IMPL/SQL/Schema/MySQL/Formatter.pm Tue Feb 12 01:24:36 2013 +0400 +++ b/Lib/IMPL/SQL/Schema/MySQL/Formatter.pm Tue Feb 12 18:43:32 2013 +0400 @@ -13,7 +13,7 @@ CharType => '-IMPL::SQL::Schema::MySQL::CharType', EnumType => '-IMPL::SQL::Schema::MySQL::EnumType', TraitsDropTable => '-IMPL::SQL::Schema::Traits::DropTable', - TraitsCreateTable => '-IMPL::SQL::Schema::Traits::CreateTable', + TraitsCreateTable => '-IMPL::SQL::Schema::Traits::CreateTable', TraitsAlterTableDropConstraint => '-IMPL::SQL::Schema::Traits::AlterTableDropConstraint', TraitsAlterTableAddConstraint => '-IMPL::SQL::Schema::Traits::AlterTableAddConstraint', TraitsAlterTableDropColumn => '-IMPL::SQL::Schema::Traits::AlterTableDropColumn', diff -r 68d905f8dc43 -r 2f06250bab5f Lib/IMPL/SQL/Schema/MySQL/Processor.pm --- a/Lib/IMPL/SQL/Schema/MySQL/Processor.pm Tue Feb 12 01:24:36 2013 +0400 +++ b/Lib/IMPL/SQL/Schema/MySQL/Processor.pm Tue Feb 12 18:43:32 2013 +0400 @@ -8,9 +8,11 @@ MySQLFormatter => 'IMPL::SQL::Schema::MySQL::Formatter', AlterTableDropConstraint => '-IMPL::SQL::Schema::Traits::AlterTableDropConstraint', AlterTableAddConstraint => '-IMPL::SQL::Schema::Traits::AlterTableAddConstraint', - CreateTable => '-IMPL::SQL::Schema::Traits::CreateTable', - Table => '-IMPL::SQL::Schema::Traits::Table', - ForeignKey => '-IMPL::SQL::Schema::Traits::ForeignKey', + DropTable => '-IMPL::SQL::Schema::Traits::DropTable', + PrimitiveDropTable => '-IMPL::SQL::Schema::MySQL::Processor::PrimitiveDropTable', + CreateTable => '-IMPL::SQL::Schema::Traits::CreateTable', + Table => '-IMPL::SQL::Schema::Traits::Table', + ForeignKey => '-IMPL::SQL::Schema::Traits::ForeignKey', }, base => [ @@ -83,6 +85,18 @@ } } + if (is($op, DropTable)) { + my $table = $this->dbSchema->GetTable($op->tableName); + + if(my $pk = $table->primaryKey) { + $this->ApplyOperation($_,$iteration) + foreach + map + AlterTableDropConstraint->new($_->table->name,$_->name), + values %{$pk->connectedFK || {}}; + } + } + $this->next::method($op,$iteration); $this->AddSqlBatch( @@ -90,4 +104,47 @@ ); } +package IMPL::SQL::Schema::MySQL::Processor::PrimitiveDropTable; +use IMPL::Const qw(:prop); +use IMPL::declare { + require => { + ArgException => '-IMPL::InvalidArgumentException' + }, + base => [ + 'IMPL::Object' => undef + ], + props => [ + tableName => PROP_RO, + ] +}; + +sub CTOR { + my ($this,$tableName) = @_; + + $this->tableName($tableName) or die ArgException->new("tableName is required"); +} + +sub CanApply { + my ($this,$schema) = @_; + + my $table = $schema->GetTable( $this->tableName ) + or return 0; + + my $pk = $table->primaryKey + or return 1; + + my $canDrop = keys(%{$pk->connectedFK || {}}) ? 0 : 1; + + warn "Can drop ", $this->tableName + if $canDrop; + + return $canDrop; +} + +sub Apply { + my ($this,$schema) = @_; + + $schema->RemoveTable($this->tableName); +} + 1; diff -r 68d905f8dc43 -r 2f06250bab5f Lib/IMPL/SQL/Schema/Traits.pm --- a/Lib/IMPL/SQL/Schema/Traits.pm Tue Feb 12 01:24:36 2013 +0400 +++ b/Lib/IMPL/SQL/Schema/Traits.pm Tue Feb 12 18:43:32 2013 +0400 @@ -540,7 +540,8 @@ sub CanApply { my ($this,$schema) = @_; - my $table = $schema->GetTable($this->tableName); + my $table = $schema->GetTable($this->tableName) + or return 0; my $constraint = $table->GetConstraint($this->constraintName) or return 0;