Attempt to fix 'Attempt to free unreferenced scalar' on 5.8
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 10_10informix_common.t
1 use DBIx::Class::Schema::Loader::Optional::Dependencies
2     -skip_all_without => 'test_rdbms_informix';
3
4 use strict;
5 use warnings;
6 use Test::More;
7 use Test::Exception;
8 use Try::Tiny;
9 use File::Path 'rmtree';
10 use DBIx::Class::Schema::Loader 'make_schema_at';
11 use DBIx::Class::Schema::Loader::Utils 'split_name';
12 use String::ToIdentifier::EN::Unicode 'to_identifier';
13
14 use lib qw(t/lib);
15
16 use dbixcsl_common_tests ();
17 use dbixcsl_test_dir '$tdir';
18
19 use constant EXTRA_DUMP_DIR => "$tdir/informix_extra_dump";
20
21 # to support " quoted identifiers
22 BEGIN { $ENV{DELIMIDENT} = 'y' }
23
24 # This test doesn't run over a shared memory connection, because of the single connection limit.
25
26 my $dsn      = $ENV{DBICTEST_INFORMIX_DSN} || '';
27 my $user     = $ENV{DBICTEST_INFORMIX_USER} || '';
28 my $password = $ENV{DBICTEST_INFORMIX_PASS} || '';
29
30 my ($schema, $extra_schema); # for cleanup in END for extra tests
31
32 dbixcsl_common_tests->new(
33     vendor         => 'Informix',
34     auto_inc_pk    => 'serial primary key',
35     null           => '',
36     default_function     => 'current year to fraction(5)',
37     default_function_def => 'datetime year to fraction(5) default current year to fraction(5)',
38     dsn            => $dsn,
39     user           => $user,
40     password       => $password,
41     loader_options => { preserve_case => 1 },
42     quote_char     => '"',
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' },
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] },
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     },
134     extra => {
135         count => 26 * 2,
136         run   => sub {
137             ($schema) = @_;
138
139             SKIP: {
140                 skip 'Set the DBICTEST_INFORMIX_EXTRADB_DSN, _USER and _PASS environment variables to run the multi-database tests', 26 * 2
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;
149
150                 $dbh1->do(<<'EOF');
151                     CREATE TABLE informix_loader_test4 (
152                         id SERIAL PRIMARY KEY,
153                         value VARCHAR(100)
154                     )
155 EOF
156                 $dbh1->do(<<'EOF');
157                     CREATE TABLE informix_loader_test5 (
158                         id SERIAL PRIMARY KEY,
159                         value VARCHAR(100),
160                         four_id INTEGER REFERENCES informix_loader_test4 (id)
161                     )
162 EOF
163                 $dbh1->do(<<'EOF');
164 ALTER TABLE informix_loader_test5 ADD CONSTRAINT UNIQUE (four_id) CONSTRAINT loader_test5_uniq
165 EOF
166
167                 my $db1 = db_name($schema);
168
169                 $dbh1->disconnect;
170
171                 my $dbh2 = $extra_schema->storage->dbh;
172
173                 $dbh2->do(<<'EOF');
174                     CREATE TABLE informix_loader_test5 (
175                         pk SERIAL PRIMARY KEY,
176                         value VARCHAR(100),
177                         four_id INTEGER
178                     )
179 EOF
180                 $dbh2->do(<<'EOF');
181 ALTER TABLE informix_loader_test5 ADD CONSTRAINT UNIQUE (four_id) CONSTRAINT loader_test5_uniq
182 EOF
183                 $dbh2->do(<<"EOF");
184                     CREATE TABLE informix_loader_test6 (
185                         id SERIAL PRIMARY KEY,
186                         value VARCHAR(100)
187                     )
188 EOF
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                     )
195 EOF
196
197                 my $db2 = db_name($extra_schema);
198
199                 $dbh2->disconnect;
200
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                         };
211
212                         make_schema_at(
213                             'InformixMultiDatabase',
214                             {
215                                 naming => 'current',
216                                 db_schema => $db_schema,
217                                 dump_directory => EXTRA_DUMP_DIR,
218                                 quiet => 1,
219                             },
220                             [ $dsn, $user, $password ],
221                         );
222
223                         InformixMultiDatabase->storage->disconnect;
224
225                         diag join "\n", @warns if @warns;
226
227                         is @warns, 0;
228                     } "dumped schema for databases $db1 and $db2 with no warnings";
229
230                     my $test_schema;
231
232                     lives_and {
233                         ok $test_schema = InformixMultiDatabase->connect($dsn, $user, $password);
234                     } 'connected test schema';
235
236                     my ($rsrc, $rs, $row, $rel_info, %uniqs);
237
238                     lives_and {
239                         ok $rsrc = $test_schema->source("InformixLoaderTest4");
240                     } 'got source for table in database one';
241
242                     is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
243                         'column in database one';
244
245                     is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
246                         'column in database one';
247
248                     is try { $rsrc->column_info('value')->{size} }, 100,
249                         'column in database one';
250
251                     lives_and {
252                         ok $rs = $test_schema->resultset("InformixLoaderTest4");
253                     } 'got resultset for table in database one';
254
255                     lives_and {
256                         ok $row = $rs->create({ value => 'foo' });
257                     } 'executed SQL on table in database one';
258
259                     $rel_info = try { $rsrc->relationship_info("informix_loader_test5") };
260
261                     is_deeply $rel_info->{cond}, {
262                         'foreign.four_id' => 'self.id'
263                     }, 'relationship in database one';
264
265                     is $rel_info->{attrs}{accessor}, 'single',
266                         'relationship in database one';
267
268                     is $rel_info->{attrs}{join_type}, 'LEFT',
269                         'relationship in database one';
270
271                     lives_and {
272                         ok $rsrc = $test_schema->source("${db1_moniker}InformixLoaderTest5");
273                     } 'got source for table in database one';
274
275                     %uniqs = try { $rsrc->unique_constraints };
276
277                     is keys %uniqs, 2,
278                         'got unique and primary constraint in database one';
279
280                     delete $uniqs{primary};
281
282                     is_deeply ((values %uniqs)[0], ['four_id'],
283                         'correct unique constraint in database one');
284
285                     lives_and {
286                         ok $rsrc = $test_schema->source("InformixLoaderTest6");
287                     } 'got source for table in database two';
288
289                     is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
290                         'column in database two introspected correctly';
291
292                     is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
293                         'column in database two introspected correctly';
294
295                     is try { $rsrc->column_info('value')->{size} }, 100,
296                         'column in database two introspected correctly';
297
298                     lives_and {
299                         ok $rs = $test_schema->resultset("InformixLoaderTest6");
300                     } 'got resultset for table in database two';
301
302                     lives_and {
303                         ok $row = $rs->create({ value => 'foo' });
304                     } 'executed SQL on table in database two';
305
306                     $rel_info = try { $rsrc->relationship_info('informix_loader_test7') };
307
308                     is_deeply $rel_info->{cond}, {
309                         'foreign.six_id' => 'self.id'
310                     }, 'relationship in database two';
311
312                     is $rel_info->{attrs}{accessor}, 'single',
313                         'relationship in database two';
314
315                     is $rel_info->{attrs}{join_type}, 'LEFT',
316                         'relationship in database two';
317
318                     lives_and {
319                         ok $rsrc = $test_schema->source("InformixLoaderTest7");
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                 }
332             }
333         },
334     },
335 )->run_tests();
336
337 sub 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;
345
346     $schema->storage->disconnect;
347 }
348
349 END {
350     if (not $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}) {
351         if (my $dbh2 = try { $extra_schema->storage->dbh }) {
352
353             try {
354                 $dbh2->do('DROP TABLE informix_loader_test7');
355                 $dbh2->do('DROP TABLE informix_loader_test6');
356                 $dbh2->do('DROP TABLE informix_loader_test5');
357             }
358             catch {
359                 die "Error dropping test tables: $_";
360             };
361
362             $dbh2->disconnect;
363         }
364
365         if (my $dbh1 = try { $schema->storage->dbh }) {
366
367             try {
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             };
374
375             $dbh1->disconnect;
376         }
377
378         rmtree EXTRA_DUMP_DIR;
379     }
380 }
381 # vim:et sts=4 sw=4 tw=0: