add force_overwrite attribute to SQLTDM
[dbsrgits/DBIx-Class-DeploymentHandler.git] / t / deploy_methods / sql_translator.t
CommitLineData
02d58ac0 1#!perl
2
3use Test::More;
4use Test::Exception;
5
6use lib 't/lib';
7use DBICDHTest;
459a67e3 8use aliased 'DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator';
3b98a3a1 9use File::Spec::Functions;
d50f2521 10use File::Path qw(rmtree mkpath);
459a67e3 11
624e3018 12my $dbh = DBI->connect('dbi:SQLite::memory:');
13my @connection = (sub { $dbh }, { ignore_version => 1 });
459a67e3 14my $sql_dir = 't/sql';
15
16DBICDHTest::ready;
17
18VERSION1: {
19 use_ok 'DBICVersion_v1';
20 my $s = DBICVersion::Schema->connect(@connection);
21 my $dm = Translator->new({
3b98a3a1 22 schema => $s,
91adde75 23 script_directory => $sql_dir,
3b98a3a1 24 databases => ['SQLite'],
02a7b8ac 25 sql_translator_args => { add_drop_table => 0 },
459a67e3 26 });
27
28 ok( $dm, 'DBIC::DH::DM::SQL::Translator gets instantiated correctly' );
3b98a3a1 29
91557c90 30 $dm->prepare_deploy;
fc4b7602 31
ff40cb1f 32 mkpath(catfile(qw( t sql SQLite initialize 1.0 )));
fc4b7602 33 open my $prerun, '>',
ff40cb1f 34 catfile(qw( t sql SQLite initialize 1.0 003-semiautomatic.pl ));
5b5defbc 35 print {$prerun} "sub {use File::Touch; touch(q(foobar));}";
fc4b7602 36 close $prerun;
ff40cb1f 37 $dm->initialize({ version => '1.0' });
fc4b7602 38
09bc35e3 39 ok -e 'foobar';
fc4b7602 40
92624ee5 41 dies_ok {$dm->prepare_deploy} 'prepare_deploy dies if you run it twice' ;
3b98a3a1 42
43 ok(
58eb99c3 44 -f catfile(qw( t sql SQLite deploy 1.0 001-auto.sql )),
3b98a3a1 45 '1.0 schema gets generated properly'
46 );
47
48 dies_ok {
49 $s->resultset('Foo')->create({
50 bar => 'frew',
51 })
52 } 'schema not deployed';
53
026eaf0c 54 $dm->deploy;
3b98a3a1 55
56 lives_ok {
57 $s->resultset('Foo')->create({
58 bar => 'frew',
59 })
60 } 'schema is deployed';
61}
62
63VERSION2: {
64 use_ok 'DBICVersion_v2';
65 my $s = DBICVersion::Schema->connect(@connection);
66 my $dm = Translator->new({
67 schema => $s,
91adde75 68 script_directory => $sql_dir,
3b98a3a1 69 databases => ['SQLite'],
02a7b8ac 70 sql_translator_args => { add_drop_table => 0 },
91557c90 71 txn_wrap => 1,
3b98a3a1 72 });
73
74 ok( $dm, 'DBIC::DH::SQL::Translator w/2.0 instantiates correctly');
75
76 $version = $s->schema_version();
91557c90 77 $dm->prepare_deploy;
3b98a3a1 78 ok(
58eb99c3 79 -f catfile(qw( t sql SQLite deploy 2.0 001-auto.sql )),
3b98a3a1 80 '2.0 schema gets generated properly'
81 );
58eb99c3 82 mkpath(catfile(qw( t sql SQLite upgrade 1.0-2.0 )));
be140a5f 83 $dm->prepare_upgrade({
84 from_version => '1.0',
85 to_version => '2.0',
86 version_set => [qw(1.0 2.0)]
87 });
d50f2521 88
89 {
90 my $warned = 0;
91 local $SIG{__WARN__} = sub{$warned = 1};
be140a5f 92 $dm->prepare_upgrade({
93 from_version => '0.0',
94 to_version => '1.0',
95 version_set => [qw(0.0 1.0)]
96 });
d50f2521 97 ok( $warned, 'prepare_upgrade with a bogus preversion warns' );
98 }
3b98a3a1 99 ok(
58eb99c3 100 -f catfile(qw( t sql SQLite upgrade 1.0-2.0 001-auto.sql )),
d50f2521 101 '1.0-2.0 diff gets generated properly and default start and end versions get set'
3b98a3a1 102 );
58eb99c3 103 mkpath(catfile(qw( t sql SQLite downgrade 2.0-1.0 )));
be140a5f 104 $dm->prepare_downgrade({
105 from_version => $version,
106 to_version => '1.0',
107 version_set => [$version, '1.0']
108 });
3b98a3a1 109 ok(
58eb99c3 110 -f catfile(qw( t sql SQLite downgrade 2.0-1.0 001-auto.sql )),
d50f2521 111 '2.0-1.0 diff gets generated properly'
3b98a3a1 112 );
113 dies_ok {
114 $s->resultset('Foo')->create({
115 bar => 'frew',
116 baz => 'frew',
117 })
118 } 'schema not deployed';
119 dies_ok {
120 $s->resultset('Foo')->create({
121 bar => 'frew',
122 baz => 'frew',
123 })
124 } 'schema not uppgrayyed';
d50f2521 125
58eb99c3 126 mkpath catfile(qw( t sql _common upgrade 1.0-2.0 ));
d50f2521 127 open my $common, '>',
58eb99c3 128 catfile(qw( t sql _common upgrade 1.0-2.0 002-semiautomatic.sql ));
d50f2521 129 print {$common} qq<INSERT INTO Foo (bar, baz) VALUES ("hello", "world");\n\n>;
130 close $common;
131
0841a743 132 open my $common_pl, '>',
58eb99c3 133 catfile(qw( t sql _common upgrade 1.0-2.0 003-semiautomatic.pl ));
0841a743 134 print {$common_pl} q|
5b5defbc 135 sub {
91557c90 136 my $schema = shift;
137 $schema->resultset('Foo')->create({
138 bar => 'goodbye',
139 baz => 'blue skies',
140 })
141 }
142 |;
0841a743 143 close $common_pl;
144
be140a5f 145 $dm->upgrade_single_step({ version_set => [qw( 1.0 2.0 )] });
d50f2521 146 is( $s->resultset('Foo')->search({
147 bar => 'hello',
148 baz => 'world',
149 })->count, 1, '_common migration got run');
0841a743 150 is( $s->resultset('Foo')->search({
151 bar => 'goodbye',
152 #baz => 'blue skies',
153 })->count, 1, '_common perl migration got run');
3b98a3a1 154 lives_ok {
155 $s->resultset('Foo')->create({
156 bar => 'frew',
157 baz => 'frew',
158 })
159 } 'schema is deployed';
be140a5f 160 $dm->downgrade_single_step({ version_set => [qw( 2.0 1.0 )] });
d50f2521 161 dies_ok {
162 $s->resultset('Foo')->create({
163 bar => 'frew',
164 baz => 'frew',
165 })
fc4b7602 166 } 'schema is downgrayyed';
be140a5f 167 $dm->upgrade_single_step({ version_set => [qw( 1.0 2.0 )] });
459a67e3 168}
02d58ac0 169
3b98a3a1 170VERSION3: {
171 use_ok 'DBICVersion_v3';
172 my $s = DBICVersion::Schema->connect(@connection);
173 my $dm = Translator->new({
174 schema => $s,
91adde75 175 script_directory => $sql_dir,
3b98a3a1 176 databases => ['SQLite'],
02a7b8ac 177 sql_translator_args => { add_drop_table => 0 },
d50f2521 178 txn_wrap => 0,
3b98a3a1 179 });
180
181 ok( $dm, 'DBIC::DH::SQL::Translator w/3.0 instantiates correctly');
182
183 $version = $s->schema_version();
91557c90 184 $dm->prepare_deploy;
3b98a3a1 185 ok(
58eb99c3 186 -f catfile(qw( t sql SQLite deploy 3.0 001-auto.sql )),
3b98a3a1 187 '2.0 schema gets generated properly'
188 );
be140a5f 189 $dm->prepare_downgrade({
190 from_version => $version,
191 to_version => '1.0',
192 version_set => [$version, '1.0']
193 });
3b98a3a1 194 ok(
58eb99c3 195 -f catfile(qw( t sql SQLite downgrade 3.0-1.0 001-auto.sql )),
d50f2521 196 '3.0-1.0 diff gets generated properly'
197 );
be140a5f 198 $dm->prepare_upgrade({
199 from_version => '1.0',
200 to_version => $version,
201 version_set => ['1.0', $version]
202 });
d50f2521 203 ok(
58eb99c3 204 -f catfile(qw( t sql SQLite upgrade 1.0-3.0 001-auto.sql )),
3b98a3a1 205 '1.0-3.0 diff gets generated properly'
206 );
be140a5f 207 $dm->prepare_upgrade({
208 from_version => '2.0',
209 to_version => $version,
210 version_set => ['2.0', $version]
211 });
92624ee5 212 dies_ok {
be140a5f 213 $dm->prepare_upgrade({
214 from_version => '2.0',
215 to_version => $version,
216 version_set => ['2.0', $version]
217 });
92624ee5 218 }
219 'prepare_upgrade dies if you clobber an existing upgrade file' ;
3b98a3a1 220 ok(
58eb99c3 221 -f catfile(qw( t sql SQLite upgrade 1.0-2.0 001-auto.sql )),
3b98a3a1 222 '2.0-3.0 diff gets generated properly'
223 );
224 dies_ok {
225 $s->resultset('Foo')->create({
226 bar => 'frew',
227 baz => 'frew',
228 biff => 'frew',
229 })
230 } 'schema not deployed';
be140a5f 231 $dm->upgrade_single_step({ version_set => [qw( 2.0 3.0 )] });
3b98a3a1 232 lives_ok {
233 $s->resultset('Foo')->create({
234 bar => 'frew',
235 baz => 'frew',
236 biff => 'frew',
237 })
5b766a24 238 } 'schema is deployed';
d50f2521 239 dies_ok {
be140a5f 240 $dm->upgrade_single_step({ version_set => [qw( 2.0 3.0 )] });
d50f2521 241 } 'dies when sql dir does not exist';
3b98a3a1 242}
02d58ac0 243done_testing;
d50f2521 244#vim: ts=2 sw=2 expandtab