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