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