change passing of preversion to be a parameter
[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 lives_ok { $admin->create($schema->storage->sqlt_type(), {}, "1.0" ); } 'Can create diff for ' . $schema->storage->sqlt_type;
95 # sleep required for upgrade table to hold a distinct time of upgrade value
96 # otherwise the returned of get_db_version can be undeterministic
97 sleep 1;
98 lives_ok {$admin->upgrade();} 'upgrade the schema';
99
100 is($schema->get_db_version, $DBICVersion::Schema::VERSION, 'Schema and db versions match');
101
102 }
103
104 { # install
105
106 clean_dir($sql_dir);
107
108 my $schema = DBICTest->init_schema(
109     no_deploy=>1,
110     no_populate=>1,
111         sqlite_use_file => 1,
112         );
113
114 my $admin = DBIx::Class::Admin->new(
115         schema_class    => 'DBICVersion::Schema', 
116         sql_dir                 => $sql_dir,
117         connect_info    => $schema->storage->connect_info(),
118         _confirm                => 1,
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 lives_ok { $admin->install("4.0"); } 'can force install to allready existing version';
128 is($admin->schema->get_db_version, "4.0", 'db thinks its version 4.0');
129 #clean_dir($sql_dir);
130 }
131
132 sub clean_dir {
133         my ($dir)  =@_;
134         $dir = $dir->resolve;
135         if ( ! -d $dir ) {
136                 $dir->mkpath();
137         }
138         foreach my $file ($dir->children) {
139                 # skip any hidden files
140                 next if ($file =~ /^\./); 
141                 unlink $file;
142         }
143 }
144
145 done_testing;