Commit | Line | Data |
cb551b07 |
1 | use DBIx::Class::Optional::Dependencies -skip_all_without => qw( admin deploy ); |
2 | |
9f3849c3 |
3 | use strict; |
4 | use warnings; |
5 | |
34963a09 |
6 | use Test::More; |
9f3849c3 |
7 | use Test::Exception; |
34963a09 |
8 | use Test::Warn; |
9f3849c3 |
9 | |
4bea1fe7 |
10 | use Path::Class; |
11 | |
12 | use lib qw(t/lib); |
13 | use DBICTest; |
052a832c |
14 | use DBIx::Class::_Util 'sigwarn_silencer'; |
4bea1fe7 |
15 | |
cb551b07 |
16 | use DBIx::Class::Admin; |
a21416fa |
17 | BEGIN { |
18 | no warnings 'redefine'; |
19 | # no questions asked |
20 | sub DBIx::Class::Admin::_confirm { 1 }; |
21 | } |
9f3849c3 |
22 | |
8d6b1478 |
23 | # lock early |
24 | DBICTest->init_schema(no_deploy => 1, no_populate => 1); |
25 | |
26 | my $db_fn = DBICTest->_sqlite_dbfilename; |
27 | my @connect_info = ( |
28 | "dbi:SQLite:$db_fn", |
29 | undef, |
30 | undef, |
31 | { on_connect_do => 'PRAGMA synchronous = OFF' }, |
038fd460 |
32 | ); |
8d6b1478 |
33 | my $ddl_dir = dir(qw/t var/, "admin_ddl-$$"); |
f267b29d |
34 | |
9f3849c3 |
35 | { # create the schema |
36 | |
2ded40e7 |
37 | # make sure we are clean |
8d6b1478 |
38 | clean_dir($ddl_dir); |
2ded40e7 |
39 | |
2ded40e7 |
40 | |
a705b175 |
41 | my $admin = DBIx::Class::Admin->new( |
1edd4ca6 |
42 | schema_class=> "DBICTest::Schema", |
8d6b1478 |
43 | sql_dir=> $ddl_dir, |
99ab62de |
44 | connect_info => \@connect_info, |
a705b175 |
45 | ); |
46 | isa_ok ($admin, 'DBIx::Class::Admin', 'create the admin object'); |
47 | lives_ok { $admin->create('MySQL'); } 'Can create MySQL sql'; |
48 | lives_ok { $admin->create('SQLite'); } 'Can Create SQLite sql'; |
f267b29d |
49 | lives_ok { |
052a832c |
50 | local $SIG{__WARN__} = sigwarn_silencer( qr/no such table.+DROP TABLE/s ); |
f267b29d |
51 | $admin->deploy() |
52 | } 'Can Deploy schema'; |
9f3849c3 |
53 | } |
54 | |
9f3849c3 |
55 | { # upgrade schema |
56 | |
8d6b1478 |
57 | clean_dir($ddl_dir); |
ebcd0e4f |
58 | require DBICVersion_v1; |
9f3849c3 |
59 | |
a705b175 |
60 | my $admin = DBIx::Class::Admin->new( |
99ab62de |
61 | schema_class => 'DBICVersion::Schema', |
8d6b1478 |
62 | sql_dir => $ddl_dir, |
1edd4ca6 |
63 | connect_info => \@connect_info, |
a705b175 |
64 | ); |
038fd460 |
65 | |
a705b175 |
66 | my $schema = $admin->schema(); |
038fd460 |
67 | |
a705b175 |
68 | lives_ok { $admin->create($schema->storage->sqlt_type(), {add_drop_table=>0}); } 'Can create DBICVersionOrig sql in ' . $schema->storage->sqlt_type; |
69 | lives_ok { $admin->deploy( ) } 'Can Deploy schema'; |
2ded40e7 |
70 | |
71 | # connect to now deployed schema |
a705b175 |
72 | lives_ok { $schema = DBICVersion::Schema->connect(@{$schema->storage->connect_info()}); } 'Connect to deployed Database'; |
2ded40e7 |
73 | |
a705b175 |
74 | is($schema->get_db_version, $DBICVersion::Schema::VERSION, 'Schema deployed and versions match'); |
2ded40e7 |
75 | |
76 | |
ebcd0e4f |
77 | require DBICVersion_v2; |
8d6b1478 |
78 | DBICVersion::Schema->upgrade_directory (undef); # so that we can test use of $ddl_dir |
2ded40e7 |
79 | |
a705b175 |
80 | $admin = DBIx::Class::Admin->new( |
99ab62de |
81 | schema_class => 'DBICVersion::Schema', |
8d6b1478 |
82 | sql_dir => $ddl_dir, |
1edd4ca6 |
83 | connect_info => \@connect_info |
a705b175 |
84 | ); |
2ded40e7 |
85 | |
a705b175 |
86 | lives_ok { $admin->create($schema->storage->sqlt_type(), {}, "1.0" ); } 'Can create diff for ' . $schema->storage->sqlt_type; |
34963a09 |
87 | { |
052a832c |
88 | local $SIG{__WARN__} = sigwarn_silencer( qr/DB version .+? is lower than the schema version/ ); |
89 | lives_ok { $admin->upgrade() } 'upgrade the schema'; |
90 | dies_ok { $admin->deploy } 'cannot deploy installed schema, should upgrade instead'; |
34963a09 |
91 | } |
2ded40e7 |
92 | |
a705b175 |
93 | is($schema->get_db_version, $DBICVersion::Schema::VERSION, 'Schema and db versions match'); |
912e2d5a |
94 | |
95 | } |
96 | |
97 | { # install |
98 | |
8d6b1478 |
99 | clean_dir($ddl_dir); |
a705b175 |
100 | |
101 | my $admin = DBIx::Class::Admin->new( |
99ab62de |
102 | schema_class => 'DBICVersion::Schema', |
8d6b1478 |
103 | sql_dir => $ddl_dir, |
1edd4ca6 |
104 | connect_info => \@connect_info, |
a705b175 |
105 | ); |
106 | |
107 | $admin->version("3.0"); |
e952df76 |
108 | $admin->install; |
a705b175 |
109 | is($admin->schema->get_db_version, "3.0", 'db thinks its version 3.0'); |
e952df76 |
110 | throws_ok { |
111 | $admin->install("4.0") |
112 | } qr/Schema already has a version. Try upgrade instead/, 'cannot install to allready existing version'; |
ebcd0e4f |
113 | |
a705b175 |
114 | $admin->force(1); |
34963a09 |
115 | warnings_exist ( sub { |
e952df76 |
116 | $admin->install("4.0") |
34963a09 |
117 | }, qr/Forcing install may not be a good idea/, 'Force warning emitted' ); |
a705b175 |
118 | is($admin->schema->get_db_version, "4.0", 'db thinks its version 4.0'); |
9f3849c3 |
119 | } |
120 | |
121 | sub clean_dir { |
34963a09 |
122 | my ($dir) = @_; |
8d6b1478 |
123 | $dir->rmtree if -d $dir; |
124 | unlink $db_fn; |
125 | } |
126 | |
127 | END { |
128 | clean_dir($ddl_dir); |
9f3849c3 |
129 | } |
130 | |
131 | done_testing; |