Fix complete breakage of ddl_filename on certain paths
[dbsrgits/DBIx-Class.git] / t / 74mssql.t
CommitLineData
70350518 1use strict;
bbdda281 2use warnings;
70350518 3
46e3af47 4# use this if you keep a copy of DBD::Sybase linked to FreeTDS somewhere else
5BEGIN {
6 if (my $lib_dirs = $ENV{DBICTEST_MSSQL_PERL5LIB}) {
7 unshift @INC, $_ for split /:/, $lib_dirs;
8 }
9}
10
70350518 11use Test::More;
8c52ffd3 12use Test::Exception;
65d35121 13use Scalar::Util 'weaken';
199fbc45 14use DBIx::Class::Optional::Dependencies ();
70350518 15use lib qw(t/lib);
16use DBICTest;
eef2ff6c 17
18my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_${_}" } qw/DSN USER PASS/};
19
70350518 20plan skip_all => 'Set $ENV{DBICTEST_MSSQL_DSN}, _USER and _PASS to run this test'
eef2ff6c 21 unless ($dsn);
22
49b3a264 23
24plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_mssql_sybase')
25 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_mssql_sybase');
26
77c7628c 27{
28 my $srv_ver = DBICTest::Schema->connect($dsn, $user, $pass)->storage->_server_info->{dbms_version};
29 ok ($srv_ver, 'Got a test server version on fresh schema: ' . ($srv_ver||'???') );
30}
31
b30f1a32 32my $schema;
33
bbdda281 34my $testdb_supports_placeholders = DBICTest::Schema->connect($dsn, $user, $pass)
35 ->storage
36 ->_supports_typeless_placeholders;
37my @test_storages = (
38 $testdb_supports_placeholders ? 'DBI::Sybase::Microsoft_SQL_Server' : (),
c29ce317 39 'DBI::Sybase::Microsoft_SQL_Server::NoBindVars',
7379eb67 40);
8c52ffd3 41
bbdda281 42for my $storage_type (@test_storages) {
43 $schema = DBICTest::Schema->connect($dsn, $user, $pass);
eef2ff6c 44
bbdda281 45 if ($storage_type =~ /NoBindVars\z/) {
46 # since we want to use the nobindvar - disable the capability so the
47 # rebless happens to the correct class
48 $schema->storage->_use_typeless_placeholders (0);
7379eb67 49 }
50
c1e5a9ac 51 local $ENV{DBIC_MSSQL_FREETDS_LOWVER_NOWARN} = 1; # disable nobindvars warning
52
bbdda281 53 $schema->storage->ensure_connected;
b30f1a32 54
55 if ($storage_type =~ /NoBindVars\z/) {
56 is $schema->storage->disable_sth_caching, 1,
57 'prepare_cached disabled for NoBindVars';
58 }
59
c29ce317 60 isa_ok($schema->storage, "DBIx::Class::Storage::$storage_type");
7379eb67 61
99083752 62 SKIP: {
63 skip 'This version of DBD::Sybase segfaults on disconnect', 1 if DBD::Sybase->VERSION < 1.08;
7379eb67 64
99083752 65 # start disconnected to test _ping
66 $schema->storage->_dbh->disconnect;
67
68 lives_ok {
69 $schema->storage->dbh_do(sub { $_[1]->do('select 1') })
70 } '_ping works';
71 }
ecdf1ac8 72
73 my $dbh = $schema->storage->dbh;
7379eb67 74
75 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL
76 DROP TABLE artist");
77 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL
78 DROP TABLE cd");
79
80 $dbh->do("CREATE TABLE artist (artistid INT IDENTITY PRIMARY KEY, name VARCHAR(100), rank INT DEFAULT '13', charfield CHAR(10) NULL);");
81 $dbh->do("CREATE TABLE cd (cdid INT IDENTITY PRIMARY KEY, artist INT, title VARCHAR(100), year VARCHAR(100), genreid INT NULL, single_track INT NULL);");
3ff5b740 82# Just to test compat shim, Auto is in Core
7379eb67 83 $schema->class('Artist')->load_components('PK::Auto::MSSQL');
eef2ff6c 84
85# Test PK
7379eb67 86 my $new = $schema->resultset('Artist')->create( { name => 'foo' } );
87 ok($new->artistid, "Auto-PK worked");
eef2ff6c 88
89# Test LIMIT
7379eb67 90 for (1..6) {
91 $schema->resultset('Artist')->create( { name => 'Artist ' . $_, rank => $_ } );
92 }
eef2ff6c 93
7379eb67 94 my $it = $schema->resultset('Artist')->search( { },
95 { rows => 3,
96 offset => 2,
97 order_by => 'artistid'
98 }
99 );
eef2ff6c 100
b4474f31 101# Test ? in data don't get treated as placeholders
7379eb67 102 my $cd = $schema->resultset('CD')->create( {
103 artist => 1,
104 title => 'Does this break things?',
105 year => 2007,
106 } );
107 ok($cd->id, 'Not treating ? in data as placeholders');
108
109 is( $it->count, 3, "LIMIT count ok" );
110 ok( $it->next->name, "iterator->next ok" );
111 $it->next;
112 $it->next;
113 is( $it->next, undef, "next past end of resultset ok" );
eef2ff6c 114
5064f5c3 115# test MONEY column support
7379eb67 116 $schema->storage->dbh_do (sub {
117 my ($storage, $dbh) = @_;
118 eval { $dbh->do("DROP TABLE money_test") };
119 $dbh->do(<<'SQL');
7379eb67 120 CREATE TABLE money_test (
121 id INT IDENTITY PRIMARY KEY,
122 amount MONEY NULL
123 )
5064f5c3 124SQL
b30f1a32 125 });
5064f5c3 126
65d35121 127 my $rs = $schema->resultset('Money');
128 weaken(my $rs_cp = $rs); # nested closure refcounting is an utter mess in perl
5064f5c3 129
7379eb67 130 my $row;
131 lives_ok {
132 $row = $rs->create({ amount => 100 });
133 } 'inserted a money value';
5064f5c3 134
a33d2444 135 cmp_ok $rs->find($row->id)->amount, '==', 100, 'money value round-trip';
5064f5c3 136
7379eb67 137 lives_ok {
138 $row->update({ amount => 200 });
139 } 'updated a money value';
5064f5c3 140
a33d2444 141 cmp_ok $rs->find($row->id)->amount, '==', 200,
142 'updated money value round-trip';
5064f5c3 143
7379eb67 144 lives_ok {
145 $row->update({ amount => undef });
146 } 'updated a money value to NULL';
5064f5c3 147
c8365716 148 is $rs->find($row->id)->amount,
149 undef, 'updated money value to NULL round-trip';
a467a0c9 150
b90d7eba 151 $rs->delete;
152
153 # test simple transaction with commit
154 lives_ok {
155 $schema->txn_do(sub {
65d35121 156 $rs_cp->create({ amount => 300 });
b90d7eba 157 });
158 } 'simple transaction';
159
b30f1a32 160 cmp_ok $rs->first->amount, '==', 300, 'committed';
b90d7eba 161
b30f1a32 162 $rs->reset;
b90d7eba 163 $rs->delete;
164
165 # test rollback
166 throws_ok {
167 $schema->txn_do(sub {
65d35121 168 $rs_cp->create({ amount => 700 });
b90d7eba 169 die 'mtfnpy';
170 });
171 } qr/mtfnpy/, 'simple failed txn';
172
173 is $rs->first, undef, 'rolled back';
b30f1a32 174
b90d7eba 175 $rs->reset;
b30f1a32 176 $rs->delete;
177
178 # test multiple active statements
179 {
180 $rs->create({ amount => 800 + $_ }) for 1..3;
181
182 my @map = (
183 [ 'Artist 1', '801.00' ],
184 [ 'Artist 2', '802.00' ],
185 [ 'Artist 3', '803.00' ]
186 );
187
188 my $artist_rs = $schema->resultset('Artist')->search({
189 name => { -like => 'Artist %' }
190 });;
191
192 my $i = 0;
193
194 while (my $money_row = $rs->next) {
195 my $artist_row = $artist_rs->next;
196
197 is_deeply [ $artist_row->name, $money_row->amount ], $map[$i++],
198 'multiple active statements';
199 }
200 $rs->reset;
201 $rs->delete;
202 }
203
c1e5a9ac 204 # test transaction handling on a disconnected handle
205 my $wrappers = {
206 no_transaction => sub { shift->() },
207 txn_do => sub { my $code = shift; $schema->txn_do(sub { $code->() } ) },
208 txn_begin => sub { $schema->txn_begin; shift->(); $schema->txn_commit },
209 txn_guard => sub { my $g = $schema->txn_scope_guard; shift->(); $g->commit },
210 };
211 for my $wrapper (keys %$wrappers) {
212 $rs->delete;
213
214 # a reconnect should trigger on next action
215 $schema->storage->_get_dbh->disconnect;
216
65d35121 217
c1e5a9ac 218 lives_and {
219 $wrappers->{$wrapper}->( sub {
65d35121 220 $rs_cp->create({ amount => 900 + $_ }) for 1..3;
c1e5a9ac 221 });
222 is $rs->count, 3;
223 } "transaction on disconnected handle with $wrapper wrapper";
224 }
225
226 TODO: {
227 local $TODO = 'Transaction handling with multiple active statements will '
228 .'need eager cursor support.';
229
230 # test transaction handling on a disconnected handle with multiple active
231 # statements
232 my $wrappers = {
233 no_transaction => sub { shift->() },
234 txn_do => sub { my $code = shift; $schema->txn_do(sub { $code->() } ) },
235 txn_begin => sub { $schema->txn_begin; shift->(); $schema->txn_commit },
236 txn_guard => sub { my $g = $schema->txn_scope_guard; shift->(); $g->commit },
237 };
238 for my $wrapper (keys %$wrappers) {
239 $rs->reset;
240 $rs->delete;
241 $rs->create({ amount => 1000 + $_ }) for (1..3);
242
243 my $artist_rs = $schema->resultset('Artist')->search({
244 name => { -like => 'Artist %' }
245 });;
246
247 $rs->next;
248
249 my $map = [ ['Artist 1', '1002.00'], ['Artist 2', '1003.00'] ];
250
65d35121 251 weaken(my $a_rs_cp = $artist_rs);
252
c1e5a9ac 253 lives_and {
254 my @results;
c1e5a9ac 255 $wrappers->{$wrapper}->( sub {
65d35121 256 while (my $money = $rs_cp->next) {
257 my $artist = $a_rs_cp->next;
c1e5a9ac 258 push @results, [ $artist->name, $money->amount ];
259 };
260 });
261
262 is_deeply \@results, $map;
263 } "transactions with multiple active statement with $wrapper wrapper";
264 }
265 }
a218ef4e 266
267 # test RNO detection when version detection fails
268 SKIP: {
269 my $storage = $schema->storage;
270 my $version = $storage->_server_info->{normalized_dbms_version};
99083752 271
272 skip 'could not detect SQL Server version', 1 if not defined $version;
a218ef4e 273
274 my $have_rno = $version >= 9 ? 1 : 0;
275
bbdda281 276 local $storage->{_dbh_details}{info} = {}; # delete cache
4282b6f8 277
a218ef4e 278 my $rno_detected =
6a247f33 279 ($storage->sql_limit_dialect eq 'RowNumberOver') ? 1 : 0;
a218ef4e 280
233c3a46 281 ok (($have_rno == $rno_detected),
a218ef4e 282 'row_number() over support detected correctly');
283 }
0a064375 284
285 {
286 my $schema = DBICTest::Schema->clone;
287 $schema->connection($dsn, $user, $pass);
288
289 like $schema->storage->sql_maker->{limit_dialect},
290 qr/^(?:Top|RowNumberOver)\z/,
291 'sql_maker is correct on unconnected schema';
292 }
7379eb67 293}
5064f5c3 294
559ae74c 295# test op-induced autoconnect
296lives_ok (sub {
297
298 my $schema = DBICTest::Schema->clone;
299 $schema->connection($dsn, $user, $pass);
300
301 my $artist = $schema->resultset ('Artist')->search ({}, { order_by => 'artistid' })->next;
302 is ($artist->id, 1, 'Artist retrieved successfully');
303}, 'Query-induced autoconnect works');
304
c1e5a9ac 305# test AutoCommit=0
306{
307 local $ENV{DBIC_UNSAFE_AUTOCOMMIT_OK} = 1;
308 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass, { AutoCommit => 0 });
309
310 my $rs = $schema2->resultset('Money');
311
312 $rs->delete;
313 $schema2->txn_commit;
314
315 is $rs->count, 0, 'initially empty'
316 || diag ('Found row with amount ' . $_->amount) for $rs->all;
317
318 $rs->create({ amount => 3000 });
319 $schema2->txn_rollback;
320
321 is $rs->count, 0, 'rolled back in AutoCommit=0'
322 || diag ('Found row with amount ' . $_->amount) for $rs->all;
323
324 $rs->create({ amount => 4000 });
325 $schema2->txn_commit;
326
327 cmp_ok $rs->first->amount, '==', 4000, 'committed in AutoCommit=0';
328}
329
1a789a72 330done_testing;
331
3ff5b740 332# clean up our mess
333END {
7379eb67 334 if (my $dbh = eval { $schema->storage->dbh }) {
335 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL DROP TABLE artist");
336 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL DROP TABLE cd");
337 $dbh->do("IF OBJECT_ID('money_test', 'U') IS NOT NULL DROP TABLE money_test");
338 }
65d35121 339
340 undef $schema;
3ff5b740 341}