annotate lib/IMPL/SQL/Schema/Diff.pm @ 418:3f38dabaf5cc ref20150831

sync
author cin
date Mon, 28 Dec 2015 15:11:35 +0300
parents c6e90e02dd17
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::SQL::Schema::Diff;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3 use warnings;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4 use IMPL::lang qw(:compare :hash is typeof);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 use IMPL::SQL::Schema::Traits();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 use IMPL::require {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 Schema => 'IMPL::SQL::Schema',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 ForeignKey => '-IMPL::SQL::Schema::Constraint::ForeignKey',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 PrimaryKey => '-IMPL::SQL::Schema::Constraint::PrimaryKey',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 UniqueConstraint =>'-IMPL::SQL::Schema::Constraint::Unique',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 Index => '-IMPL::SQL::Schema::Constraint::Index',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 TraitsForeignKey => '-IMPL::SQL::Schema::Traits::ForeignKey',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 TraitsPrimaryKey => '-IMPL::SQL::Schema::Traits::PrimaryKey',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 TraitsUnique => '-IMPL::SQL::Schema::Traits::Unique',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 TraitsIndex => '-IMPL::SQL::Schema::Traits::Index',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 TraitsDropTable => '-IMPL::SQL::Schema::Traits::DropTable',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 TraitsCreateTable => '-IMPL::SQL::Schema::Traits::CreateTable',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 TraitsTable => '-IMPL::SQL::Schema::Traits::Table',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 TraitsColumn => '-IMPL::SQL::Schema::Traits::Column',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 TraitsAlterTableDropConstraint => '-IMPL::SQL::Schema::Traits::AlterTableDropConstraint',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 TraitsAlterTableAddConstraint => '-IMPL::SQL::Schema::Traits::AlterTableAddConstraint',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 TraitsAlterTableDropColumn => '-IMPL::SQL::Schema::Traits::AlterTableDropColumn',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 TraitsAlterTableAddColumn => '-IMPL::SQL::Schema::Traits::AlterTableAddColumn',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 TraitsAlterTableChangeColumn => '-IMPL::SQL::Schema::Traits::AlterTableChangeColumn',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 Exception => 'IMPL::Exception',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 ArgException => '-IMPL::InvalidArgumentException'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 sub Diff {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 my ($self,$src,$dst) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 die ArgException->new( src => "A valid source schema is required") unless is($src,Schema);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 die ArgException->new( dst => "A valid desctination schema is requried" ) unless is($src,Schema);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 my %dstTables = map { $_->name, $_ } $dst->GetTables;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 my @operations;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 foreach my $srcTable ( $src->GetTables) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 my $dstTable = delete $dstTables{$srcTable->name};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 if (not $dstTable) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 # if a source table doesn't have a corresponding destination table, it should be deleted
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 push @operations, TraitsDropTable->new($srcTable->name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 # a source table needs to be updated
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 push @operations, $self->_DiffTables($srcTable,$dstTable);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 foreach my $tbl ( values %dstTables ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 push @operations, TraitsCreateTable->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 TraitsTable->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 $tbl->name,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 [ map _Column2Traits($_), @{$tbl->columns} ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 [ map _Constraint2Traits($_), $tbl->GetConstraints()],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 $tbl->{tag}
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61 )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 return \@operations;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 sub _DiffTables {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 my ($self,$src,$dst) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 my @dropConstraints;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 my @createConstraints;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 my %srcConstraints = map { $_->name, $_ } $src->GetConstraints();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 my %dstConstraints = map { $_->name, $_ } $dst->GetConstraints();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 foreach my $cnSrcName (keys %srcConstraints) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 if ( my $cnDst = delete $dstConstraints{$cnSrcName} ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 unless ( $srcConstraints{$cnSrcName}->SameValue($cnDst) ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 push @dropConstraints,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 TraitsAlterTableDropConstraint->new( $src->name, $cnSrcName );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 push @createConstraints,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 TraitsAlterTableAddConstraint->new( $dst->name, _Constraint2Traits($cnDst) );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 push @dropConstraints,TraitsAlterTableDropConstraint->new( $src->name, $cnSrcName );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 foreach my $cnDst (values %dstConstraints) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91 push @createConstraints,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 TraitsAlterTableAddConstraint->new( $dst->name, _Constraint2Traits($cnDst) );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95 my @deleteColumns;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 my @addColumns;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97 my @updateColumns;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99 my %dstColumnIndexes = map {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100 my $col = $dst->GetColumnAt($_);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101 ($col->name, { column => $col, index => $_ })
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 } 0 .. $dst->ColumnsCount-1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 my @columns;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106 # remove old columns, mark for update changed columns
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107 for( my $i=0; $i < $src->ColumnsCount; $i++) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108 my $colSrc = $src->GetColumnAt($i);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 if ( my $infoDst = delete $dstColumnIndexes{$colSrc->name} ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 $infoDst->{prevColumn} = $colSrc;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 push @columns,$infoDst;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 push @deleteColumns,TraitsAlterTableDropColumn->new($src->name,$colSrc->name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 #insert new columns at specified positions
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 foreach ( sort { $a->{index} <=> $b->{index} } values %dstColumnIndexes ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 splice(@columns,$_->{index},0,$_);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121 push @addColumns, TraitsAlterTableAddColumn->new($src->name, _Column2Traits( $_->{column}, position => $_->{index} ));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124 # remember old indexes
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125 for(my $i =0; $i< @columns; $i ++) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126 $columns[$i]->{prevIndex} = $i;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 # reorder columns
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130 @columns = sort { $a->{index} <=> $b->{index} } @columns;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132 foreach my $info (@columns) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133 if ($info->{prevColumn} && ( !$info->{column}->SameValue($info->{prevColumn}) or $info->{index}!= $info->{prevIndex} ) ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134 my $op = TraitsAlterTableChangeColumn->new($src->name,$info->{column}->name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136 $op->position( $info->{index} ) unless $info->{prevIndex} == $info->{index};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137 $op->isNullable( $info->{column}->isNullable ) unless equals($info->{column}->isNullable,$info->{prevColumn}->isNullable);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138 $op->defaultValue( $info->{column}->defaultValue ) unless equals($info->{column}->defaultValue, $info->{prevColumn}->defaultValue);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140 my $diff = hashDiff($info->{prevColumn}->tag,$info->{column}->tag);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141 $op->options($diff) if %$diff;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143 push @updateColumns, $op;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
145 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
146
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
147 my @result = (@dropConstraints, @deleteColumns, @addColumns, @updateColumns, @createConstraints);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
148
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
149 return @result;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
150 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
151
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
152 sub _Column2Traits {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
153 my ($column,%options) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
154
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
155 return TraitsColumn->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
156 $column->name,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
157 $column->type,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
158 isNullable => $column->isNullable,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
159 defaultValue => $column->defaultValue,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
160 tag => $column->tag,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
161 %options
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
162 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
163 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
164
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
165 sub _Constraint2Traits {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
166 my ($constraint) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
167
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
168 my $map = {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
169 ForeignKey , TraitsForeignKey,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
170 PrimaryKey , TraitsPrimaryKey,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
171 UniqueConstraint , TraitsUnique,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
172 Index , TraitsIndex
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
173 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
174
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
175 my $class = $map->{typeof($constraint)} or die Exception->new("Can't map the constraint",typeof($constraint));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
176
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
177 if ($class eq TraitsForeignKey) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
178 return $class->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
179 $constraint->name,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
180 [ map $_->name, $constraint->columns ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
181 $constraint->referencedPrimaryKey->table->name,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
182 [ map $_->name, $constraint->referencedPrimaryKey->columns ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
183 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
184 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
185 return $class->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
186 $constraint->name,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
187 [ map $_->name, $constraint->columns ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
188 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
189 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
190 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
191
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
192 1;