Mercurial > pub > Impl
comparison _test/Test/SQL/Diff.pm @ 271:56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
| author | cin |
|---|---|
| date | Mon, 28 Jan 2013 02:43:14 +0400 |
| parents | 4d0e1962161c |
| children | 8d36073411b1 |
comparison
equal
deleted
inserted
replaced
| 270:3f59fd828d5f | 271:56364d0c4b4f |
|---|---|
| 1 package Test::SQL::Diff; | 1 package Test::SQL::Diff; |
| 2 use strict; | 2 use strict; |
| 3 use warnings; | 3 use warnings; |
| 4 use parent qw(IMPL::Test::Unit); | 4 |
| 5 use IMPL::declare { | |
| 6 require => { | |
| 7 MySQLProcessor => 'IMPL::SQL::Schema::MySQL::Processor', | |
| 8 SQLSchema => 'IMPL::SQL::Schema', | |
| 9 SQLDiff => 'IMPL::SQL::Schema::Diff' | |
| 10 }, | |
| 11 base => [ | |
| 12 'IMPL::Test::Unit' => '@_' | |
| 13 ] | |
| 14 }; | |
| 5 | 15 |
| 6 use IMPL::Test qw(test failed assert); | 16 use IMPL::Test qw(test failed assert); |
| 7 use IMPL::SQL::Schema(); | |
| 8 use IMPL::SQL::Types qw(Integer Varchar Text); | 17 use IMPL::SQL::Types qw(Integer Varchar Text); |
| 9 use IMPL::SQL::Schema::Traits::Diff(); | |
| 10 use Data::Dumper; | 18 use Data::Dumper; |
| 11 | 19 |
| 12 __PACKAGE__->PassThroughArgs; | |
| 13 | 20 |
| 14 test diff => sub { | 21 test diff => sub { |
| 15 my $schemaSrc = new IMPL::SQL::Schema(name => 'simple', version => 1 ); | 22 my $schemaSrc = SQLSchema->new(name => 'simple', version => 1 ); |
| 16 | 23 |
| 17 my $tbl = $schemaSrc->AddTable({ | 24 my $schemaDst = SQLSchema->new(name => 'simple', version => 2 ); |
| 18 name => 'User', | |
| 19 columns => [ | |
| 20 { name => 'name', type => Varchar(255) }, | |
| 21 { name => 'description', type => Varchar(255) } | |
| 22 ] | |
| 23 }); | |
| 24 | |
| 25 $tbl->AddConstraint( unique => { name => 'unique_name', columns => ['name'] }); | |
| 26 | |
| 27 my $schemaDst = new IMPL::SQL::Schema(name => 'simple', version => 2 ); | |
| 28 | 25 |
| 29 my $users = $schemaDst->AddTable({ | 26 my $users = $schemaDst->AddTable({ |
| 30 name => 'User', | 27 name => 'User', |
| 31 columns => [ | 28 columns => [ |
| 32 { name => 'id', type => Integer }, | 29 { name => 'id', type => Integer, tag => {auto_increment => 1} }, |
| 33 { name => 'login', type => Varchar(255) }, | 30 { name => 'login', type => Varchar(255) }, |
| 34 { name => 'description', type => Text, isNullable => 1 } | 31 { name => 'description', type => Text, isNullable => 1 } |
| 35 ] | 32 ] |
| 36 }); | 33 }); |
| 37 | 34 |
| 38 $users->SetPrimaryKey('id'); | 35 $users->SetPrimaryKey('id'); |
| 39 $users->AddConstraint( unique => { name => 'unique_login', columns => ['login'] } ); | 36 $users->AddConstraint( unique => { name => 'unique_login', columns => ['login'] } ); |
| 40 | 37 |
| 41 #warn Dumper(IMPL::SQL::Schema::Traits::Diff->Diff($schemaSrc,$schemaDst)); | 38 my $profile = $schemaDst->AddTable({ |
| 39 name => 'Profile', | |
| 40 columns => [ | |
| 41 { name => 'id', type => Integer, tag => {auto_increment => 1} }, | |
| 42 { name => 'uid', type => Integer }, | |
| 43 { name => 'data', type => Text } | |
| 44 ] | |
| 45 }); | |
| 46 | |
| 47 $profile->SetPrimaryKey('id'); | |
| 48 $profile->LinkTo($users, 'uid'); | |
| 49 | |
| 50 my $diff = SQLDiff->Diff($schemaSrc,$schemaDst); | |
| 51 | |
| 52 my $processor = MySQLProcessor->new($schemaSrc); | |
| 53 $processor->ProcessBatch($diff); | |
| 54 | |
| 55 warn Dumper($diff); | |
| 56 warn Dumper($processor->sqlBatch); | |
| 42 | 57 |
| 43 $schemaSrc->Dispose; | 58 $schemaSrc->Dispose; |
| 44 $schemaDst->Dispose; | 59 $schemaDst->Dispose; |
| 45 | 60 |
| 46 }; | 61 }; |
