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