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