8 use aliased 'DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator';
9 use File::Spec::Functions;
10 use File::Path qw(rmtree mkpath);
12 my $db = 'dbi:SQLite:db.db';
13 my @connection = ($db, '', '', { ignore_version => 1 });
14 my $sql_dir = 't/sql';
19 use_ok 'DBICVersion_v1';
20 my $s = DBICVersion::Schema->connect(@connection);
21 my $dm = Translator->new({
23 upgrade_directory => $sql_dir,
24 databases => ['SQLite'],
25 sqltargs => { add_drop_table => 0 },
28 ok( $dm, 'DBIC::DH::DM::SQL::Translator gets instantiated correctly' );
33 local $SIG{__WARN__} = sub{$warned = 1};
35 ok( $warned, 'prepare_install warns if you run it twice' );
37 mkpath(catfile(qw( t sql _common schema 1.0 )));
39 catfile(qw( t sql _common schema 1.0 002-error.sql ));
40 print {$common} qq<syntax fail\n\n>;
44 -f catfile(qw( t sql SQLite schema 1.0 001-auto.sql )),
45 '1.0 schema gets generated properly'
49 $s->resultset('Foo')->create({
52 } 'schema not deployed';
54 mkpath catfile(qw( t sql _common schema 1.0 ));
56 catfile(qw( t sql _common schema 1.0 001-auto.sql ));
57 print {$common} qq<This will never get run>;
61 local $SIG{__WARN__} = sub{$warned = 1};
63 ok( $warned, 'deploy warns on sql errors' );
67 $s->resultset('Foo')->create({
70 } 'schema is deployed';
74 use_ok 'DBICVersion_v2';
75 my $s = DBICVersion::Schema->connect(@connection);
76 my $dm = Translator->new({
78 upgrade_directory => $sql_dir,
79 databases => ['SQLite'],
80 sqltargs => { add_drop_table => 0 },
84 ok( $dm, 'DBIC::DH::SQL::Translator w/2.0 instantiates correctly');
86 $version = $s->schema_version();
89 -f catfile(qw( t sql SQLite schema 2.0 001-auto.sql )),
90 '2.0 schema gets generated properly'
92 mkpath(catfile(qw( t sql SQLite up 1.0-2.0 )));
93 $dm->prepare_upgrade(qw(1.0 2.0), [qw(1.0 2.0)]);
97 local $SIG{__WARN__} = sub{$warned = 1};
98 $dm->prepare_upgrade(qw(0.0 1.0), [qw(0.0 1.0)]);
99 ok( $warned, 'prepare_upgrade with a bogus preversion warns' );
102 -f catfile(qw( t sql SQLite up 1.0-2.0 001-auto.sql )),
103 '1.0-2.0 diff gets generated properly and default start and end versions get set'
105 mkpath(catfile(qw( t sql SQLite down 2.0-1.0 )));
106 $dm->prepare_downgrade($version, '1.0', [$version, '1.0']);
108 -f catfile(qw( t sql SQLite down 2.0-1.0 001-auto.sql )),
109 '2.0-1.0 diff gets generated properly'
112 $s->resultset('Foo')->create({
116 } 'schema not deployed';
118 $s->resultset('Foo')->create({
122 } 'schema not uppgrayyed';
124 mkpath catfile(qw( t sql _common up 1.0-2.0 ));
125 open my $common, '>',
126 catfile(qw( t sql _common up 1.0-2.0 002-semiautomatic.sql ));
127 print {$common} qq<INSERT INTO Foo (bar, baz) VALUES ("hello", "world");\n\n>;
130 open my $common_pl, '>',
131 catfile(qw( t sql _common up 1.0-2.0 003-semiautomatic.pl ));
132 print {$common_pl} q|
135 $schema->resultset('Foo')->create({
143 $dm->upgrade_single_step([qw( 1.0 2.0 )]);
144 is( $s->resultset('Foo')->search({
147 })->count, 1, '_common migration got run');
148 is( $s->resultset('Foo')->search({
150 #baz => 'blue skies',
151 })->count, 1, '_common perl migration got run');
153 $s->resultset('Foo')->create({
157 } 'schema is deployed';
158 $dm->downgrade_single_step([qw( 2.0 1.0 )]);
160 $s->resultset('Foo')->create({
164 } 'schema is downpgrayyed';
165 $dm->upgrade_single_step([qw( 1.0 2.0 )]);
169 use_ok 'DBICVersion_v3';
170 my $s = DBICVersion::Schema->connect(@connection);
171 my $dm = Translator->new({
173 upgrade_directory => $sql_dir,
174 databases => ['SQLite'],
175 sqltargs => { add_drop_table => 0 },
179 ok( $dm, 'DBIC::DH::SQL::Translator w/3.0 instantiates correctly');
181 $version = $s->schema_version();
182 $dm->prepare_install;
184 -f catfile(qw( t sql SQLite schema 3.0 001-auto.sql )),
185 '2.0 schema gets generated properly'
187 $dm->prepare_downgrade($version, '1.0', [$version, '1.0']);
189 -f catfile(qw( t sql SQLite down 3.0-1.0 001-auto.sql )),
190 '3.0-1.0 diff gets generated properly'
192 $dm->prepare_upgrade( '1.0', $version, ['1.0', $version] );
194 -f catfile(qw( t sql SQLite up 1.0-3.0 001-auto.sql )),
195 '1.0-3.0 diff gets generated properly'
197 $dm->prepare_upgrade( '2.0', $version, ['2.0', $version]);
200 local $SIG{__WARN__} = sub{$warned = 1};
201 $dm->prepare_upgrade( '2.0', $version, ['2.0', $version] );
202 ok( $warned, 'prepare_upgrade warns if you clobber an existing upgrade file' );
205 -f catfile(qw( t sql SQLite up 1.0-2.0 001-auto.sql )),
206 '2.0-3.0 diff gets generated properly'
208 mkpath catfile(qw( t sql _generic up 2.0-3.0 ));
209 rename catfile(qw( t sql SQLite up 2.0-3.0 001-auto.sql )), catfile(qw( t sql _generic up 2.0-3.0 001-auto.sql ));
210 rmtree(catfile(qw( t sql SQLite )));
211 warn 'how can this be' if -d catfile(qw( t sql SQLite ));
213 $s->resultset('Foo')->create({
218 } 'schema not deployed';
219 $dm->upgrade_single_step([qw( 2.0 3.0 )]);
221 $s->resultset('Foo')->create({
226 } 'schema is deployed using _generic';
227 rmtree(catfile(qw( t sql SQLite )));
228 rmtree(catfile(qw( t sql _generic )));
230 $dm->upgrade_single_step([qw( 2.0 3.0 )]);
231 } 'dies when sql dir does not exist';
234 #vim: ts=2 sw=2 expandtab