Mercurial > pub > Impl
comparison Lib/IMPL/SQL/Schema/Traits/Diff.pm @ 167:1f7a6d762394
SQL schema in progress
| author | sourcer |
|---|---|
| date | Thu, 12 May 2011 08:57:19 +0400 |
| parents | 76515373dac0 |
| children | 6148f89bb7bf |
comparison
equal
deleted
inserted
replaced
| 166:4267a2ac3d46 | 167:1f7a6d762394 |
|---|---|
| 22 | 22 |
| 23 foreach my $srcTable ( $src->GetTables) { | 23 foreach my $srcTable ( $src->GetTables) { |
| 24 my $dstTable = delete $dstTables{$srcTable->name}; | 24 my $dstTable = delete $dstTables{$srcTable->name}; |
| 25 | 25 |
| 26 if (not $dstTable) { | 26 if (not $dstTable) { |
| 27 # if a source table doesn't have a corresponding destination table, it should be deleted | |
| 27 push @operations, new IMPL::SQL::Schema::Traits::DropTable() | 28 push @operations, new IMPL::SQL::Schema::Traits::DropTable() |
| 28 } else { | 29 } else { |
| 29 | 30 # a source table needs to be updated |
| 31 push @operations, $self->_DiffTables($srcTable,$dstTable); | |
| 32 } | |
| 33 | |
| 34 foreach my $tbl ( values %dstTables ) { | |
| 35 push @operations, new IMPL::SQL::Schema::Traits::CreateTable( | |
| 36 new IMPL::SQL::Schema::Traits::Table( | |
| 37 $tbl->name, | |
| 38 [ map _Column2Traits($_), $tbl->columns ], | |
| 39 [ map _Constraint2Traits($_), $tbl->constraints], | |
| 40 $tbl->{tag} | |
| 41 ) | |
| 42 ) | |
| 30 } | 43 } |
| 31 | 44 |
| 32 } | 45 } |
| 33 } | 46 } |
| 34 | 47 |
| 48 sub _DiffTables { | |
| 49 my ($self,$src,$dst) = @_; | |
| 50 | |
| 51 my @dropConstraints; | |
| 52 my @createConstraints; | |
| 53 | |
| 54 my %srcConstraints = map { $_->name, $_ } $src->GetConstraints(); | |
| 55 my %dstConstraints = map { $_->name, $_ } $dst->GetConstraints(); | |
| 56 | |
| 57 foreach my $cnSrcName (keys %srcConstraints) { | |
| 58 if ( my $cnDst = delete $dstConstraints{$cnSrcName} ) { | |
| 59 unless ( $srcConstraints{$cnSrcName}->SameValue($cnDst) ) { | |
| 60 push @dropConstraints, | |
| 61 IMPL::SQL::Schema::Traits::AlterTableDropConstraint->new( $src->name, $cnSrcName ); | |
| 62 push @createConstraints, | |
| 63 IMPL::SQL::Schema::Traits::AlterTableAddConstraint->new( $dst->name, _Constraint2Traits($cnDst) ); | |
| 64 } | |
| 65 } else { | |
| 66 push @dropConstraints, IMPL::SQL::Schema::Traits::AlterTableDropConstrait->new( $src->name, $cnSrcName ); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 foreach my $cnDst (values %dstConstraints) { | |
| 71 push @createConstraints, | |
| 72 IMPL::SQL::Schema::Traits::AlterTableAddConstraint->new( $dst->name, _Constraint2Traits($cnDst) ); | |
| 73 } | |
| 74 | |
| 75 my @deleteColumns; | |
| 76 my @addColumns; | |
| 77 my @updateColumns; | |
| 78 | |
| 79 my %dstColumnIndexes = map { | |
| 80 my $col = $dst->GetColumnAt($_); | |
| 81 ($col->name, { column => $col, index => $_ }) | |
| 82 } 0 .. $dst->ColumnsCount-1; | |
| 83 | |
| 84 # get changed and | |
| 85 | |
| 86 my @columns; | |
| 87 | |
| 88 for( my $i=0; $i < $src->ColumnsCount; $i++) { | |
| 89 my $colSrc = $src->GetColumnAt($i); | |
| 90 | |
| 91 if ( my $infoDst = delete $dstColumnIndexes{$colSrc->name} ) { | |
| 92 $infoDst->{update} = 1 unless $infoDst->{column}->SameValue($colSrc); | |
| 93 push @columns,$infoDst; | |
| 94 } else { | |
| 95 push @deleteColumns, IMPL::SQL::Schema::Traits::AlterTableDropColumn($src->name,$colSrc->name); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 splice(@columns,$_->{index},0,$_) foreach ( sort { $a->{index} <=> $b->{index} } values %dstColumnIndexes ); | |
| 100 | |
| 101 for(my $i =0; $i< @columns; $i ++) { | |
| 102 | |
| 103 } | |
| 104 | |
| 105 # determine constraints to be dropped, | |
| 106 # drop columns | |
| 107 # create columns | |
| 108 # update/reorder columns | |
| 109 # create constraints | |
| 110 } | |
| 111 | |
| 112 sub _Column2Traits { | |
| 113 my ($column) = @_; | |
| 114 | |
| 115 return new IMPL::SQL::Schema::Traits::Columns( | |
| 116 $column->name, | |
| 117 $column->type, | |
| 118 $column->isNullable, | |
| 119 $column->defaultValue, | |
| 120 $column->tag | |
| 121 ); | |
| 122 } | |
| 123 | |
| 124 sub _Constraint2Traits { | |
| 125 my ($constraint) = @_; | |
| 126 | |
| 127 return new IMPL::SQL::Schema::Traits::Constraint( | |
| 128 $constraint->name, | |
| 129 [ map $_->name, $_->columns ] | |
| 130 ) | |
| 131 } | |
| 132 | |
| 35 1; | 133 1; |
