More tets cleanup
[dbsrgits/DBIx-Class.git] / t / admin / 02ddl.t
1 #
2 #===============================================================================
3 #
4 #         FILE:  02admin..t
5 #
6 #  DESCRIPTION:  
7 #
8 #        FILES:  ---
9 #         BUGS:  ---
10 #        NOTES:  ---
11 #       AUTHOR:  Gordon Irving (), <goraxe@cpan.org>
12 #      VERSION:  1.0
13 #      CREATED:  28/11/09 16:14:21 GMT
14 #     REVISION:  ---
15 #===============================================================================
16
17 use strict;
18 use warnings;
19
20 use Test::More;
21 use Test::Exception;
22 use Test::Warn;
23
24
25 BEGIN {
26     eval "use DBIx::Class::Admin";
27     plan skip_all => "Deps not installed: $@" if $@;
28 }
29
30
31 use lib qw(t/lib);
32 use DBICTest;
33
34 use Path::Class;
35
36 use ok 'DBIx::Class::Admin';
37
38
39 my $sql_dir = dir(qw/t var/);
40 my @connect_info = DBICTest->_database(
41   no_deploy=>1,
42   no_populate=>1,
43   sqlite_use_file  => 1,
44 );
45 { # create the schema
46
47 #  make sure we are  clean
48 clean_dir($sql_dir);
49
50
51 my $admin = DBIx::Class::Admin->new(
52   schema_class=> "DBICTest::Schema",
53   sql_dir=> $sql_dir,
54   connect_info => \@connect_info, 
55 );
56 isa_ok ($admin, 'DBIx::Class::Admin', 'create the admin object');
57 lives_ok { $admin->create('MySQL'); } 'Can create MySQL sql';
58 lives_ok { $admin->create('SQLite'); } 'Can Create SQLite sql';
59 }
60
61 { # upgrade schema
62
63 #my $schema = DBICTest->init_schema(
64 #  no_deploy    => 1,
65 #  no_populat    => 1,
66 #  sqlite_use_file  => 1,
67 #);
68
69 clean_dir($sql_dir);
70 require DBICVersionOrig;
71
72 my $admin = DBIx::Class::Admin->new(
73   schema_class => 'DBICVersion::Schema', 
74   sql_dir =>  $sql_dir,
75   connect_info => \@connect_info,
76 );
77
78 my $schema = $admin->schema();
79
80 lives_ok { $admin->create($schema->storage->sqlt_type(), {add_drop_table=>0}); } 'Can create DBICVersionOrig sql in ' . $schema->storage->sqlt_type;
81 lives_ok { $admin->deploy(  ) } 'Can Deploy schema';
82
83 # connect to now deployed schema
84 lives_ok { $schema = DBICVersion::Schema->connect(@{$schema->storage->connect_info()}); } 'Connect to deployed Database';
85
86 is($schema->get_db_version, $DBICVersion::Schema::VERSION, 'Schema deployed and versions match');
87
88
89 require DBICVersionNew;
90
91 $admin = DBIx::Class::Admin->new(
92   schema_class => 'DBICVersion::Schema', 
93   sql_dir =>  $sql_dir,
94   connect_info => \@connect_info
95 );
96
97 lives_ok { $admin->create($schema->storage->sqlt_type(), {}, "1.0" ); } 'Can create diff for ' . $schema->storage->sqlt_type;
98 # sleep required for upgrade table to hold a distinct time of upgrade value
99 # otherwise the returned of get_db_version can be undeterministic
100 sleep 1;
101 {
102   local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DB version .+? is lower than the schema version/ };
103   lives_ok {$admin->upgrade();} 'upgrade the schema';
104 }
105
106 is($schema->get_db_version, $DBICVersion::Schema::VERSION, 'Schema and db versions match');
107
108 }
109
110 { # install
111
112 clean_dir($sql_dir);
113
114 my $admin = DBIx::Class::Admin->new(
115   schema_class  => 'DBICVersion::Schema', 
116   sql_dir      => $sql_dir,
117   _confirm    => 1,
118   connect_info  => \@connect_info,
119 );
120
121 $admin->version("3.0");
122 lives_ok { $admin->install(); } 'install schema version 3.0';
123 is($admin->schema->get_db_version, "3.0", 'db thinks its version 3.0');
124 dies_ok { $admin->install("4.0"); } 'cannot install to allready existing version';
125 sleep 1;
126 $admin->force(1);
127 warnings_exist ( sub {
128   lives_ok { $admin->install("4.0") } 'can force install to allready existing version'
129 }, qr/Forcing install may not be a good idea/, 'Force warning emitted' );
130 is($admin->schema->get_db_version, "4.0", 'db thinks its version 4.0');
131 #clean_dir($sql_dir);
132 }
133
134 sub clean_dir {
135   my ($dir) = @_;
136   $dir = $dir->resolve;
137   if ( ! -d $dir ) {
138     $dir->mkpath();
139   }
140   foreach my $file ($dir->children) {
141     # skip any hidden files
142     next if ($file =~ /^\./); 
143     unlink $file;
144   }
145 }
146
147 done_testing;