add sleep 1 to t/admin/02ddl.t so insert into upgrade table does not happen too quickly
[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 (), <Gordon.irving@sophos.com>
12 #      COMPANY:  Sophos
13 #      VERSION:  1.0
14 #      CREATED:  28/11/09 16:14:21 GMT
15 #     REVISION:  ---
16 #===============================================================================
17
18 use strict;
19 use warnings;
20
21 use Test::More;                      # last test to print
22
23 use Test::Exception;
24
25 use Path::Class;
26 use FindBin qw($Bin);
27
28 use Module::Load;
29
30 use lib dir($Bin,'..', '..','lib')->stringify;
31 use lib dir($Bin,'..', 'lib')->stringify;
32
33 use ok 'DBIx::Class::Admin';
34
35 use DBICTest;
36
37 my $sql_dir = dir($Bin,"..","var");
38
39 { # create the schema
40
41 #  make sure we are  clean
42 clean_dir($sql_dir);
43
44 # create a DBICTest so we can steal its connect info
45 my $schema = DBICTest->init_schema(
46     no_deploy=>1,
47     no_populate=>1,
48         );
49
50
51 my $admin = DBIx::Class::Admin->new(
52         schema_class=> "DBICTest::Schema",
53         sql_dir=> $sql_dir,
54         connect_info => $schema->storage->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 load 'DBICVersionOrig';
71
72 my $admin = DBIx::Class::Admin->new(
73         schema_class => 'DBICVersion::Schema', 
74         sql_dir =>  $sql_dir,
75         connect_info => $schema->storage->connect_info(),
76 );
77 lives_ok { $admin->create($schema->storage->sqlt_type(), {add_drop_table=>0}); } 'Can create DBICVersionOrig sql in ' . $schema->storage->sqlt_type;
78 lives_ok { $admin->deploy(  ) } 'Can Deploy schema';
79
80 # connect to now deployed schema
81 lives_ok { $schema = DBICVersion::Schema->connect(@{$schema->storage->connect_info()}); } 'Connect to deployed Database';
82
83 is($schema->get_db_version, $DBICVersion::Schema::VERSION, 'Schema deployed and versions match');
84
85
86 load 'DBICVersionNew';
87
88 $admin = DBIx::Class::Admin->new(
89         schema_class => 'DBICVersion::Schema', 
90         sql_dir =>  "t/var",
91         connect_info => $schema->storage->connect_info(),
92 );
93
94 $admin->preversion("1.0");
95 lives_ok { $admin->create($schema->storage->sqlt_type(), ); } 'Can create diff for ' . $schema->storage->sqlt_type;
96 # sleep required for upgrade table to hold a distinct time of upgrade value
97 # otherwise the returned of get_db_version can be undeterministic
98 sleep 1;
99 lives_ok {$admin->upgrade();} 'upgrade the schema';
100
101 is($schema->get_db_version, $DBICVersion::Schema::VERSION, 'Schema and db versions match');
102
103 }
104
105 { # install
106
107 clean_dir($sql_dir);
108
109 my $schema = DBICTest->init_schema(
110     no_deploy=>1,
111     no_populate=>1,
112         sqlite_use_file => 1,
113         );
114
115 my $admin = DBIx::Class::Admin->new(
116         schema_class    => 'DBICVersion::Schema', 
117         sql_dir                 => $sql_dir,
118         connect_info    => $schema->storage->connect_info(),
119         _confirm                => 1,
120 );
121
122 $admin->version("3.0");
123 lives_ok { $admin->install(); } 'install schema version 3.0';
124 is($admin->schema->get_db_version, "3.0", 'db thinks its version 3.0');
125 dies_ok { $admin->install("4.0"); } 'cannot install to allready existing version';
126 sleep 1;
127 $admin->force(1);
128 lives_ok { $admin->install("4.0"); } 'can force install to allready existing version';
129 is($admin->schema->get_db_version, "4.0", 'db thinks its version 4.0');
130 #clean_dir($sql_dir);
131 }
132
133 sub clean_dir {
134         my ($dir)  =@_;
135         $dir = $dir->resolve;
136         if ( ! -d $dir ) {
137                 $dir->mkpath();
138         }
139         foreach my $file ($dir->children) {
140                 # skip any hidden files
141                 next if ($file =~ /^\./); 
142                 unlink $file;
143         }
144 }
145
146 done_testing;