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