change all the tests to use the new test schema namespace
[dbsrgits/DBIx-Class-DeploymentHandler.git] / t / mysql / basic.t
1 #!/usr/bin/env perl
2
3 use warnings;
4 use strict;
5 use lib 't/lib';
6
7 use Test::More;
8 use Test::Requires qw(Test::postgresql);
9 use Test::Requires qw(Test::mysqld);
10 use File::Spec::Functions 'catdir', 'catfile';
11 use File::Path 'remove_tree';
12 use DBIx::Class::DeploymentHandler;
13 use DBICDHTest;
14
15
16 VERSION1: {
17   ok my $testdb = build_test_mysql(),
18     'good test db';
19
20   my $dbh = DBI->connect("DBI:mysql:test;mysql_socket=${\$testdb->base_dir}/tmp/mysql.sock",'root','');
21
22   use_ok 'DBICVersion_v1';
23   $DBICVersion::Schema1::VERSION = 1;
24   ok my $schema = DBICVersion::Schema1->connect(sub { $dbh }),
25     'got schema';
26
27   ok my $dbic_dh = build_dh($schema),
28     'got dbicdh';
29
30   $dbic_dh->prepare_install;
31   $dbic_dh->install;
32
33   is $dbic_dh->database_version, 1, 'correct db version';
34 }
35
36 VERSION2: {
37   ok my $testdb = build_test_mysql(),
38     'good test db';
39
40   my $dbh = DBI->connect("DBI:mysql:test;mysql_socket=${\$testdb->base_dir}/tmp/mysql.sock",'root','');
41
42   use_ok 'DBICVersion_v2';
43   $DBICVersion::Schema2::VERSION = 2;
44   ok my $schema = DBICVersion::Schema2->connect(sub { $dbh }),
45     'got schema';
46
47   ok my $dbic_dh = build_dh($schema,1),
48     'got dbicdh';
49
50   $dbic_dh->prepare_install();
51   $dbic_dh->prepare_upgrade();
52   $dbic_dh->prepare_downgrade();
53   $dbic_dh->upgrade;
54
55   is $dbic_dh->database_version, 2, 'correct db version';
56
57 }
58
59 ok -d catdir('t','share','var','mysql-deploy','MySQL','downgrade','2-1'),
60   'reasonable defaults properly creates a downgrade';
61
62 VERSION1FORCE: {
63
64   remove_tree catdir('t','share','var','mysql');
65
66   ok my $testdb = build_test_mysql(),
67     'good test db';
68
69   my $dbh = DBI->connect("DBI:mysql:test;mysql_socket=${\$testdb->base_dir}/tmp/mysql.sock",'root','');
70
71   use_ok 'DBICVersion_v2';
72   $DBICVersion::Schema2::VERSION = 2;
73   ok my $schema = DBICVersion::Schema2->connect(sub { $dbh }),
74     'got schema';
75
76   my $dbic_dh = DBIx::Class::DeploymentHandler->new({
77     script_directory => catdir('t','share','var','mysql-deploy'),
78     to_version => 1,
79     schema => $schema,
80     databases => ['MySQL']});
81
82   $dbic_dh->install;
83
84   is $dbic_dh->database_version, 1, 'correct db version';
85 }
86
87 done_testing();
88
89 sub build_dh {
90   DBIx::Class::DeploymentHandler->new({
91     script_directory => catdir('t','share','var','mysql-deploy'),
92     schema => shift,
93     databases => ['MySQL']});
94 }
95
96 sub build_test_mysql {
97   my $base_dir = catdir('t','share','var','mysql');
98   my $auto_start = -d $base_dir ? 1:2;
99   my %config = (
100     base_dir => $base_dir,
101     auto_start => $auto_start);
102
103   return Test::mysqld->new(
104     auto_start => $auto_start,
105     base_dir => $base_dir);
106
107
108   if(my $testdb = Test::mysqld->new(%config)) {
109     return $testdb;
110   } else {
111     die $Test::mysqld::errstr;
112   }
113 }
114
115 END {
116   remove_tree catdir('t','share','var','mysql');
117   remove_tree catdir('t','share','var','mysql-deploy');
118 }
119