changeset 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 68d905f8dc43
children f2a6bc5f3184
files Lib/IMPL/SQL/Schema/MySQL/Formatter.pm Lib/IMPL/SQL/Schema/MySQL/Processor.pm Lib/IMPL/SQL/Schema/Traits.pm
diffstat 3 files changed, 63 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- 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',
--- 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;
--- 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;