diff Lib/IMPL/SQL/Schema/MySQL/Processor.pm @ 283:2f06250bab5f

*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
author sergey
date Tue, 12 Feb 2013 18:43:32 +0400
parents 56364d0c4b4f
children
line wrap: on
line diff
--- 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;