Attempt to fix 'Attempt to free unreferenced scalar' on 5.8
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 10_10informix_common.t
CommitLineData
406a97c2 1use DBIx::Class::Schema::Loader::Optional::Dependencies
2 -skip_all_without => 'test_rdbms_informix';
3
bc5afe55 4use strict;
c4a69b87 5use warnings;
6use Test::More;
7use Test::Exception;
8use Try::Tiny;
9use File::Path 'rmtree';
10use DBIx::Class::Schema::Loader 'make_schema_at';
5975bbe6 11use DBIx::Class::Schema::Loader::Utils 'split_name';
12use String::ToIdentifier::EN::Unicode 'to_identifier';
c4a69b87 13
bc5afe55 14use lib qw(t/lib);
c4a69b87 15
16use dbixcsl_common_tests ();
17use dbixcsl_test_dir '$tdir';
18
19use constant EXTRA_DUMP_DIR => "$tdir/informix_extra_dump";
bc5afe55 20
b511f36e 21# to support " quoted identifiers
22BEGIN { $ENV{DELIMIDENT} = 'y' }
23
bc5afe55 24# This test doesn't run over a shared memory connection, because of the single connection limit.
25
26my $dsn = $ENV{DBICTEST_INFORMIX_DSN} || '';
27my $user = $ENV{DBICTEST_INFORMIX_USER} || '';
28my $password = $ENV{DBICTEST_INFORMIX_PASS} || '';
29
c4a69b87 30my ($schema, $extra_schema); # for cleanup in END for extra tests
31
406a97c2 32dbixcsl_common_tests->new(
bc5afe55 33 vendor => 'Informix',
c7e6dc1f 34 auto_inc_pk => 'serial primary key',
bc5afe55 35 null => '',
c7e6dc1f 36 default_function => 'current year to fraction(5)',
37 default_function_def => 'datetime year to fraction(5) default current year to fraction(5)',
bc5afe55 38 dsn => $dsn,
39 user => $user,
40 password => $password,
b511f36e 41 loader_options => { preserve_case => 1 },
42 quote_char => '"',
c7e6dc1f 43 data_types => {
44 # http://publib.boulder.ibm.com/infocenter/idshelp/v115/index.jsp?topic=/com.ibm.sqlr.doc/ids_sqr_094.htm
45
46 # Numeric Types
47 'int' => { data_type => 'integer' },
48 integer => { data_type => 'integer' },
49 int8 => { data_type => 'bigint' },
50 bigint => { data_type => 'bigint' },
51 serial => { data_type => 'integer', is_auto_increment => 1 },
52 bigserial => { data_type => 'bigint', is_auto_increment => 1 },
53 serial8 => { data_type => 'bigint', is_auto_increment => 1 },
54 smallint => { data_type => 'smallint' },
55 real => { data_type => 'real' },
56 smallfloat => { data_type => 'real' },
57 # just 'double' is a syntax error
58 'double precision' => { data_type => 'double precision' },
59 float => { data_type => 'double precision' },
60 'float(1)' => { data_type => 'double precision' },
61 'float(5)' => { data_type => 'double precision' },
62 'float(10)' => { data_type => 'double precision' },
63 'float(15)' => { data_type => 'double precision' },
64 'float(16)' => { data_type => 'double precision' },
65 numeric => { data_type => 'numeric' },
66 decimal => { data_type => 'numeric' },
67 dec => { data_type => 'numeric' },
494e0205 68 'numeric(6,3)' => { data_type => 'numeric', size => [6,3] },
69 'decimal(6,3)' => { data_type => 'numeric', size => [6,3] },
70 'dec(6,3)' => { data_type => 'numeric', size => [6,3] },
c7e6dc1f 71
72 # Boolean Type
73 # XXX this should map to 'boolean'
74 boolean => { data_type => 'smallint' },
75
76 # Money Type
77 money => { data_type => 'money' },
78 'money(3,3)' => { data_type => 'numeric', size => [3,3] },
79
80 # Byte Type
81 byte => { data_type => 'bytea', original => { data_type => 'byte' } },
82
83 # Character String Types
84 char => { data_type => 'char', size => 1 },
85 'char(3)' => { data_type => 'char', size => 3 },
86 character => { data_type => 'char', size => 1 },
87 'character(3)' => { data_type => 'char', size => 3 },
88 'varchar(3)' => { data_type => 'varchar', size => 3 },
89 'character varying(3)'
90 => { data_type => 'varchar', size => 3 },
91 # XXX min size not supported, colmin from syscolumns is NULL
92 'varchar(3,2)' => { data_type => 'varchar', size => 3 },
93 'character varying(3,2)'
94 => { data_type => 'varchar', size => 3 },
95 nchar => { data_type => 'nchar', size => 1 },
96 'nchar(3)' => { data_type => 'nchar', size => 3 },
97 'nvarchar(3)' => { data_type => 'nvarchar', size => 3 },
98 'nvarchar(3,2)' => { data_type => 'nvarchar', size => 3 },
99 'lvarchar(3)' => { data_type => 'lvarchar', size => 3 },
100 'lvarchar(33)' => { data_type => 'lvarchar', size => 33 },
101 text => { data_type => 'text' },
102
103 # DateTime Types
104 date => { data_type => 'date' },
105 'date default today'
106 => { data_type => 'date', default_value => \'today' },
107 # XXX support all precisions
108 'datetime year to fraction(5)',
109 => { data_type => 'datetime year to fraction(5)' },
110 'datetime year to fraction(5) default current year to fraction(5)',
111 => { data_type => 'datetime year to fraction(5)', default_value => \'current year to fraction(5)' },
112 # XXX do interval
113
114 # Blob Types
115 # XXX no way to distinguish opaque types boolean, blob and clob
116 blob => { data_type => 'blob' },
117 clob => { data_type => 'blob' },
118
119 # IDSSECURITYLABEL Type
120 #
121 # This requires the DBSECADM privilege and a security policy on the
122 # table, things I know nothing about.
123# idssecuritylabel => { data_type => 'idssecuritylabel' },
124
125 # List Types
126 # XXX need to introspect element type too
127 'list(varchar(20) not null)'
128 => { data_type => 'list' },
129 'multiset(varchar(20) not null)'
130 => { data_type => 'multiset' },
131 'set(varchar(20) not null)'
132 => { data_type => 'set' },
133 },
c4a69b87 134 extra => {
5975bbe6 135 count => 26 * 2,
c4a69b87 136 run => sub {
137 ($schema) = @_;
138
139 SKIP: {
5975bbe6 140 skip 'Set the DBICTEST_INFORMIX_EXTRADB_DSN, _USER and _PASS environment variables to run the multi-database tests', 26 * 2
c4a69b87 141 unless $ENV{DBICTEST_INFORMIX_EXTRADB_DSN};
142
143 $extra_schema = $schema->clone;
144 $extra_schema->connection(@ENV{map "DBICTEST_INFORMIX_EXTRADB_$_",
145 qw/DSN USER PASS/
146 });
147
148 my $dbh1 = $schema->storage->dbh;
c4a69b87 149
150 $dbh1->do(<<'EOF');
151 CREATE TABLE informix_loader_test4 (
152 id SERIAL PRIMARY KEY,
153 value VARCHAR(100)
154 )
155EOF
156 $dbh1->do(<<'EOF');
157 CREATE TABLE informix_loader_test5 (
158 id SERIAL PRIMARY KEY,
159 value VARCHAR(100),
5975bbe6 160 four_id INTEGER REFERENCES informix_loader_test4 (id)
c4a69b87 161 )
162EOF
5975bbe6 163 $dbh1->do(<<'EOF');
164ALTER TABLE informix_loader_test5 ADD CONSTRAINT UNIQUE (four_id) CONSTRAINT loader_test5_uniq
165EOF
e8c7b904 166
167 my $db1 = db_name($schema);
168
169 $dbh1->disconnect;
170
171 my $dbh2 = $extra_schema->storage->dbh;
172
5975bbe6 173 $dbh2->do(<<'EOF');
174 CREATE TABLE informix_loader_test5 (
175 pk SERIAL PRIMARY KEY,
176 value VARCHAR(100),
177 four_id INTEGER
178 )
179EOF
180 $dbh2->do(<<'EOF');
181ALTER TABLE informix_loader_test5 ADD CONSTRAINT UNIQUE (four_id) CONSTRAINT loader_test5_uniq
182EOF
c4a69b87 183 $dbh2->do(<<"EOF");
184 CREATE TABLE informix_loader_test6 (
185 id SERIAL PRIMARY KEY,
186 value VARCHAR(100)
187 )
188EOF
189 $dbh2->do(<<"EOF");
190 CREATE TABLE informix_loader_test7 (
191 id SERIAL PRIMARY KEY,
192 value VARCHAR(100),
193 six_id INTEGER UNIQUE REFERENCES informix_loader_test6 (id)
194 )
195EOF
c4a69b87 196
5975bbe6 197 my $db2 = db_name($extra_schema);
198
e8c7b904 199 $dbh2->disconnect;
200
5975bbe6 201 my $db1_moniker = join '', map ucfirst lc, split_name to_identifier $db1;
202 my $db2_moniker = join '', map ucfirst lc, split_name to_identifier $db2;
203
204 foreach my $db_schema ({ $db1 => '%', $db2 => '%' }, { '%' => '%' }) {
205 lives_and {
206 my @warns;
207 local $SIG{__WARN__} = sub {
208 push @warns, $_[0] unless $_[0] =~ /\bcollides\b/
209 || $_[0] =~ /unreferencable/;
210 };
494e0205 211
5975bbe6 212 make_schema_at(
213 'InformixMultiDatabase',
214 {
215 naming => 'current',
216 db_schema => $db_schema,
5975bbe6 217 dump_directory => EXTRA_DUMP_DIR,
218 quiet => 1,
219 },
220 [ $dsn, $user, $password ],
221 );
c4a69b87 222
e8c7b904 223 InformixMultiDatabase->storage->disconnect;
224
5975bbe6 225 diag join "\n", @warns if @warns;
c4a69b87 226
5975bbe6 227 is @warns, 0;
228 } "dumped schema for databases $db1 and $db2 with no warnings";
c4a69b87 229
5975bbe6 230 my $test_schema;
c4a69b87 231
5975bbe6 232 lives_and {
233 ok $test_schema = InformixMultiDatabase->connect($dsn, $user, $password);
234 } 'connected test schema';
c4a69b87 235
5975bbe6 236 my ($rsrc, $rs, $row, $rel_info, %uniqs);
c4a69b87 237
5975bbe6 238 lives_and {
4c2e2ce9 239 ok $rsrc = $test_schema->source("InformixLoaderTest4");
5975bbe6 240 } 'got source for table in database one';
c4a69b87 241
5975bbe6 242 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
243 'column in database one';
c4a69b87 244
5975bbe6 245 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
246 'column in database one';
c4a69b87 247
5975bbe6 248 is try { $rsrc->column_info('value')->{size} }, 100,
249 'column in database one';
c4a69b87 250
5975bbe6 251 lives_and {
4c2e2ce9 252 ok $rs = $test_schema->resultset("InformixLoaderTest4");
5975bbe6 253 } 'got resultset for table in database one';
c4a69b87 254
5975bbe6 255 lives_and {
256 ok $row = $rs->create({ value => 'foo' });
257 } 'executed SQL on table in database one';
c4a69b87 258
5975bbe6 259 $rel_info = try { $rsrc->relationship_info("informix_loader_test5") };
c4a69b87 260
5975bbe6 261 is_deeply $rel_info->{cond}, {
262 'foreign.four_id' => 'self.id'
263 }, 'relationship in database one';
c4a69b87 264
5975bbe6 265 is $rel_info->{attrs}{accessor}, 'single',
266 'relationship in database one';
c4a69b87 267
5975bbe6 268 is $rel_info->{attrs}{join_type}, 'LEFT',
269 'relationship in database one';
c4a69b87 270
5975bbe6 271 lives_and {
272 ok $rsrc = $test_schema->source("${db1_moniker}InformixLoaderTest5");
273 } 'got source for table in database one';
c4a69b87 274
5975bbe6 275 %uniqs = try { $rsrc->unique_constraints };
c4a69b87 276
5975bbe6 277 is keys %uniqs, 2,
278 'got unique and primary constraint in database one';
c4a69b87 279
5975bbe6 280 delete $uniqs{primary};
c4a69b87 281
5975bbe6 282 is_deeply ((values %uniqs)[0], ['four_id'],
283 'correct unique constraint in database one');
c4a69b87 284
5975bbe6 285 lives_and {
4c2e2ce9 286 ok $rsrc = $test_schema->source("InformixLoaderTest6");
5975bbe6 287 } 'got source for table in database two';
c4a69b87 288
5975bbe6 289 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
290 'column in database two introspected correctly';
c4a69b87 291
5975bbe6 292 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
293 'column in database two introspected correctly';
c4a69b87 294
5975bbe6 295 is try { $rsrc->column_info('value')->{size} }, 100,
296 'column in database two introspected correctly';
c4a69b87 297
5975bbe6 298 lives_and {
4c2e2ce9 299 ok $rs = $test_schema->resultset("InformixLoaderTest6");
5975bbe6 300 } 'got resultset for table in database two';
c4a69b87 301
5975bbe6 302 lives_and {
303 ok $row = $rs->create({ value => 'foo' });
304 } 'executed SQL on table in database two';
c4a69b87 305
5975bbe6 306 $rel_info = try { $rsrc->relationship_info('informix_loader_test7') };
c4a69b87 307
5975bbe6 308 is_deeply $rel_info->{cond}, {
309 'foreign.six_id' => 'self.id'
310 }, 'relationship in database two';
c4a69b87 311
5975bbe6 312 is $rel_info->{attrs}{accessor}, 'single',
313 'relationship in database two';
c4a69b87 314
5975bbe6 315 is $rel_info->{attrs}{join_type}, 'LEFT',
316 'relationship in database two';
317
318 lives_and {
4c2e2ce9 319 ok $rsrc = $test_schema->source("InformixLoaderTest7");
5975bbe6 320 } 'got source for table in database two';
321
322 %uniqs = try { $rsrc->unique_constraints };
323
324 is keys %uniqs, 2,
325 'got unique and primary constraint in database two';
326
327 delete $uniqs{primary};
328
329 is_deeply ((values %uniqs)[0], ['six_id'],
330 'correct unique constraint in database two');
331 }
c4a69b87 332 }
333 },
334 },
406a97c2 335)->run_tests();
c4a69b87 336
5975bbe6 337sub db_name {
338 my $schema = shift;
339
340 # When we clone the schema, it still references the original loader, which
341 # references the original schema.
342 local $schema->loader->{schema} = $schema;
343
344 return $schema->loader->_current_db;
e8c7b904 345
346 $schema->storage->disconnect;
5975bbe6 347}
348
c4a69b87 349END {
350 if (not $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}) {
351 if (my $dbh2 = try { $extra_schema->storage->dbh }) {
c4a69b87 352
353 try {
354 $dbh2->do('DROP TABLE informix_loader_test7');
355 $dbh2->do('DROP TABLE informix_loader_test6');
5975bbe6 356 $dbh2->do('DROP TABLE informix_loader_test5');
e8c7b904 357 }
358 catch {
359 die "Error dropping test tables: $_";
360 };
361
362 $dbh2->disconnect;
363 }
364
365 if (my $dbh1 = try { $schema->storage->dbh }) {
494e0205 366
e8c7b904 367 try {
c4a69b87 368 $dbh1->do('DROP TABLE informix_loader_test5');
369 $dbh1->do('DROP TABLE informix_loader_test4');
370 }
371 catch {
372 die "Error dropping test tables: $_";
373 };
e8c7b904 374
375 $dbh1->disconnect;
c4a69b87 376 }
377
378 rmtree EXTRA_DUMP_DIR;
379 }
380}
bc5afe55 381# vim:et sts=4 sw=4 tw=0: