add namespace::autoclean to DBIx::Class::Admin
[dbsrgits/DBIx-Class.git] / t / admin / 02ddl.t
CommitLineData
9f3849c3 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
18use strict;
19use warnings;
20
21use Test::More; # last test to print
22
23use Test::Exception;
24
25use Path::Class;
26use FindBin qw($Bin);
2ded40e7 27
28use Module::Load;
29
9f3849c3 30use lib dir($Bin,'..', '..','lib')->stringify;
31use lib dir($Bin,'..', 'lib')->stringify;
32
33use ok 'DBIx::Class::Admin';
34
35use DBICTest;
36
912e2d5a 37my $sql_dir = dir($Bin,"..","var");
9f3849c3 38
9f3849c3 39{ # create the schema
40
2ded40e7 41# make sure we are clean
42clean_dir($sql_dir);
43
44# create a DBICTest so we can steal its connect info
9f3849c3 45my $schema = DBICTest->init_schema(
46 no_deploy=>1,
47 no_populate=>1,
48 );
2ded40e7 49
50
9f3849c3 51my $admin = DBIx::Class::Admin->new(
52 schema_class=> "DBICTest::Schema",
53 sql_dir=> $sql_dir,
54 connect_info => $schema->storage->connect_info()
55);
2ded40e7 56isa_ok ($admin, 'DBIx::Class::Admin', 'create the admin object');
9f3849c3 57lives_ok { $admin->create('MySQL'); } 'Can create MySQL sql';
58lives_ok { $admin->create('SQLite'); } 'Can Create SQLite sql';
59}
60
9f3849c3 61{ # upgrade schema
62
63my $schema = DBICTest->init_schema(
2ded40e7 64 no_deploy => 1,
65 no_populat => 1,
66 sqlite_use_file => 1,
67);
9f3849c3 68
2ded40e7 69clean_dir($sql_dir);
70load 'DBICVersionOrig';
9f3849c3 71
72my $admin = DBIx::Class::Admin->new(
73 schema_class => 'DBICVersion::Schema',
74 sql_dir => $sql_dir,
75 connect_info => $schema->storage->connect_info(),
76);
2ded40e7 77lives_ok { $admin->create($schema->storage->sqlt_type(), {add_drop_table=>0}); } 'Can create DBICVersionOrig sql in ' . $schema->storage->sqlt_type;
78lives_ok { $admin->deploy( ) } 'Can Deploy schema';
79
80# connect to now deployed schema
81lives_ok { $schema = DBICVersion::Schema->connect(@{$schema->storage->connect_info()}); } 'Connect to deployed Database';
82
83is($schema->get_db_version, $DBICVersion::Schema::VERSION, 'Schema deployed and versions match');
84
85
86load 'DBICVersionNew';
87
88$admin = DBIx::Class::Admin->new(
89 schema_class => 'DBICVersion::Schema',
912e2d5a 90 sql_dir => "t/var",
2ded40e7 91 connect_info => $schema->storage->connect_info(),
92);
93
4858d9ac 94lives_ok { $admin->create($schema->storage->sqlt_type(), {}, "1.0" ); } 'Can create diff for ' . $schema->storage->sqlt_type;
6daebebc 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
97sleep 1;
1a01a17d 98lives_ok {$admin->upgrade();} 'upgrade the schema';
2ded40e7 99
912e2d5a 100is($schema->get_db_version, $DBICVersion::Schema::VERSION, 'Schema and db versions match');
101
102}
103
104{ # install
105
106clean_dir($sql_dir);
107
108my $schema = DBICTest->init_schema(
109 no_deploy=>1,
110 no_populate=>1,
111 sqlite_use_file => 1,
112 );
9f3849c3 113
912e2d5a 114my $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");
122lives_ok { $admin->install(); } 'install schema version 3.0';
123is($admin->schema->get_db_version, "3.0", 'db thinks its version 3.0');
124dies_ok { $admin->install("4.0"); } 'cannot install to allready existing version';
125sleep 1;
126$admin->force(1);
127lives_ok { $admin->install("4.0"); } 'can force install to allready existing version';
128is($admin->schema->get_db_version, "4.0", 'db thinks its version 4.0');
129#clean_dir($sql_dir);
9f3849c3 130}
131
132sub clean_dir {
133 my ($dir) =@_;
912e2d5a 134 $dir = $dir->resolve;
135 if ( ! -d $dir ) {
136 $dir->mkpath();
137 }
9f3849c3 138 foreach my $file ($dir->children) {
2ded40e7 139 # skip any hidden files
140 next if ($file =~ /^\./);
9f3849c3 141 unlink $file;
142 }
143}
144
145done_testing;