6 use File::Path 'rmtree';
7 use DBIx::Class::Optional::Dependencies;
8 use DBIx::Class::Schema::Loader 'make_schema_at';
12 use dbixcsl_common_tests ();
13 use dbixcsl_test_dir '$tdir';
15 use constant EXTRA_DUMP_DIR => "$tdir/db2_extra_dump";
17 my $dsn = $ENV{DBICTEST_DB2_DSN} || '';
18 my $user = $ENV{DBICTEST_DB2_USER} || '';
19 my $password = $ENV{DBICTEST_DB2_PASS} || '';
21 plan skip_all => 'You need to set the DBICTEST_DB2_DSN, _USER, and _PASS environment variables'
22 unless ($dsn && $user);
24 plan skip_all => 'You need to install ' . DBIx::Class::Optional::Dependencies->req_missing_for ('rdbms_db2')
25 unless DBIx::Class::Optional::Dependencies->req_ok_for ('rdbms_db2');
27 my ($schema, $schemas_created); # for cleanup in END for extra tests
31 my $dbh = DBI->connect ($dsn, $user, $password, { RaiseError => 1, PrintError => 0} );
32 eval { $dbh->get_info(18) } || 0;
34 my ($maj_srv_ver) = $srv_ver =~ /^(\d+)/;
36 my $extra_graphics_data_types = {
37 graphic => { data_type => 'graphic', size => 1 },
38 'graphic(3)' => { data_type => 'graphic', size => 3 },
39 'vargraphic(3)' => { data_type => 'vargraphic', size => 3 },
40 'long vargraphic' => { data_type => 'long vargraphic' },
41 'dbclob' => { data_type => 'dbclob' },
44 my $tester = dbixcsl_common_tests->new(
46 auto_inc_pk => 'INTEGER GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY',
49 password => $password,
51 preserve_case_mode_is_exclusive => 1,
53 default_is_deferrable => 1,
54 default_on_clause => 'NO ACTION',
56 # http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.doc/admin/r0008483.htm
59 smallint => { data_type => 'smallint' },
60 integer => { data_type => 'integer' },
61 'int' => { data_type => 'integer' },
62 real => { data_type => 'real' },
63 'double precision' => { data_type => 'double precision' },
64 double => { data_type => 'double precision' },
65 float => { data_type => 'double precision' },
66 'float(24)' => { data_type => 'real' },
67 'float(25)' => { data_type => 'double precision' },
68 'float(53)' => { data_type => 'double precision' },
69 numeric => { data_type => 'numeric' },
70 decimal => { data_type => 'numeric' },
71 'numeric(6,3)' => { data_type => 'numeric', size => [6,3] },
72 'decimal(6,3)' => { data_type => 'numeric', size => [6,3] },
74 # Character String Types
75 char => { data_type => 'char', size => 1 },
76 'char(3)' => { data_type => 'char', size => 3 },
77 'varchar(3)' => { data_type => 'varchar', size => 3 },
78 'long varchar' => { data_type => 'long varchar' },
79 'clob' => { data_type => 'clob' },
81 # Graphic String Types (double-byte strings)
82 ($maj_srv_ver >= 9) ? (%$extra_graphics_data_types) : (),
85 'char for bit data'=> { data_type => 'binary', size => 1, original => { data_type => 'char for bit data' } },
86 'char(3) for bit data'
87 => { data_type => 'binary', size => 3, original => { data_type => 'char for bit data' } },
88 'varchar(3) for bit data'
89 => { data_type => 'varbinary', size => 3, original => { data_type => 'varchar for bit data' } },
90 'long varchar for bit data'
91 => { data_type => 'blob', original => { data_type => 'long varchar for bit data' } },
92 blob => { data_type => 'blob' },
95 'date' => { data_type => 'date' },
96 'date default current date'
97 => { data_type => 'date', default_value => \'current_timestamp',
98 original => { default_value => \'current date' } },
99 'time' => { data_type => 'time' },
100 'time default current time'
101 => { data_type => 'time', default_value => \'current_timestamp',
102 original => { default_value => \'current time' } },
103 timestamp => { data_type => 'timestamp' },
104 'timestamp default current timestamp'
105 => { data_type => 'timestamp', default_value => \'current_timestamp',
106 original => { default_value => \'current timestamp' } },
109 # XXX I don't know how to make these
110 # datalink => { data_type => 'datalink' },
114 # 4 through 8 are used for the multi-schema tests
116 create table db2_loader_test9 (
117 id int generated by default as identity not null primary key
121 create table db2_loader_test10 (
122 id int generated by default as identity not null primary key,
124 foreign key (nine_id) references db2_loader_test9(id)
125 on delete set null on update restrict
129 drop => [ qw/db2_loader_test9 db2_loader_test10/ ],
134 # test on delete/update fk clause introspection
135 ok ((my $rel_info = $schema->source('Db2LoaderTest10')->relationship_info('nine')),
138 is $rel_info->{attrs}{on_delete}, 'SET NULL',
139 'ON DELETE clause introspected correctly';
141 is $rel_info->{attrs}{on_update}, 'RESTRICT',
142 'ON UPDATE clause introspected correctly';
144 is $rel_info->{attrs}{is_deferrable}, 1,
145 'DEFERRABLE defaults to 1';
148 my $dbh = $schema->storage->dbh;
151 $dbh->do('CREATE SCHEMA "dbicsl-test"');
154 $schemas_created = 0;
155 skip "no CREATE SCHEMA privileges", 30 * 2;
159 CREATE TABLE "dbicsl-test".db2_loader_test4 (
160 id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY,
165 CREATE TABLE "dbicsl-test".db2_loader_test5 (
166 id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY,
168 four_id INTEGER NOT NULL,
169 CONSTRAINT loader_test5_uniq UNIQUE (four_id),
170 FOREIGN KEY (four_id) REFERENCES "dbicsl-test".db2_loader_test4 (id)
173 $dbh->do('CREATE SCHEMA "dbicsl.test"');
175 CREATE TABLE "dbicsl.test".db2_loader_test5 (
176 pk INT GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY,
178 four_id INTEGER NOT NULL,
179 CONSTRAINT loader_test5_uniq UNIQUE (four_id),
180 FOREIGN KEY (four_id) REFERENCES "dbicsl-test".db2_loader_test4 (id)
184 CREATE TABLE "dbicsl.test".db2_loader_test6 (
185 id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY,
187 db2_loader_test4_id INTEGER,
188 FOREIGN KEY (db2_loader_test4_id) REFERENCES "dbicsl-test".db2_loader_test4 (id)
192 CREATE TABLE "dbicsl.test".db2_loader_test7 (
193 id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY,
195 six_id INTEGER NOT NULL UNIQUE,
196 FOREIGN KEY (six_id) REFERENCES "dbicsl.test".db2_loader_test6 (id)
200 CREATE TABLE "dbicsl-test".db2_loader_test8 (
201 id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY,
203 db2_loader_test7_id INTEGER,
204 FOREIGN KEY (db2_loader_test7_id) REFERENCES "dbicsl.test".db2_loader_test7 (id)
208 $schemas_created = 1;
210 foreach my $db_schema (['dbicsl-test', 'dbicsl.test'], '%') {
212 rmtree EXTRA_DUMP_DIR;
215 local $SIG{__WARN__} = sub {
216 push @warns, $_[0] unless $_[0] =~ /\bcollides\b/;
223 db_schema => $db_schema,
224 dump_directory => EXTRA_DUMP_DIR,
227 [ $dsn, $user, $password ],
230 diag join "\n", @warns if @warns;
233 } 'dumped schema for "dbicsl-test" and "dbicsl.test" schemas with no warnings';
235 my ($test_schema, $rsrc, $rs, $row, %uniqs, $rel_info);
238 ok $test_schema = DB2MultiSchema->connect($dsn, $user, $password);
239 } 'connected test schema';
242 ok $rsrc = $test_schema->source('Db2LoaderTest4');
243 } 'got source for table in schema name with dash';
245 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
246 'column in schema name with dash';
248 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
249 'column in schema name with dash';
251 is try { $rsrc->column_info('value')->{size} }, 100,
252 'column in schema name with dash';
255 ok $rs = $test_schema->resultset('Db2LoaderTest4');
256 } 'got resultset for table in schema name with dash';
259 ok $row = $rs->create({ value => 'foo' });
260 } 'executed SQL on table in schema name with dash';
262 $rel_info = try { $rsrc->relationship_info('dbicsl_dash_test_db2_loader_test5') };
264 is_deeply $rel_info->{cond}, {
265 'foreign.four_id' => 'self.id'
266 }, 'relationship in schema name with dash';
268 is $rel_info->{attrs}{accessor}, 'single',
269 'relationship in schema name with dash';
271 is $rel_info->{attrs}{join_type}, 'LEFT',
272 'relationship in schema name with dash';
275 ok $rsrc = $test_schema->source('DbicslDashTestDb2LoaderTest5');
276 } 'got source for table in schema name with dash';
278 %uniqs = try { $rsrc->unique_constraints };
281 'got unique and primary constraint in schema name with dash';
283 delete $uniqs{primary};
285 is_deeply ((values %uniqs)[0], ['four_id'],
286 'correct unique constraint in schema name with dash');
289 ok $rsrc = $test_schema->source('Db2LoaderTest6');
290 } 'got source for table in schema name with dot';
292 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
293 'column in schema name with dot introspected correctly';
295 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
296 'column in schema name with dot introspected correctly';
298 is try { $rsrc->column_info('value')->{size} }, 100,
299 'column in schema name with dot introspected correctly';
302 ok $rs = $test_schema->resultset('Db2LoaderTest6');
303 } 'got resultset for table in schema name with dot';
306 ok $row = $rs->create({ value => 'foo' });
307 } 'executed SQL on table in schema name with dot';
309 $rel_info = try { $rsrc->relationship_info('db2_loader_test7') };
311 is_deeply $rel_info->{cond}, {
312 'foreign.six_id' => 'self.id'
313 }, 'relationship in schema name with dot';
315 is $rel_info->{attrs}{accessor}, 'single',
316 'relationship in schema name with dot';
318 is $rel_info->{attrs}{join_type}, 'LEFT',
319 'relationship in schema name with dot';
322 ok $rsrc = $test_schema->source('Db2LoaderTest7');
323 } 'got source for table in schema name with dot';
325 %uniqs = try { $rsrc->unique_constraints };
328 'got unique and primary constraint in schema name with dot';
330 delete $uniqs{primary};
332 is_deeply ((values %uniqs)[0], ['six_id'],
333 'correct unique constraint in schema name with dot');
336 ok $test_schema->source('Db2LoaderTest6')
337 ->has_relationship('db2_loader_test4');
338 } 'cross-schema relationship in multi-db_schema';
341 ok $test_schema->source('Db2LoaderTest4')
342 ->has_relationship('db2_loader_test6s');
343 } 'cross-schema relationship in multi-db_schema';
346 ok $test_schema->source('Db2LoaderTest8')
347 ->has_relationship('db2_loader_test7');
348 } 'cross-schema relationship in multi-db_schema';
351 ok $test_schema->source('Db2LoaderTest7')
352 ->has_relationship('db2_loader_test8s');
353 } 'cross-schema relationship in multi-db_schema';
361 $tester->run_tests();
364 if (not $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}) {
365 if ($schemas_created && (my $dbh = try { $schema->storage->dbh })) {
366 foreach my $table ('"dbicsl-test".db2_loader_test8',
367 '"dbicsl.test".db2_loader_test7',
368 '"dbicsl.test".db2_loader_test6',
369 '"dbicsl-test".db2_loader_test5',
370 '"dbicsl.test".db2_loader_test5',
371 '"dbicsl-test".db2_loader_test4') {
373 $dbh->do("DROP TABLE $table");
376 diag "Error dropping table: $_";
380 foreach my $db_schema (qw/dbicsl-test dbicsl.test/) {
382 $dbh->do(qq{DROP SCHEMA "$db_schema" RESTRICT});
385 diag "Error dropping test schema $db_schema: $_";
389 rmtree EXTRA_DUMP_DIR;
392 # vim:et sts=4 sw=4 tw=0: