1 use DBIx::Class::Schema::Loader::Optional::Dependencies
2 -skip_all_without => 'test_rdbms_informix';
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';
16 use dbixcsl_common_tests ();
17 use dbixcsl_test_dir '$tdir';
19 use constant EXTRA_DUMP_DIR => "$tdir/informix_extra_dump";
21 # to support " quoted identifiers
22 BEGIN { $ENV{DELIMIDENT} = 'y' }
24 # This test doesn't run over a shared memory connection, because of the single connection limit.
26 my $dsn = $ENV{DBICTEST_INFORMIX_DSN} || '';
27 my $user = $ENV{DBICTEST_INFORMIX_USER} || '';
28 my $password = $ENV{DBICTEST_INFORMIX_PASS} || '';
30 my ($schema, $extra_schema); # for cleanup in END for extra tests
32 dbixcsl_common_tests->new(
34 auto_inc_pk => 'serial primary key',
36 default_function => 'current year to fraction(5)',
37 default_function_def => 'datetime year to fraction(5) default current year to fraction(5)',
40 password => $password,
41 loader_options => { preserve_case => 1 },
44 # http://publib.boulder.ibm.com/infocenter/idshelp/v115/index.jsp?topic=/com.ibm.sqlr.doc/ids_sqr_094.htm
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] },
73 # XXX this should map to 'boolean'
74 boolean => { data_type => 'smallint' },
77 money => { data_type => 'money' },
78 'money(3,3)' => { data_type => 'numeric', size => [3,3] },
81 byte => { data_type => 'bytea', original => { data_type => 'byte' } },
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' },
104 date => { data_type => 'date' },
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)' },
115 # XXX no way to distinguish opaque types boolean, blob and clob
116 blob => { data_type => 'blob' },
117 clob => { data_type => 'blob' },
119 # IDSSECURITYLABEL Type
121 # This requires the DBSECADM privilege and a security policy on the
122 # table, things I know nothing about.
123 # idssecuritylabel => { data_type => 'idssecuritylabel' },
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' },
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};
143 $extra_schema = $schema->clone;
144 $extra_schema->connection(@ENV{map "DBICTEST_INFORMIX_EXTRADB_$_",
148 my $dbh1 = $schema->storage->dbh;
151 CREATE TABLE informix_loader_test4 (
152 id SERIAL PRIMARY KEY,
157 CREATE TABLE informix_loader_test5 (
158 id SERIAL PRIMARY KEY,
160 four_id INTEGER REFERENCES informix_loader_test4 (id)
164 ALTER TABLE informix_loader_test5 ADD CONSTRAINT UNIQUE (four_id) CONSTRAINT loader_test5_uniq
167 my $db1 = db_name($schema);
171 my $dbh2 = $extra_schema->storage->dbh;
174 CREATE TABLE informix_loader_test5 (
175 pk SERIAL PRIMARY KEY,
181 ALTER TABLE informix_loader_test5 ADD CONSTRAINT UNIQUE (four_id) CONSTRAINT loader_test5_uniq
184 CREATE TABLE informix_loader_test6 (
185 id SERIAL PRIMARY KEY,
190 CREATE TABLE informix_loader_test7 (
191 id SERIAL PRIMARY KEY,
193 six_id INTEGER UNIQUE REFERENCES informix_loader_test6 (id)
197 my $db2 = db_name($extra_schema);
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;
204 foreach my $db_schema ({ $db1 => '%', $db2 => '%' }, { '%' => '%' }) {
207 local $SIG{__WARN__} = sub {
208 push @warns, $_[0] unless $_[0] =~ /\bcollides\b/
209 || $_[0] =~ /unreferencable/;
213 'InformixMultiDatabase',
216 db_schema => $db_schema,
217 dump_directory => EXTRA_DUMP_DIR,
220 [ $dsn, $user, $password ],
223 InformixMultiDatabase->storage->disconnect;
225 diag join "\n", @warns if @warns;
228 } "dumped schema for databases $db1 and $db2 with no warnings";
233 ok $test_schema = InformixMultiDatabase->connect($dsn, $user, $password);
234 } 'connected test schema';
236 my ($rsrc, $rs, $row, $rel_info, %uniqs);
239 ok $rsrc = $test_schema->source("InformixLoaderTest4");
240 } 'got source for table in database one';
242 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
243 'column in database one';
245 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
246 'column in database one';
248 is try { $rsrc->column_info('value')->{size} }, 100,
249 'column in database one';
252 ok $rs = $test_schema->resultset("InformixLoaderTest4");
253 } 'got resultset for table in database one';
256 ok $row = $rs->create({ value => 'foo' });
257 } 'executed SQL on table in database one';
259 $rel_info = try { $rsrc->relationship_info("informix_loader_test5") };
261 is_deeply $rel_info->{cond}, {
262 'foreign.four_id' => 'self.id'
263 }, 'relationship in database one';
265 is $rel_info->{attrs}{accessor}, 'single',
266 'relationship in database one';
268 is $rel_info->{attrs}{join_type}, 'LEFT',
269 'relationship in database one';
272 ok $rsrc = $test_schema->source("${db1_moniker}InformixLoaderTest5");
273 } 'got source for table in database one';
275 %uniqs = try { $rsrc->unique_constraints };
278 'got unique and primary constraint in database one';
280 delete $uniqs{primary};
282 is_deeply ((values %uniqs)[0], ['four_id'],
283 'correct unique constraint in database one');
286 ok $rsrc = $test_schema->source("InformixLoaderTest6");
287 } 'got source for table in database two';
289 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
290 'column in database two introspected correctly';
292 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
293 'column in database two introspected correctly';
295 is try { $rsrc->column_info('value')->{size} }, 100,
296 'column in database two introspected correctly';
299 ok $rs = $test_schema->resultset("InformixLoaderTest6");
300 } 'got resultset for table in database two';
303 ok $row = $rs->create({ value => 'foo' });
304 } 'executed SQL on table in database two';
306 $rel_info = try { $rsrc->relationship_info('informix_loader_test7') };
308 is_deeply $rel_info->{cond}, {
309 'foreign.six_id' => 'self.id'
310 }, 'relationship in database two';
312 is $rel_info->{attrs}{accessor}, 'single',
313 'relationship in database two';
315 is $rel_info->{attrs}{join_type}, 'LEFT',
316 'relationship in database two';
319 ok $rsrc = $test_schema->source("InformixLoaderTest7");
320 } 'got source for table in database two';
322 %uniqs = try { $rsrc->unique_constraints };
325 'got unique and primary constraint in database two';
327 delete $uniqs{primary};
329 is_deeply ((values %uniqs)[0], ['six_id'],
330 'correct unique constraint in database two');
340 # When we clone the schema, it still references the original loader, which
341 # references the original schema.
342 local $schema->loader->{schema} = $schema;
344 return $schema->loader->_current_db;
346 $schema->storage->disconnect;
350 if (not $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}) {
351 if (my $dbh2 = try { $extra_schema->storage->dbh }) {
354 $dbh2->do('DROP TABLE informix_loader_test7');
355 $dbh2->do('DROP TABLE informix_loader_test6');
356 $dbh2->do('DROP TABLE informix_loader_test5');
359 die "Error dropping test tables: $_";
365 if (my $dbh1 = try { $schema->storage->dbh }) {
368 $dbh1->do('DROP TABLE informix_loader_test5');
369 $dbh1->do('DROP TABLE informix_loader_test4');
372 die "Error dropping test tables: $_";
378 rmtree EXTRA_DUMP_DIR;
381 # vim:et sts=4 sw=4 tw=0: