Mercurial > pub > Impl
comparison 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 |
comparison
equal
deleted
inserted
replaced
282:68d905f8dc43 | 283:2f06250bab5f |
---|---|
6 use IMPL::declare { | 6 use IMPL::declare { |
7 require => { | 7 require => { |
8 MySQLFormatter => 'IMPL::SQL::Schema::MySQL::Formatter', | 8 MySQLFormatter => 'IMPL::SQL::Schema::MySQL::Formatter', |
9 AlterTableDropConstraint => '-IMPL::SQL::Schema::Traits::AlterTableDropConstraint', | 9 AlterTableDropConstraint => '-IMPL::SQL::Schema::Traits::AlterTableDropConstraint', |
10 AlterTableAddConstraint => '-IMPL::SQL::Schema::Traits::AlterTableAddConstraint', | 10 AlterTableAddConstraint => '-IMPL::SQL::Schema::Traits::AlterTableAddConstraint', |
11 CreateTable => '-IMPL::SQL::Schema::Traits::CreateTable', | 11 DropTable => '-IMPL::SQL::Schema::Traits::DropTable', |
12 Table => '-IMPL::SQL::Schema::Traits::Table', | 12 PrimitiveDropTable => '-IMPL::SQL::Schema::MySQL::Processor::PrimitiveDropTable', |
13 ForeignKey => '-IMPL::SQL::Schema::Traits::ForeignKey', | 13 CreateTable => '-IMPL::SQL::Schema::Traits::CreateTable', |
14 Table => '-IMPL::SQL::Schema::Traits::Table', | |
15 ForeignKey => '-IMPL::SQL::Schema::Traits::ForeignKey', | |
14 | 16 |
15 }, | 17 }, |
16 base => [ | 18 base => [ |
17 'IMPL::SQL::Schema::Processor' => sub { $_[0] } | 19 'IMPL::SQL::Schema::Processor' => sub { $_[0] } |
18 ], | 20 ], |
81 map AlterTableAddConstraint->new($table->{name},$_), @fk | 83 map AlterTableAddConstraint->new($table->{name},$_), @fk |
82 ); | 84 ); |
83 } | 85 } |
84 } | 86 } |
85 | 87 |
88 if (is($op, DropTable)) { | |
89 my $table = $this->dbSchema->GetTable($op->tableName); | |
90 | |
91 if(my $pk = $table->primaryKey) { | |
92 $this->ApplyOperation($_,$iteration) | |
93 foreach | |
94 map | |
95 AlterTableDropConstraint->new($_->table->name,$_->name), | |
96 values %{$pk->connectedFK || {}}; | |
97 } | |
98 } | |
99 | |
86 $this->next::method($op,$iteration); | 100 $this->next::method($op,$iteration); |
87 | 101 |
88 $this->AddSqlBatch( | 102 $this->AddSqlBatch( |
89 $this->formatter->Format($op,@formatterParams) | 103 $this->formatter->Format($op,@formatterParams) |
90 ); | 104 ); |
91 } | 105 } |
92 | 106 |
107 package IMPL::SQL::Schema::MySQL::Processor::PrimitiveDropTable; | |
108 use IMPL::Const qw(:prop); | |
109 use IMPL::declare { | |
110 require => { | |
111 ArgException => '-IMPL::InvalidArgumentException' | |
112 }, | |
113 base => [ | |
114 'IMPL::Object' => undef | |
115 ], | |
116 props => [ | |
117 tableName => PROP_RO, | |
118 ] | |
119 }; | |
120 | |
121 sub CTOR { | |
122 my ($this,$tableName) = @_; | |
123 | |
124 $this->tableName($tableName) or die ArgException->new("tableName is required"); | |
125 } | |
126 | |
127 sub CanApply { | |
128 my ($this,$schema) = @_; | |
129 | |
130 my $table = $schema->GetTable( $this->tableName ) | |
131 or return 0; | |
132 | |
133 my $pk = $table->primaryKey | |
134 or return 1; | |
135 | |
136 my $canDrop = keys(%{$pk->connectedFK || {}}) ? 0 : 1; | |
137 | |
138 warn "Can drop ", $this->tableName | |
139 if $canDrop; | |
140 | |
141 return $canDrop; | |
142 } | |
143 | |
144 sub Apply { | |
145 my ($this,$schema) = @_; | |
146 | |
147 $schema->RemoveTable($this->tableName); | |
148 } | |
149 | |
93 1; | 150 1; |