6 use File::Path 'rmtree';
7 use DBIx::Class::Optional::Dependencies;
8 use DBIx::Class::Schema::Loader 'make_schema_at';
9 use DBIx::Class::Schema::Loader::Utils 'split_name';
10 use String::ToIdentifier::EN::Unicode 'to_identifier';
15 use dbixcsl_common_tests ();
16 use dbixcsl_test_dir '$tdir';
18 use constant EXTRA_DUMP_DIR => "$tdir/informix_extra_dump";
20 # to support " quoted identifiers
21 BEGIN { $ENV{DELIMIDENT} = 'y' }
23 # This test doesn't run over a shared memory connection, because of the single connection limit.
25 my $dsn = $ENV{DBICTEST_INFORMIX_DSN} || '';
26 my $user = $ENV{DBICTEST_INFORMIX_USER} || '';
27 my $password = $ENV{DBICTEST_INFORMIX_PASS} || '';
29 my ($schema, $extra_schema); # for cleanup in END for extra tests
31 my $tester = dbixcsl_common_tests->new(
33 auto_inc_pk => 'serial primary key',
35 default_function => 'current year to fraction(5)',
36 default_function_def => 'datetime year to fraction(5) default current year to fraction(5)',
39 password => $password,
40 loader_options => { preserve_case => 1 },
43 # http://publib.boulder.ibm.com/infocenter/idshelp/v115/index.jsp?topic=/com.ibm.sqlr.doc/ids_sqr_094.htm
46 'int' => { data_type => 'integer' },
47 integer => { data_type => 'integer' },
48 int8 => { data_type => 'bigint' },
49 bigint => { data_type => 'bigint' },
50 serial => { data_type => 'integer', is_auto_increment => 1 },
51 bigserial => { data_type => 'bigint', is_auto_increment => 1 },
52 serial8 => { data_type => 'bigint', is_auto_increment => 1 },
53 smallint => { data_type => 'smallint' },
54 real => { data_type => 'real' },
55 smallfloat => { data_type => 'real' },
56 # just 'double' is a syntax error
57 'double precision' => { data_type => 'double precision' },
58 float => { data_type => 'double precision' },
59 'float(1)' => { data_type => 'double precision' },
60 'float(5)' => { data_type => 'double precision' },
61 'float(10)' => { data_type => 'double precision' },
62 'float(15)' => { data_type => 'double precision' },
63 'float(16)' => { data_type => 'double precision' },
64 numeric => { data_type => 'numeric' },
65 decimal => { data_type => 'numeric' },
66 dec => { data_type => 'numeric' },
67 'numeric(6,3)' => { data_type => 'numeric', size => [6,3] },
68 'decimal(6,3)' => { data_type => 'numeric', size => [6,3] },
69 'dec(6,3)' => { data_type => 'numeric', size => [6,3] },
72 # XXX this should map to 'boolean'
73 boolean => { data_type => 'smallint' },
76 money => { data_type => 'money' },
77 'money(3,3)' => { data_type => 'numeric', size => [3,3] },
80 byte => { data_type => 'bytea', original => { data_type => 'byte' } },
82 # Character String Types
83 char => { data_type => 'char', size => 1 },
84 'char(3)' => { data_type => 'char', size => 3 },
85 character => { data_type => 'char', size => 1 },
86 'character(3)' => { data_type => 'char', size => 3 },
87 'varchar(3)' => { data_type => 'varchar', size => 3 },
88 'character varying(3)'
89 => { data_type => 'varchar', size => 3 },
90 # XXX min size not supported, colmin from syscolumns is NULL
91 'varchar(3,2)' => { data_type => 'varchar', size => 3 },
92 'character varying(3,2)'
93 => { data_type => 'varchar', size => 3 },
94 nchar => { data_type => 'nchar', size => 1 },
95 'nchar(3)' => { data_type => 'nchar', size => 3 },
96 'nvarchar(3)' => { data_type => 'nvarchar', size => 3 },
97 'nvarchar(3,2)' => { data_type => 'nvarchar', size => 3 },
98 'lvarchar(3)' => { data_type => 'lvarchar', size => 3 },
99 'lvarchar(33)' => { data_type => 'lvarchar', size => 33 },
100 text => { data_type => 'text' },
103 date => { data_type => 'date' },
105 => { data_type => 'date', default_value => \'today' },
106 # XXX support all precisions
107 'datetime year to fraction(5)',
108 => { data_type => 'datetime year to fraction(5)' },
109 'datetime year to fraction(5) default current year to fraction(5)',
110 => { data_type => 'datetime year to fraction(5)', default_value => \'current year to fraction(5)' },
114 # XXX no way to distinguish opaque types boolean, blob and clob
115 blob => { data_type => 'blob' },
116 clob => { data_type => 'blob' },
118 # IDSSECURITYLABEL Type
120 # This requires the DBSECADM privilege and a security policy on the
121 # table, things I know nothing about.
122 # idssecuritylabel => { data_type => 'idssecuritylabel' },
125 # XXX need to introspect element type too
126 'list(varchar(20) not null)'
127 => { data_type => 'list' },
128 'multiset(varchar(20) not null)'
129 => { data_type => 'multiset' },
130 'set(varchar(20) not null)'
131 => { data_type => 'set' },
139 skip 'Set the DBICTEST_INFORMIX_EXTRADB_DSN, _USER and _PASS environment variables to run the multi-database tests', 26 * 2
140 unless $ENV{DBICTEST_INFORMIX_EXTRADB_DSN};
142 $extra_schema = $schema->clone;
143 $extra_schema->connection(@ENV{map "DBICTEST_INFORMIX_EXTRADB_$_",
147 my $dbh1 = $schema->storage->dbh;
150 CREATE TABLE informix_loader_test4 (
151 id SERIAL PRIMARY KEY,
156 CREATE TABLE informix_loader_test5 (
157 id SERIAL PRIMARY KEY,
159 four_id INTEGER REFERENCES informix_loader_test4 (id)
163 ALTER TABLE informix_loader_test5 ADD CONSTRAINT UNIQUE (four_id) CONSTRAINT loader_test5_uniq
166 my $db1 = db_name($schema);
170 my $dbh2 = $extra_schema->storage->dbh;
173 CREATE TABLE informix_loader_test5 (
174 pk SERIAL PRIMARY KEY,
180 ALTER TABLE informix_loader_test5 ADD CONSTRAINT UNIQUE (four_id) CONSTRAINT loader_test5_uniq
183 CREATE TABLE informix_loader_test6 (
184 id SERIAL PRIMARY KEY,
189 CREATE TABLE informix_loader_test7 (
190 id SERIAL PRIMARY KEY,
192 six_id INTEGER UNIQUE REFERENCES informix_loader_test6 (id)
196 my $db2 = db_name($extra_schema);
200 my $db1_moniker = join '', map ucfirst lc, split_name to_identifier $db1;
201 my $db2_moniker = join '', map ucfirst lc, split_name to_identifier $db2;
203 foreach my $db_schema ({ $db1 => '%', $db2 => '%' }, { '%' => '%' }) {
206 local $SIG{__WARN__} = sub {
207 push @warns, $_[0] unless $_[0] =~ /\bcollides\b/
208 || $_[0] =~ /unreferencable/;
212 'InformixMultiDatabase',
215 db_schema => $db_schema,
216 dump_directory => EXTRA_DUMP_DIR,
219 [ $dsn, $user, $password ],
222 InformixMultiDatabase->storage->disconnect;
224 diag join "\n", @warns if @warns;
227 } "dumped schema for databases $db1 and $db2 with no warnings";
232 ok $test_schema = InformixMultiDatabase->connect($dsn, $user, $password);
233 } 'connected test schema';
235 my ($rsrc, $rs, $row, $rel_info, %uniqs);
238 ok $rsrc = $test_schema->source("InformixLoaderTest4");
239 } 'got source for table in database one';
241 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
242 'column in database one';
244 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
245 'column in database one';
247 is try { $rsrc->column_info('value')->{size} }, 100,
248 'column in database one';
251 ok $rs = $test_schema->resultset("InformixLoaderTest4");
252 } 'got resultset for table in database one';
255 ok $row = $rs->create({ value => 'foo' });
256 } 'executed SQL on table in database one';
258 $rel_info = try { $rsrc->relationship_info("informix_loader_test5") };
260 is_deeply $rel_info->{cond}, {
261 'foreign.four_id' => 'self.id'
262 }, 'relationship in database one';
264 is $rel_info->{attrs}{accessor}, 'single',
265 'relationship in database one';
267 is $rel_info->{attrs}{join_type}, 'LEFT',
268 'relationship in database one';
271 ok $rsrc = $test_schema->source("${db1_moniker}InformixLoaderTest5");
272 } 'got source for table in database one';
274 %uniqs = try { $rsrc->unique_constraints };
277 'got unique and primary constraint in database one';
279 delete $uniqs{primary};
281 is_deeply ((values %uniqs)[0], ['four_id'],
282 'correct unique constraint in database one');
285 ok $rsrc = $test_schema->source("InformixLoaderTest6");
286 } 'got source for table in database two';
288 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
289 'column in database two introspected correctly';
291 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
292 'column in database two introspected correctly';
294 is try { $rsrc->column_info('value')->{size} }, 100,
295 'column in database two introspected correctly';
298 ok $rs = $test_schema->resultset("InformixLoaderTest6");
299 } 'got resultset for table in database two';
302 ok $row = $rs->create({ value => 'foo' });
303 } 'executed SQL on table in database two';
305 $rel_info = try { $rsrc->relationship_info('informix_loader_test7') };
307 is_deeply $rel_info->{cond}, {
308 'foreign.six_id' => 'self.id'
309 }, 'relationship in database two';
311 is $rel_info->{attrs}{accessor}, 'single',
312 'relationship in database two';
314 is $rel_info->{attrs}{join_type}, 'LEFT',
315 'relationship in database two';
318 ok $rsrc = $test_schema->source("InformixLoaderTest7");
319 } 'got source for table in database two';
321 %uniqs = try { $rsrc->unique_constraints };
324 'got unique and primary constraint in database two';
326 delete $uniqs{primary};
328 is_deeply ((values %uniqs)[0], ['six_id'],
329 'correct unique constraint in database two');
337 $tester->skip_tests('You need to set the DBICTEST_INFORMIX_DSN, _USER, and _PASS environment variables');
339 elsif (!DBIx::Class::Optional::Dependencies->req_ok_for ('rdbms_informix')) {
340 $tester->skip_tests('You need to install ' . DBIx::Class::Optional::Dependencies->req_missing_for ('rdbms_informix'));
343 $tester->run_tests();
349 # When we clone the schema, it still references the original loader, which
350 # references the original schema.
351 local $schema->loader->{schema} = $schema;
353 return $schema->loader->_current_db;
355 $schema->storage->disconnect;
359 if (not $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}) {
360 if (my $dbh2 = try { $extra_schema->storage->dbh }) {
363 $dbh2->do('DROP TABLE informix_loader_test7');
364 $dbh2->do('DROP TABLE informix_loader_test6');
365 $dbh2->do('DROP TABLE informix_loader_test5');
368 die "Error dropping test tables: $_";
374 if (my $dbh1 = try { $schema->storage->dbh }) {
377 $dbh1->do('DROP TABLE informix_loader_test5');
378 $dbh1->do('DROP TABLE informix_loader_test4');
381 die "Error dropping test tables: $_";
387 rmtree EXTRA_DUMP_DIR;
390 # vim:et sts=4 sw=4 tw=0: