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