Commit | Line | Data |
9f3849c3 |
1 | use strict; |
2 | use warnings; |
3 | |
34963a09 |
4 | use Test::More; |
9f3849c3 |
5 | use Test::Exception; |
34963a09 |
6 | use Test::Warn; |
9f3849c3 |
7 | |
6bceaca3 |
8 | |
9 | BEGIN { |
10 | eval "use DBIx::Class::Admin"; |
11 | plan skip_all => "Deps not installed: $@" if $@; |
12 | } |
13 | |
34963a09 |
14 | |
15 | use lib qw(t/lib); |
16 | use DBICTest; |
17 | |
9f3849c3 |
18 | use Path::Class; |
9f3849c3 |
19 | |
20 | use ok 'DBIx::Class::Admin'; |
21 | |
9f3849c3 |
22 | |
34963a09 |
23 | my $sql_dir = dir(qw/t var/); |
038fd460 |
24 | my @connect_info = DBICTest->_database( |
1edd4ca6 |
25 | no_deploy=>1, |
26 | no_populate=>1, |
27 | sqlite_use_file => 1, |
038fd460 |
28 | ); |
9f3849c3 |
29 | { # create the schema |
30 | |
2ded40e7 |
31 | # make sure we are clean |
a705b175 |
32 | clean_dir($sql_dir); |
2ded40e7 |
33 | |
2ded40e7 |
34 | |
a705b175 |
35 | my $admin = DBIx::Class::Admin->new( |
1edd4ca6 |
36 | schema_class=> "DBICTest::Schema", |
37 | sql_dir=> $sql_dir, |
38 | connect_info => \@connect_info, |
a705b175 |
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'; |
9f3849c3 |
43 | } |
44 | |
9f3849c3 |
45 | { # upgrade schema |
46 | |
038fd460 |
47 | #my $schema = DBICTest->init_schema( |
1edd4ca6 |
48 | # no_deploy => 1, |
49 | # no_populat => 1, |
50 | # sqlite_use_file => 1, |
038fd460 |
51 | #); |
9f3849c3 |
52 | |
a705b175 |
53 | clean_dir($sql_dir); |
54 | require DBICVersionOrig; |
9f3849c3 |
55 | |
a705b175 |
56 | my $admin = DBIx::Class::Admin->new( |
1edd4ca6 |
57 | schema_class => 'DBICVersion::Schema', |
58 | sql_dir => $sql_dir, |
59 | connect_info => \@connect_info, |
a705b175 |
60 | ); |
038fd460 |
61 | |
a705b175 |
62 | my $schema = $admin->schema(); |
038fd460 |
63 | |
a705b175 |
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'; |
2ded40e7 |
66 | |
67 | # connect to now deployed schema |
a705b175 |
68 | lives_ok { $schema = DBICVersion::Schema->connect(@{$schema->storage->connect_info()}); } 'Connect to deployed Database'; |
2ded40e7 |
69 | |
a705b175 |
70 | is($schema->get_db_version, $DBICVersion::Schema::VERSION, 'Schema deployed and versions match'); |
2ded40e7 |
71 | |
72 | |
a705b175 |
73 | require DBICVersionNew; |
2ded40e7 |
74 | |
a705b175 |
75 | $admin = DBIx::Class::Admin->new( |
1edd4ca6 |
76 | schema_class => 'DBICVersion::Schema', |
34963a09 |
77 | sql_dir => $sql_dir, |
1edd4ca6 |
78 | connect_info => \@connect_info |
a705b175 |
79 | ); |
2ded40e7 |
80 | |
a705b175 |
81 | lives_ok { $admin->create($schema->storage->sqlt_type(), {}, "1.0" ); } 'Can create diff for ' . $schema->storage->sqlt_type; |
6daebebc |
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 |
a705b175 |
84 | sleep 1; |
34963a09 |
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 | } |
2ded40e7 |
89 | |
a705b175 |
90 | is($schema->get_db_version, $DBICVersion::Schema::VERSION, 'Schema and db versions match'); |
912e2d5a |
91 | |
92 | } |
93 | |
94 | { # install |
95 | |
a705b175 |
96 | clean_dir($sql_dir); |
97 | |
98 | my $admin = DBIx::Class::Admin->new( |
1edd4ca6 |
99 | schema_class => 'DBICVersion::Schema', |
100 | sql_dir => $sql_dir, |
101 | _confirm => 1, |
102 | connect_info => \@connect_info, |
a705b175 |
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); |
34963a09 |
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' ); |
a705b175 |
114 | is($admin->schema->get_db_version, "4.0", 'db thinks its version 4.0'); |
912e2d5a |
115 | #clean_dir($sql_dir); |
9f3849c3 |
116 | } |
117 | |
118 | sub clean_dir { |
34963a09 |
119 | my ($dir) = @_; |
1edd4ca6 |
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 | } |
9f3849c3 |
129 | } |
130 | |
131 | done_testing; |