annotate Lib/Schema/DB/Traits.pm @ 0:03e58a454b20

Создан репозитарий
author Sergey
date Tue, 14 Jul 2009 12:54:37 +0400
parents
children 16ada169ca75
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
1 package Schema::DB::Traits;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
2 use strict;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
3 use Common;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
4 our @ISA = qw (Object);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
5
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
6 use constant {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
7 STATE_NORMAL => 0,
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
8 STATE_UPDATED => 1,
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
9 STATE_CREATED => 2,
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
10 STATE_REMOVED => 3,
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
11 STATE_PENDING => 4
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
12 } ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
13
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
14 BEGIN {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
15 DeclareProperty SrcSchema => ACCESS_NONE;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
16 DeclareProperty DstSchema => ACCESS_NONE;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
17 DeclareProperty PendingActions => ACCESS_READ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
18 DeclareProperty TableInfo => ACCESS_READ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
19 DeclareProperty Handler => ACCESS_READ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
20 DeclareProperty TableMap => ACCESS_NONE;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
21 DeclareProperty KeepTables => ACCESS_ALL;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
22 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
23
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
24 sub CTOR {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
25 my $this = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
26 $this->SUPER::CTOR(@_);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
27
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
28 $this->{$SrcSchema} or die new Exception('A source schema is required');
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
29 $this->{$DstSchema} or die new Exception('A destination schema is required');
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
30 $this->{$Handler} or die new Exception('A handler is required to produce the update batch');
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
31
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
32 $this->{$TableInfo} = {};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
33 $this->{$PendingActions} = [];
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
34
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
35 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
36
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
37 sub UpdateTable {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
38 my ($this,$srcTable) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
39
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
40 return 1 if $this->{$TableInfo}->{$srcTable->Name}->{'processed'};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
41
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
42 my $dstTableName = $this->{$TableMap}->{$srcTable->Name} ? $this->{$TableMap}->{$srcTable->Name} : $srcTable->Name;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
43 my $dstTable = $this->{$DstSchema}->Tables->{$dstTableName};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
44
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
45 $this->{$TableInfo}->{$srcTable->Name}->{'processed'} = 1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
46
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
47 if (not $dstTable) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
48 $this->DropTable($srcTable) if not $this->{$KeepTables};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
49 return 1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
50 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
51
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
52 if ( not grep {$srcTable->Column($_->Name)} $dstTable->Columns ) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
53
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
54 $this->{$TableInfo}->{$srcTable->Name}->{'NewName'} = $dstTable->Name if $srcTable->Name ne $dstTable->Name;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
55
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
56 $this->DropTable($srcTable);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
57 $this->CreateTable($dstTable);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
58
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
59 return 1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
60 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
61
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
62 if ($srcTable->Name ne $dstTableName) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
63 $this->RenameTable($srcTable,$dstTableName);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
64 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
65
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
66 my %dstConstraints = %{$dstTable->Constraints};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
67
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
68 foreach my $srcConstraint (values %{$srcTable->Constraints}) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
69 if (my $dstConstraint = delete $dstConstraints{$srcConstraint->Name}) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
70 $this->UpdateConstraint($srcConstraint,$dstConstraint);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
71 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
72 $this->DropConstraint($srcConstraint);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
73 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
74 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
75
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
76 my $i = 0;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
77 my %dstColumns = map { $_->Name, $i++} $dstTable->Columns ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
78
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
79 # ñíà÷àëà óäàëÿåì ñòîëáöû
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
80 # ïîòîì äîáàâëÿåì íåäîñòàþùèå è èçìåíÿåì ñòîëáöû â íóæíîì ïîðÿäêå
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
81
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
82 my @columnsToUpdate;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
83
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
84 foreach my $srcColumn ($srcTable->Columns) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
85 if (defined (my $dstColumnIndex = delete $dstColumns{$srcColumn->Name})) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
86 push @columnsToUpdate, { Action => 'update', ColumnSrc => $srcColumn, ColumnDst => $dstTable->ColumnAt($dstColumnIndex), NewPosition => $dstColumnIndex};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
87 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
88 $this->DropColumn($srcTable,$srcColumn);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
89 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
90 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
91 push @columnsToUpdate, map { {Action => 'add', ColumnDst => $dstTable->ColumnAt($_), NewPosition => $_} } values %dstColumns;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
92
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
93 foreach my $action (sort {$a->{'NewPosition'} <=> $b->{'NewPosition'}} @columnsToUpdate ) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
94 if ($action->{'Action'} eq 'update') {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
95 $this->UpdateColumn($srcTable,@$action{'ColumnSrc','ColumnDst'},$dstTable,$action->{'NewPosition'}); # change type and position
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
96 }elsif ($action->{'Action'} eq 'add') {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
97 $this->AddColumn($srcTable,$action->{'ColumnDst'},$dstTable,$action->{'NewPosition'}); # add at specified position
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
98 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
99 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
100
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
101 foreach my $dstConstraint (values %dstConstraints) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
102 $this->AddConstraint($dstConstraint);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
103 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
104
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
105 $this->{$TableInfo}{$srcTable->Name}{'State'} = STATE_UPDATED;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
106 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
107
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
108 sub UpdateConstraint {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
109 my ($this,$src,$dst) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
110
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
111 if (not ConstraintEquals($src,$dst)) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
112 if (UNIVERSAL::isa($src,'Schema::DB::Constraint::PrimaryKey')) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
113 $this->UpdateTable($_->Table) foreach values %{$src->ConnectedFK};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
114 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
115 $this->DropConstraint($src);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
116 $this->AddConstraint($dst);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
117 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
118 $this->{$TableInfo}->{$this->MapTableName($src->Table->Name)}->{'Constraints'}->{$src->Name} = STATE_UPDATED;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
119 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
120 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
121
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
122 sub ConstraintEquals {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
123 my ($src,$dst) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
124
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
125 ref $src eq ref $dst or return 0;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
126
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
127 my @dstColumns = $dst->Columns;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
128 scalar(@{$src->Columns}) == scalar(@{$dst->Columns}) and not grep { my $column = shift @dstColumns; not $column->isSame($_) } $src->Columns or return 0;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
129
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
130 not UNIVERSAL::isa($src,'Schema::DB::Constraint::ForeignKey') or ConstraintEquals($src->ReferencedPrimaryKey,$dst->ReferencedPrimaryKey) or return 0;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
131
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
132 1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
133 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
134
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
135 sub UpdateSchema {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
136 my ($this) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
137
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
138 my %Updated = map { $this->UpdateTable($_); $this->MapTableName($_->Name) , 1; } values %{$this->{$SrcSchema}->Tables ? $this->{$SrcSchema}->Tables : {} };
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
139
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
140 $this->CreateTable($_) foreach grep {not $Updated{$_->Name}} values %{$this->{$DstSchema}->Tables};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
141
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
142 $this->ProcessPendingActions();
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
143 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
144
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
145 sub RenameTable {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
146 my ($this,$tblSrc,$tblDstName) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
147
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
148 $this->{$Handler}->AlterTableRename($tblSrc->Name,$tblDstName);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
149 $this->{$TableInfo}->{$tblSrc->Name}->{'NewName'} = $tblDstName;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
150 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
151
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
152 sub MapTableName {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
153 my ($this,$srcName) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
154
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
155 $this->{$TableInfo}->{$srcName}->{'NewName'} ? $this->{$TableInfo}->{$srcName}->{'NewName'} : $srcName;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
156 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
157
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
158 sub DropTable {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
159 my ($this,$tbl) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
160
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
161 if ($tbl->PrimaryKey) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
162 $this->UpdateTable($_->Table) foreach values %{$tbl->PrimaryKey->ConnectedFK};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
163 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
164
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
165 $this->{$Handler}->DropTable($this->MapTableName($tbl->Name));
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
166 $this->{$TableInfo}{$this->MapTableName($tbl->Name)}{'State'} = STATE_REMOVED;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
167 $this->{$TableInfo}{$this->MapTableName($tbl->Name)}{'Constraints'} = {map {$_,STATE_REMOVED} keys %{$tbl->Constraints}};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
168 $this->{$TableInfo}{$this->MapTableName($tbl->Name)}{'Columns'} = {map { $_->Name, STATE_REMOVED} $tbl->Columns};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
169
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
170 return 1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
171 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
172
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
173 sub CreateTable {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
174 my ($this,$tbl) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
175
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
176 # ñîçäàåì òàáëèöó, êðîìå âíåøíèõ êëþ÷åé
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
177 $this->{$Handler}->CreateTable($tbl,skip_foreign_keys => 1);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
178
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
179 $this->{$TableInfo}->{$tbl->Name}->{'State'} = STATE_CREATED;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
180
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
181 $this->{$TableInfo}->{$tbl->Name}->{'Columns'} = {map { $_->Name, STATE_CREATED } $tbl->Columns};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
182 $this->{$TableInfo}->{$tbl->Name}->{'Constraints'} = {map {$_->Name, STATE_CREATED} grep { not UNIVERSAL::isa($_,'Schema::DB::Constraint::ForeignKey') } values %{$tbl->Constraints}};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
183
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
184 $this->AddConstraint($_) foreach grep { UNIVERSAL::isa($_,'Schema::DB::Constraint::ForeignKey') } values %{$tbl->Constraints};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
185
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
186 return 1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
187 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
188
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
189 sub AddColumn {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
190 my ($this,$tblSrc,$column,$tblDst,$pos) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
191
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
192 $this->{$Handler}->AlterTableAddColumn($this->MapTableName($tblSrc->Name),$column,$tblDst,$pos);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
193 $this->{$TableInfo}->{$this->MapTableName($tblSrc->Name)}->{'Columns'}->{$column->Name} = STATE_CREATED;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
194
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
195 return 1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
196 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
197
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
198 sub DropColumn {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
199 my ($this,$tblSrc,$column) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
200 $this->{$Handler}->AlterTableDropColumn($this->MapTableName($tblSrc->Name),$column->Name);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
201 $this->{$TableInfo}->{$this->MapTableName($tblSrc->Name)}->{'Columns'}->{$column->Name} = STATE_REMOVED;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
202
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
203 return 1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
204 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
205
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
206 sub UpdateColumn {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
207 my ($this,$tblSrc,$srcColumn,$dstColumn,$tblDst,$pos) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
208
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
209 if ($srcColumn->isSame($dstColumn) and $pos < @{$tblSrc->Columns} and $tblSrc->ColumnAt($pos) == $srcColumn) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
210 $this->{$TableInfo}->{$this->MapTableName($tblSrc->Name)}->{'Columns'}->{$dstColumn->Name} = STATE_UPDATED;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
211 return 1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
212 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
213
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
214 $this->{$Handler}->AlterTableChangeColumn($this->MapTableName($tblSrc->Name),$dstColumn,$tblDst,$pos);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
215 $this->{$TableInfo}->{$this->MapTableName($tblSrc->Name)}->{'Columns'}->{$dstColumn->Name} = STATE_UPDATED;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
216
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
217 return 1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
218 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
219
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
220 sub DropConstraint {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
221 my ($this,$constraint) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
222
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
223 $this->{$Handler}->AlterTableDropConstraint($this->MapTableName($constraint->Table->Name),$constraint);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
224 $this->{$TableInfo}->{$constraint->Table->Name}->{'Constraints'}->{$constraint->Name} = STATE_REMOVED;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
225
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
226 return 1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
227 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
228
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
229 sub IfUndef {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
230 my ($value,$default) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
231
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
232 return defined $value ? $value : $default;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
233 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
234
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
235 sub AddConstraint {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
236 my ($this,$constraint) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
237
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
238 # ïåðåä äîáàâëåíèåì îãðàíè÷åíèÿ íóæíî óáåäèòüñÿ â òîì, ÷òî ñîçäàíû âñå íåîáõîäèìûå ñòîëáöû è ñîïóòñòâóþùèå
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
239 # îãðàíè÷åíèÿ (íàïðèìåð ïåðâè÷íûå êëþ÷è)
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
240
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
241 my $pending;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
242
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
243 $pending = grep { my $column = $_; not grep { IfUndef($this->{$TableInfo}{$constraint->Table->Name}{'Columns'}{$column->Name}, STATE_NORMAL) == $_ } (STATE_UPDATED, STATE_CREATED) } $constraint->Columns;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
244
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
245 if ($pending) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
246 push @{$this->{$PendingActions}},{Action => \&AddConstraint, Args => [$constraint]};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
247 return 2;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
248 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
249 if (UNIVERSAL::isa($constraint,'Schema::DB::Constraint::ForeignKey')) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
250 if (not grep { IfUndef($this->{$TableInfo}{$constraint->ReferencedPrimaryKey->Table->Name}{'Constraints'}{$constraint->ReferencedPrimaryKey->Name},STATE_NORMAL) == $_} (STATE_UPDATED, STATE_CREATED)) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
251 push @{$this->{$PendingActions}},{Action => \&AddConstraint, Args => [$constraint]};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
252 return 2;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
253 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
254 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
255 $this->{$Handler}->AlterTableAddConstraint($constraint->Table->Name,$constraint);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
256 $this->{$TableInfo}->{$constraint->Table->Name}->{'Constraints'}->{$constraint->Name} = STATE_CREATED;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
257 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
258 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
259
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
260 sub ProcessPendingActions {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
261 my ($this) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
262
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
263 while (my $action = shift @{$this->{$PendingActions}}) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
264 $action->{'Action'}->($this,@{$action->{'Args'}});
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
265 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
266 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
267
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
268 1;