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; |
12 | |
6bceaca3 |
13 | BEGIN { |
ebcd0e4f |
14 | require DBIx::Class; |
15 | plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for('admin') |
16 | unless DBIx::Class::Optional::Dependencies->req_ok_for('admin'); |
6bceaca3 |
17 | |
ebcd0e4f |
18 | plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for('deploy') |
19 | unless DBIx::Class::Optional::Dependencies->req_ok_for('deploy'); |
20 | } |
34963a09 |
21 | |
c71d3367 |
22 | use_ok 'DBIx::Class::Admin'; |
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 { |
51 | $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ /no such table.+DROP TABLE/s }; |
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 | { |
89 | local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DB version .+? is lower than the schema version/ }; |
90 | lives_ok {$admin->upgrade();} 'upgrade the schema'; |
99ab62de |
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 | _confirm => 1, |
106 | connect_info => \@connect_info, |
a705b175 |
107 | ); |
108 | |
109 | $admin->version("3.0"); |
110 | lives_ok { $admin->install(); } 'install schema version 3.0'; |
111 | is($admin->schema->get_db_version, "3.0", 'db thinks its version 3.0'); |
112 | dies_ok { $admin->install("4.0"); } 'cannot install to allready existing version'; |
ebcd0e4f |
113 | |
a705b175 |
114 | $admin->force(1); |
34963a09 |
115 | warnings_exist ( sub { |
116 | lives_ok { $admin->install("4.0") } 'can force install to allready existing version' |
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; |