Support identity columns in PostgreSQL v10
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 10_09firebird_common.t
CommitLineData
4cbddf8d 1use strict;
3a89a69f 2use warnings;
3use Test::More;
4use Scope::Guard ();
4fb2971c 5use DBIx::Class::Optional::Dependencies;
ff4b0152 6use DBIx::Class::Schema::Loader::Utils qw/sigwarn_silencer/;
4cbddf8d 7use lib qw(t/lib);
8use dbixcsl_common_tests;
9
4fb2971c 10my %dsns;
11for (qw(FIREBIRD FIREBIRD_ODBC FIREBIRD_INTERBASE)) {
12 next unless $ENV{"DBICTEST_${_}_DSN"};
c4a69b87 13
4fb2971c 14 my $dep_group = lc "rdbms_$_";
15 if (!DBIx::Class::Optional::Dependencies->req_ok_for($dep_group)) {
16 diag 'You need to install ' . DBIx::Class::Optional::Dependencies->req_missing_for($dep_group)
17 . " to test with $_";
18 next;
19 }
20
21 $dsns{$_}{dsn} = $ENV{"DBICTEST_${_}_DSN"};
22 $dsns{$_}{user} = $ENV{"DBICTEST_${_}_USER"};
23 $dsns{$_}{password} = $ENV{"DBICTEST_${_}_PASS"};
24 $dsns{$_}{connect_info_opts} = { on_connect_call => 'use_softcommit' }
786d242e 25 if /\AFIREBIRD(?:_INTERBASE)?\z/;
4fb2971c 26};
3a89a69f 27
4fb2971c 28plan skip_all => 'You need to set the DBICTEST_FIREBIRD_DSN, _USER and _PASS and/or the DBICTEST_FIREBIRD_ODBC_DSN, _USER and _PASS and/or the DBICTEST_FIREBIRD_INTERBASE_DSN, _USER and _PASS environment variables'
29 unless %dsns;
3a89a69f 30
31my $schema;
4cbddf8d 32
33my $tester = dbixcsl_common_tests->new(
34 vendor => 'Firebird',
35 auto_inc_pk => 'INTEGER NOT NULL PRIMARY KEY',
36 auto_inc_cb => sub {
37 my ($table, $col) = @_;
38 return (
39 qq{ CREATE GENERATOR gen_${table}_${col} },
40 qq{
41 CREATE TRIGGER ${table}_bi FOR $table
42 ACTIVE BEFORE INSERT POSITION 0
43 AS
44 BEGIN
45 IF (NEW.$col IS NULL) THEN
46 NEW.$col = GEN_ID(gen_${table}_${col},1);
47 END
48 }
49 );
50 },
51 auto_inc_drop_cb => sub {
52 my ($table, $col) = @_;
53 return (
54 qq{ DROP TRIGGER ${table}_bi },
55 qq{ DROP GENERATOR gen_${table}_${col} },
56 );
57 },
58 null => '',
b511f36e 59 preserve_case_mode_is_exclusive => 1,
60 quote_char => '"',
7336f5dc 61 connect_info => [ map { $dsns{$_} } sort keys %dsns ],
cf0ba25b 62 data_types => {
63 # based on the Interbase Data Definition Guide
64 # http://www.ibphoenix.com/downloads/60DataDef.zip
65 #
66 # Numeric types
67 'smallint' => { data_type => 'smallint' },
68 'int' => { data_type => 'integer' },
69 'integer' => { data_type => 'integer' },
70 'bigint' => { data_type => 'bigint' },
8ec0dd69 71 'float' => { data_type => 'real' },
cf0ba25b 72 'double precision' =>
73 { data_type => 'double precision' },
8ec0dd69 74 'real' => { data_type => 'real' },
cf0ba25b 75
8ec0dd69 76 'float(2)' => { data_type => 'real' },
77 'float(7)' => { data_type => 'real' },
cf0ba25b 78 'float(8)' => { data_type => 'double precision' },
79
80 'decimal' => { data_type => 'decimal' },
81 'dec' => { data_type => 'decimal' },
82 'numeric' => { data_type => 'numeric' },
83
84 'decimal(3)' => { data_type => 'decimal', size => [3,0] },
85
86 'decimal(3,3)' => { data_type => 'decimal', size => [3,3] },
87 'dec(3,3)' => { data_type => 'decimal', size => [3,3] },
88 'numeric(3,3)' => { data_type => 'numeric', size => [3,3] },
89
0ae64d34 90 'decimal(6,3)' => { data_type => 'decimal', size => [6,3] },
91 'numeric(6,3)' => { data_type => 'numeric', size => [6,3] },
92
93 'decimal(12,3)' => { data_type => 'decimal', size => [12,3] },
94 'numeric(12,3)' => { data_type => 'numeric', size => [12,3] },
95
cf0ba25b 96 'decimal(18,18)' => { data_type => 'decimal', size => [18,18] },
97 'dec(18,18)' => { data_type => 'decimal', size => [18,18] },
98 'numeric(18,18)' => { data_type => 'numeric', size => [18,18] },
99
100 # Date and Time Types
101 'date' => { data_type => 'date' },
6e566cc4 102 'timestamp default current_timestamp'
103 => { data_type => 'timestamp', default_value => \'current_timestamp' },
cf0ba25b 104 'time' => { data_type => 'time' },
105
106 # String Types
107 'char' => { data_type => 'char', size => 1 },
108 'char(11)' => { data_type => 'char', size => 11 },
109 'varchar(20)' => { data_type => 'varchar', size => 20 },
5111e5d0 110 'char(22) character set unicode_fss' =>
111 => { data_type => 'char(x) character set unicode_fss', size => 22 },
112 'varchar(33) character set unicode_fss' =>
113 => { data_type => 'varchar(x) character set unicode_fss', size => 33 },
114
cf0ba25b 115 # Blob types
116 'blob' => { data_type => 'blob' },
117 'blob sub_type text'
118 => { data_type => 'blob sub_type text' },
5111e5d0 119 'blob sub_type text character set unicode_fss'
120 => { data_type => 'blob sub_type text character set unicode_fss' },
cf0ba25b 121 },
3a89a69f 122 extra => {
d7e0e0e8 123 count => 11,
f7c4e9ae 124 create => [
125 q{
126 CREATE TABLE "Firebird_Loader_Test1" (
127 "Id" INTEGER NOT NULL PRIMARY KEY,
128 "Foo" INTEGER DEFAULT 42
129 )
130 },
131 q{
132 CREATE GENERATOR "Gen_Firebird_Loader_Test1_Id"
133 },
134 q{
135 CREATE TRIGGER "Firebird_Loader_Test1_BI" for "Firebird_Loader_Test1"
136 ACTIVE BEFORE INSERT POSITION 0
137 AS
138 BEGIN
139 IF (NEW."Id" IS NULL) THEN
140 NEW."Id" = GEN_ID("Gen_Firebird_Loader_Test1_Id",1);
141 END
142 },
d7e0e0e8 143 q{
144 CREATE VIEW firebird_loader_test2 AS SELECT * FROM "Firebird_Loader_Test1"
145 },
f7c4e9ae 146 ],
147 pre_drop_ddl => [
d7e0e0e8 148 'DROP VIEW firebird_loader_test2',
f7c4e9ae 149 'DROP TRIGGER "Firebird_Loader_Test1_BI"',
150 'DROP GENERATOR "Gen_Firebird_Loader_Test1_Id"',
151 'DROP TABLE "Firebird_Loader_Test1"',
152 ],
3a89a69f 153 run => sub {
154 $schema = shift;
1ad8e8c3 155 my ($monikers, $classes, $self) = @_;
3a89a69f 156
3a89a69f 157 my $dbh = $schema->storage->dbh;
158
159# create a mixed case table
160 $dbh->do($_) for (
3a89a69f 161 );
162
c4a69b87 163 local $schema->loader->{preserve_case} = 1;
164 $schema->loader->_setup;
ec957051 165
1ad8e8c3 166 $self->rescan_without_warnings($schema);
3a89a69f 167
168 ok ((my $rsrc = eval { $schema->resultset('FirebirdLoaderTest1')->result_source }),
169 'got rsrc for mixed case table');
170
171 ok ((my $col_info = eval { $rsrc->column_info('Id') }),
172 'got column_info for column Id');
173
174 is $col_info->{accessor}, 'id', 'column Id has lowercase accessor "id"';
175
176 is $col_info->{is_auto_increment}, 1, 'is_auto_increment detected for mixed case trigger';
177
178 is $col_info->{sequence}, 'Gen_Firebird_Loader_Test1_Id', 'correct mixed case sequence name';
179
180 is eval { $rsrc->column_info('Foo')->{default_value} }, 42, 'default_value detected for mixed case column';
5111e5d0 181
d7e0e0e8 182 # test that views are marked as such
183 my $view_source = $schema->resultset($monikers->{firebird_loader_test2})->result_source;
184 isa_ok $view_source, 'DBIx::Class::ResultSource::View',
185 'view result source';
186
187 like $view_source->view_definition,
188 qr/\A \s* select\b .* \bfrom \s+ (?-i:"Firebird_Loader_Test1") \s* \z/imsx,
189 'view definition';
190
5111e5d0 191 # test the fixed up ->_dbh_type_info_type_name for the ODBC driver
192 if ($schema->storage->_dbi_connect_info->[0] =~ /:ODBC:/i) {
193 my %truncated_types = (
194 4 => 'INTEGER',
195 -9 => 'VARCHAR(x) CHARACTER SET UNICODE_FSS',
196 -10 => 'BLOB SUB_TYPE TEXT CHARACTER SET UNICODE_FSS',
197 );
198
199 for my $type_num (keys %truncated_types) {
c4a69b87 200 is $schema->loader->_dbh_type_info_type_name($type_num),
5111e5d0 201 $truncated_types{$type_num},
202 "ODBC ->_dbh_type_info_type_name correct for '$truncated_types{$type_num}'";
203 }
204 }
205 else {
206 my $tb = Test::More->builder;
207 $tb->skip('not testing _dbh_type_info_type_name on DBD::InterBase') for 1..3;
208 }
3a89a69f 209 },
210 },
4cbddf8d 211);
212
4fb2971c 213{
4145a6f3 214 # get rid of stupid warning from InterBase/GetInfo.pm
4fb2971c 215 if ($dsns{FIREBIRD_INTERBASE}) {
ff4b0152 216 local $SIG{__WARN__} = sigwarn_silencer(
071c2f8d 217 qr{^(?:Use of uninitialized value|Argument "[0-9_]+" isn't numeric|Missing argument) in sprintf at \S+DBD/InterBase/GetInfo.pm line \d+\.$}
ff4b0152 218 );
4145a6f3 219 require DBD::InterBase;
220 require DBD::InterBase::GetInfo;
221 }
c4a69b87 222
4cbddf8d 223 $tester->run_tests();
224}
3a89a69f 225
e32d24a5 226# vim:et sts=4 sw=4 tw=0: