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