annotate lib/IMPL/SQL/Schema/Traits/Diff.pm @ 414:ec6f2d389d1e ref20150831

working on IMPL::Config::Bag
author cin
date Fri, 02 Oct 2015 06:56:24 +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::Traits::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();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 use IMPL::SQL::Schema::Traits();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 # defining a constant is a good style to enable compile checks
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 use constant {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 schema_t => 'IMPL::SQL::Schema',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 ConstraintForeignKey => 'IMPL::SQL::Schema::Constraint::ForeignKey',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 TraitsForeignKey => 'IMPL::SQL::Schema::Traits::ForeignKey',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 ConstraintPrimaryKey => 'IMPL::SQL::Schema::Constraint::PrimaryKey',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 TraitsPrimaryKey => 'IMPL::SQL::Schema::Traits::PrimaryKey',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 ConstraintUnique => 'IMPL::SQL::Schema::Constraint::Unique',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 TraitsUnique => 'IMPL::SQL::Schema::Traits::Unique',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 ConstraintIndex => 'IMPL::SQL::Schema::Constraint::Index',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 TraitsIndex => 'IMPL::SQL::Schema::Traits::Index'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 sub Diff {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 my ($self,$src,$dst) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 die new IMPL::InvalidArgumentException( src => "A valid source schema is required") unless is($src,schema_t);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 die new IMPL::InvalidArgumentException( dst => "A valid desctination schema is requried" ) unless is($src,schema_t);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 my %dstTables = map { $_->name, $_ } $dst->GetTables;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 my @operations;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 foreach my $srcTable ( $src->GetTables) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 my $dstTable = delete $dstTables{$srcTable->name};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 if (not $dstTable) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 # if a source table doesn't have a corresponding destination table, it should be deleted
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 push @operations, new IMPL::SQL::Schema::Traits::DropTable($srcTable->name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 # a source table needs to be updated
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 push @operations, $self->_DiffTables($srcTable,$dstTable);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 foreach my $tbl ( values %dstTables ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 push @operations, new IMPL::SQL::Schema::Traits::CreateTable(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 new IMPL::SQL::Schema::Traits::Table(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 $tbl->name,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 [ map _Column2Traits($_), @{$tbl->columns} ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 [ map _Constraint2Traits($_), $tbl->GetConstraints()],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 $tbl->{tag}
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 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 return \@operations;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 sub _DiffTables {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 my ($self,$src,$dst) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 my @dropConstraints;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 my @createConstraints;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 my %srcConstraints = map { $_->name, $_ } $src->GetConstraints();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 my %dstConstraints = map { $_->name, $_ } $dst->GetConstraints();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 foreach my $cnSrcName (keys %srcConstraints) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 if ( my $cnDst = delete $dstConstraints{$cnSrcName} ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 unless ( $srcConstraints{$cnSrcName}->SameValue($cnDst) ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 push @dropConstraints,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 new IMPL::SQL::Schema::Traits::AlterTableDropConstraint( $src->name, $cnSrcName );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 push @createConstraints,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 new IMPL::SQL::Schema::Traits::AlterTableAddConstraint( $dst->name, _Constraint2Traits($cnDst) );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 push @dropConstraints,new IMPL::SQL::Schema::Traits::AlterTableDropConstraint( $src->name, $cnSrcName );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 foreach my $cnDst (values %dstConstraints) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 push @createConstraints,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 IMPL::SQL::Schema::Traits::AlterTableAddConstraint->new( $dst->name, _Constraint2Traits($cnDst) );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 my @deleteColumns;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87 my @addColumns;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 my @updateColumns;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 my %dstColumnIndexes = map {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91 my $col = $dst->GetColumnAt($_);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 ($col->name, { column => $col, index => $_ })
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 } 0 .. $dst->ColumnsCount-1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95 my @columns;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97 # remove old columns, mark for update changed columns
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 for( my $i=0; $i < $src->ColumnsCount; $i++) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99 my $colSrc = $src->GetColumnAt($i);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101 if ( my $infoDst = delete $dstColumnIndexes{$colSrc->name} ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 $infoDst->{prevColumn} = $colSrc;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103 push @columns,$infoDst;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105 push @deleteColumns,new IMPL::SQL::Schema::Traits::AlterTableDropColumn($src->name,$colSrc->name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 #insert new columns at specified positions
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 foreach ( sort { $a->{index} <=> $b->{index} } values %dstColumnIndexes ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 splice(@columns,$_->{index},0,$_);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 push @addColumns, new IMPL::SQL::Schema::Traits::AlterTableAddColumn($src->name, _Column2Traits( $_->{column}, position => $_->{index} ));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 # remember old indexes
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 for(my $i =0; $i< @columns; $i ++) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 $columns[$i]->{prevIndex} = $i;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 # reorder columns
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121 @columns = sort { $a->{index} <=> $b->{index} } @columns;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 foreach my $info (@columns) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124 if ($info->{prevColumn} && ( !$info->{column}->SameValue($info->{prevColumn}) or $info->{index}!= $info->{prevIndex} ) ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125 my $op = new IMPL::SQL::Schema::Traits::AlterTableChangeColumn($src->name,$info->{column}->name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 $op->position( $info->{index} ) unless $info->{prevIndex} == $info->{index};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128 $op->isNullable( $info->{column}->isNullable ) unless equals($info->{column}->isNullable,$info->{prevColumn}->isNullable);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 $op->defaultValue( $info->{column}->defaultValue ) unless equals($info->{column}->defaultValue, $info->{prevColumn}->defaultValue);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131 my $diff = hashDiff($info->{prevColumn}->tag,$info->{column}->tag);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132 $op->options($diff) if %$diff;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134 push @updateColumns, $op;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138 my @result = (@dropConstraints, @deleteColumns, @addColumns, @updateColumns, @createConstraints);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140 return @result;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143 sub _Column2Traits {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144 my ($column,%options) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
145
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
146 return new IMPL::SQL::Schema::Traits::Column(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
147 $column->name,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
148 $column->type,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
149 isNullable => $column->isNullable,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
150 defaultValue => $column->defaultValue,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
151 tag => $column->tag,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
152 %options
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
153 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
154 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
155
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
156 sub _Constraint2Traits {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
157 my ($constraint) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
158
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
159 my $map = {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
160 ConstraintForeignKey , TraitsForeignKey,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
161 ConstraintPrimaryKey , TraitsPrimaryKey,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
162 ConstraintUnique , TraitsUnique,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
163 ConstraintIndex , TraitsIndex
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
164 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
165
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
166 my $class = $map->{typeof($constraint)} or die new IMPL::Exception("Can't map the constraint",typeof($constraint));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
167
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
168 return $class->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
169 $constraint->name,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
170 [ map $_->name, $constraint->columns ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
171 )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
172 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
173
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
174 1;