8b1c57fc2d2ca28ae56eda711c8181d333ba3cba
[dbsrgits/DBIx-Class.git] / t / admin / 02ddl.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use Test::Warn;
7
8 use Path::Class;
9
10 use lib qw(t/lib);
11 use DBICTest;
12
13 BEGIN {
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');
17
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 }
21
22 use_ok 'DBIx::Class::Admin';
23
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' },
33 );
34 my $ddl_dir = dir(qw/t var/, "admin_ddl-$$");
35
36 { # create the schema
37
38 #  make sure we are  clean
39 clean_dir($ddl_dir);
40
41
42 my $admin = DBIx::Class::Admin->new(
43   schema_class=> "DBICTest::Schema",
44   sql_dir=> $ddl_dir,
45   connect_info => \@connect_info,
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';
50 lives_ok {
51   $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ /no such table.+DROP TABLE/s };
52   $admin->deploy()
53 } 'Can Deploy schema';
54 }
55
56 { # upgrade schema
57
58 clean_dir($ddl_dir);
59 require DBICVersion_v1;
60
61 my $admin = DBIx::Class::Admin->new(
62   schema_class => 'DBICVersion::Schema',
63   sql_dir =>  $ddl_dir,
64   connect_info => \@connect_info,
65 );
66
67 my $schema = $admin->schema();
68
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';
71
72 # connect to now deployed schema
73 lives_ok { $schema = DBICVersion::Schema->connect(@{$schema->storage->connect_info()}); } 'Connect to deployed Database';
74
75 is($schema->get_db_version, $DBICVersion::Schema::VERSION, 'Schema deployed and versions match');
76
77
78 require DBICVersion_v2;
79 DBICVersion::Schema->upgrade_directory (undef);  # so that we can test use of $ddl_dir
80
81 $admin = DBIx::Class::Admin->new(
82   schema_class => 'DBICVersion::Schema',
83   sql_dir =>  $ddl_dir,
84   connect_info => \@connect_info
85 );
86
87 lives_ok { $admin->create($schema->storage->sqlt_type(), {}, "1.0" ); } 'Can create diff for ' . $schema->storage->sqlt_type;
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';
91   dies_ok {$admin->deploy} 'cannot deploy installed schema, should upgrade instead';
92 }
93
94 is($schema->get_db_version, $DBICVersion::Schema::VERSION, 'Schema and db versions match');
95
96 }
97
98 { # install
99
100 clean_dir($ddl_dir);
101
102 my $admin = DBIx::Class::Admin->new(
103   schema_class  => 'DBICVersion::Schema',
104   sql_dir      => $ddl_dir,
105   _confirm    => 1,
106   connect_info  => \@connect_info,
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';
113
114 $admin->force(1);
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' );
118 is($admin->schema->get_db_version, "4.0", 'db thinks its version 4.0');
119 }
120
121 sub clean_dir {
122   my ($dir) = @_;
123   $dir->rmtree if -d $dir;
124   unlink $db_fn;
125 }
126
127 END {
128   clean_dir($ddl_dir);
129 }
130
131 done_testing;