DBIx/Class/Schema/Versioned.pm

filter: Statementstmt 31.1% 65 / 209 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 44.7% 17 / 38 Podpod 100% 12 / 12 totaltotal 26.4% 94 / 356 CC 41 SCAR 36.4
CC
41
Cov
22%
upgrade
132.0
_on_connect
90.0
run_upgrade
30.0
CRAP 38.1
« DBIx/Class/Storage/DBI/SQLAnywhere.pm ↑ Summary DBIx/Class/Storage/DBI/SQLite.pm »
1 package # Hide from PAUSE
2 DBIx::Class::Version::Table;
3 1 ▶use base 'DBIx::Class::Core';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 __PACKAGE__->table('dbix_class_schema_versions');
8  
9 1 __PACKAGE__->add_columns
10 ( 'version' => {
11 'data_type' => 'VARCHAR',
12 'is_auto_increment' => 0,
13 'default_value' => undef,
14 'is_foreign_key' => 0,
15 'name' => 'version',
16 'is_nullable' => 0,
17 'size' => '10'
18 },
19 'installed' => {
20 'data_type' => 'VARCHAR',
21 'is_auto_increment' => 0,
22 'default_value' => undef,
23 'is_foreign_key' => 0,
24 'name' => 'installed',
25 'is_nullable' => 0,
26 'size' => '20'
27 },
28 );
29 1 __PACKAGE__->set_primary_key('version');
30  
31 package # Hide from PAUSE
32 DBIx::Class::Version::TableCompat;
33 1 ▶use base 'DBIx::Class::Core';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 __PACKAGE__->table('SchemaVersions');
35  
36 1 __PACKAGE__->add_columns
37 ( 'Version' => {
38 'data_type' => 'VARCHAR',
39 },
40 'Installed' => {
41 'data_type' => 'VARCHAR',
42 },
43 );
44 1 __PACKAGE__->set_primary_key('Version');
45  
46 package # Hide from PAUSE
47 DBIx::Class::Version;
48 1 ▶use base 'DBIx::Class::Schema';
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 1 __PACKAGE__->register_class('Table', 'DBIx::Class::Version::Table');
53  
54 package # Hide from PAUSE
55 DBIx::Class::VersionCompat;
56 1 ▶use base 'DBIx::Class::Schema';
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 1 __PACKAGE__->register_class('TableCompat', 'DBIx::Class::Version::TableCompat');
61  
62  
63 # ---------------------------------------------------------------------------
64  
65 =head1 NAME
66  
67 DBIx::Class::Schema::Versioned - DBIx::Class::Schema plugin for Schema upgrades
68  
69 =head1 SYNOPSIS
70  
71 package MyApp::Schema;
72 use base qw/DBIx::Class::Schema/;
73  
74 our $VERSION = 0.001;
75  
76 # load MyApp::Schema::CD, MyApp::Schema::Book, MyApp::Schema::DVD
77 __PACKAGE__->load_classes(qw/CD Book DVD/);
78  
79 __PACKAGE__->load_components(qw/Schema::Versioned/);
80 __PACKAGE__->upgrade_directory('/path/to/upgrades/');
81  
82  
83 =head1 DESCRIPTION
84  
85 This module provides methods to apply DDL changes to your database using SQL
86 diff files. Normally these diff files would be created using
87 L<DBIx::Class::Schema/create_ddl_dir>.
88  
89 A table called I<dbix_class_schema_versions> is created and maintained by the
90 module. This is used to determine which version your database is currently at.
91 Similarly the $VERSION in your DBIC schema class is used to determine the
92 current DBIC schema version.
93  
94 The upgrade is initiated manually by calling C<upgrade> on your schema object,
95 this will attempt to upgrade the database from its current version to the current
96 schema version using a diff from your I<upgrade_directory>. If a suitable diff is
97 not found then no upgrade is possible.
98  
99 =head1 SEE ALSO
100  
101 L<DBIx::Class::DeploymentHandler> is a much more powerful alternative to this
102 module. Examples of things it can do that this module cannot do include
103  
104 =over
105  
106 =item *
107  
108 Downgrades in addition to upgrades
109  
110 =item *
111  
112 Multiple sql files per upgrade/downgrade/install
113  
114 =item *
115  
116 Perl scripts allowed for upgrade/downgrade/install
117  
118 =item *
119  
120 Just one set of files needed for upgrade, unlike this module where one might
121 need to generate C<factorial(scalar @versions)>
122  
123 =back
124  
125 =head1 GETTING STARTED
126  
127 Firstly you need to setup your schema class as per the L</SYNOPSIS>, make sure
128 you have specified an upgrade_directory and an initial $VERSION.
129  
130 Then you'll need two scripts, one to create DDL files and diffs and another to perform
131 upgrades. Your creation script might look like a bit like this:
132  
133 use strict;
134 use Pod::Usage;
135 use Getopt::Long;
136 use MyApp::Schema;
137  
138 my ( $preversion, $help );
139 GetOptions(
140 'p|preversion:s' => \$preversion,
141 ) or die pod2usage;
142  
143 my $schema = MyApp::Schema->connect(
144 $dsn,
145 $user,
146 $password,
147 );
148 my $sql_dir = './sql';
149 my $version = $schema->schema_version();
150 $schema->create_ddl_dir( 'MySQL', $version, $sql_dir, $preversion );
151  
152 Then your upgrade script might look like so:
153  
154 use strict;
155 use MyApp::Schema;
156  
157 my $schema = MyApp::Schema->connect(
158 $dsn,
159 $user,
160 $password,
161 );
162  
163 if (!$schema->get_db_version()) {
164 # schema is unversioned
165 $schema->deploy();
166 } else {
167 $schema->upgrade();
168 }
169  
170 The script above assumes that if the database is unversioned then it is empty
171 and we can safely deploy the DDL to it. However things are not always so simple.
172  
173 if you want to initialise a pre-existing database where the DDL is not the same
174 as the DDL for your current schema version then you will need a diff which
175 converts the database's DDL to the current DDL. The best way to do this is
176 to get a dump of the database schema (without data) and save that in your
177 SQL directory as version 0.000 (the filename must be as with
178 L<DBIx::Class::Schema/ddl_filename>) then create a diff using your create DDL
179 script given above from version 0.000 to the current version. Then hand check
180 and if necessary edit the resulting diff to ensure that it will apply. Once you have
181 done all that you can do this:
182  
183 if (!$schema->get_db_version()) {
184 # schema is unversioned
185 $schema->install("0.000");
186 }
187  
188 # this will now apply the 0.000 to current version diff
189 $schema->upgrade();
190  
191 In the case of an unversioned database the above code will create the
192 dbix_class_schema_versions table and write version 0.000 to it, then
193 upgrade will then apply the diff we talked about creating in the previous paragraph
194 and then you're good to go.
195  
196 =cut
197  
198 package DBIx::Class::Schema::Versioned;
199  
200 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
201 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
202 1 ▶use base 'DBIx::Class::Schema';
Subroutine BEGIN: called CC=1 SCAR=0.0
203  
204 1 ▶use DBIx::Class::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
205 1 ▶use Time::HiRes qw/gettimeofday/;
Subroutine BEGIN: called CC=1 SCAR=0.0
206 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
207 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
208  
209 1 __PACKAGE__->mk_classdata('_filedata');
210 1 __PACKAGE__->mk_classdata('upgrade_directory');
211 1 __PACKAGE__->mk_classdata('backup_directory');
212 1 __PACKAGE__->mk_classdata('do_backup');
213 1 __PACKAGE__->mk_classdata('do_diff_on_init');
214  
215  
216 =head1 METHODS
217  
218 =head2 upgrade_directory
219  
220 Use this to set the directory your upgrade files are stored in.
221  
222 =head2 backup_directory
223  
224 Use this to set the directory you want your backups stored in (note that backups
225 are disabled by default).
226  
227 =cut
228  
229 =head2 install
230  
231 =over 4
232  
233 =item Arguments: $db_version
234  
235 =back
236  
237 Call this to initialise a previously unversioned database. The table 'dbix_class_schema_versions' will be created which will be used to store the database version.
238  
239 Takes one argument which should be the version that the database is currently at. Defaults to the return value of L</schema_version>.
240  
241 See L</getting_started> for more details.
242  
243 =cut
244  
245 sub install
246 {
247 0 ▶ my ($self, $new_version) = @_;
Subroutine install: not called CC=4 SCAR=30.0
Pod documented
248  
249 # must be called on a fresh database
250 0 ▶ if ($self->get_db_version()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_db_version) 0 0 0
251 0 $self->throw_exception("A versioned schema has already been deployed, try upgrade instead.\n");
252 }
253  
254 # default to current version if none passed
255 0 ▶ $new_version ||= $self->schema_version();
Condition0%0/3
$new_version ||= $self->schema_version
l!l&&r!l&&!r
000
MC/DC0%0/2
$new_version ||= $self->schema_version
$new_version $self->schema_version
Truth table0%0/3
A ||= B
A= $new_versionB= $self->schema_version
ABresult
1X1
011
000
256  
257 0 ▶ if ($new_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_version) 0 0 0
258 # create versions table and version row
259 0 $self->{vschema}->deploy;
260 0 $self->_set_db_version({ version => $new_version });
261 }
262 }
263  
264 =head2 deploy
265  
266 Same as L<DBIx::Class::Schema/deploy> but also calls C<install>.
267  
268 =cut
269  
270 sub deploy {
271 0 ▶ my $self = shift;
Subroutine deploy: not called CC=1 SCAR=6.9
Pod documented
272 0 $self->next::method(@_);
273 0 $self->install();
274 }
275  
276 =head2 create_upgrade_path
277  
278 =over 4
279  
280 =item Arguments: { upgrade_file => $file }
281  
282 =back
283  
284 Virtual method that should be overridden to create an upgrade file.
285 This is useful in the case of upgrading across multiple versions
286 to concatenate several files to create one upgrade file.
287  
288 You'll probably want the db_version retrieved via $self->get_db_version
289 and the schema_version which is retrieved via $self->schema_version
290  
291 =cut
292  
293 ▶sub create_upgrade_path {
Subroutine create_upgrade_path: not called CC=1 SCAR=0.0
Pod documented
294 ## override this method
295 }
296  
297 =head2 ordered_schema_versions
298  
299 =over 4
300  
301 =item Return Value: a list of version numbers, ordered from lowest to highest
302  
303 =back
304  
305 Virtual method that should be overridden to return an ordered list
306 of schema versions. This is then used to produce a set of steps to
307 upgrade through to achieve the required schema version.
308  
309 You may want the db_version retrieved via $self->get_db_version
310 and the schema_version which is retrieved via $self->schema_version
311  
312 =cut
313  
314 ▶sub ordered_schema_versions {
Subroutine ordered_schema_versions: not called CC=1 SCAR=0.0
Pod documented
315 ## override this method
316 }
317  
318 =head2 upgrade
319  
320 Call this to attempt to upgrade your database from the version it
321 is at to the version this DBIC schema is at. If they are the same
322 it does nothing.
323  
324 It will call L</ordered_schema_versions> to retrieve an ordered
325 list of schema versions (if ordered_schema_versions returns nothing
326 then it is assumed you can do the upgrade as a single step). It
327 then iterates through the list of versions between the current db
328 version and the schema version applying one update at a time until
329 all relevant updates are applied.
330  
331 The individual update steps are performed by using
332 L</upgrade_single_step>, which will apply the update and also
333 update the dbix_class_schema_versions table.
334  
335 =cut
336  
337 sub upgrade {
338 0 ▶ my ($self) = @_;
Subroutine upgrade: not called CC=11 SCAR=48.8
Pod documented
339 0 my $db_version = $self->get_db_version();
340  
341 # db unversioned
342 0 ▶ unless ($db_version) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($db_version) 0 0 0
343 0 carp 'Upgrade not possible as database is unversioned. Please call install first.';
344 0 return;
345 }
346  
347 # db and schema at same version. do nothing
348 0 ▶ if ( $db_version eq $self->schema_version ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($db_version eq $self->schema_version) 0 0 0
349 0 carp 'Upgrade not necessary';
350 0 return;
351 }
352  
353 0 my @version_list = $self->ordered_schema_versions;
354  
355 # if nothing returned then we preload with min/max
356 0 ▶ @version_list = ( $db_version, $self->schema_version )
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @version_list 0 0 0
357 unless ( scalar(@version_list) );
358  
359 # catch the case of someone returning an arrayref
360 0 ▶ @version_list = @{ $version_list[0] }
Branch: 1 branch
BranchTrueFalseTotal
if ref $version_list[0] eq "ARRAY" 0 0 0
361 if ( ref( $version_list[0] ) eq 'ARRAY' );
362  
363 # remove all versions in list above the required version
364 0 ▶ while ( scalar(@version_list)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @version_list and $version_list[-1] ne $self->schema_version) 0 0 0
Condition0%0/3
scalar @version_list and $version_list[-1] ne $self->schema_version
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @version_list and $version_list[-1] ne $self->schema_version
scalar @version_list $version_list[-1] ne $self->schema_version
Truth table0%0/3
A and B
A= scalar @version_listB= $version_list[-1] ne $self->schema_version
ABresult
0X0
100
111
365 && ( $version_list[-1] ne $self->schema_version ) )
366 {
367 0 pop @version_list;
368 }
369  
370 # remove all versions in list below the current version
371 0 ▶ while ( scalar(@version_list) && ( $version_list[0] ne $db_version ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @version_list and $version_list[0] ne $db_version) 0 0 0
Condition0%0/3
scalar @version_list and $version_list[0] ne $db_version
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @version_list and $version_list[0] ne $db_version
scalar @version_list $version_list[0] ne $db_version
Truth table0%0/3
A and B
A= scalar @version_listB= $version_list[0] ne $db_version
ABresult
0X0
100
111
372 0 shift @version_list;
373 }
374  
375 # check we have an appropriate list of versions
376 0 ▶ if ( scalar(@version_list) < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @version_list < 2) 0 0 0
377 0 die;
378 }
379  
380 # do sets of upgrade
381 0 ▶ while ( scalar(@version_list) >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @version_list >= 2) 0 0 0
382 0 $self->upgrade_single_step( $version_list[0], $version_list[1] );
383 0 shift @version_list;
384 }
385 }
386  
387 =head2 upgrade_single_step
388  
389 =over 4
390  
391 =item Arguments: db_version - the version currently within the db
392  
393 =item Arguments: target_version - the version to upgrade to
394  
395 =back
396  
397 Call this to attempt to upgrade your database from the
398 I<db_version> to the I<target_version>. If they are the same it
399 does nothing.
400  
401 It requires an SQL diff file to exist in your I<upgrade_directory>,
402 normally you will have created this using L<DBIx::Class::Schema/create_ddl_dir>.
403  
404 If successful the dbix_class_schema_versions table is updated with
405 the I<target_version>.
406  
407 This method may be called repeatedly by the upgrade method to
408 upgrade through a series of updates.
409  
410 =cut
411  
412 sub upgrade_single_step
413 {
414 0 ▶ my ($self,
Subroutine upgrade_single_step: not called CC=4 SCAR=30.0
Pod documented
415 $db_version,
416 $target_version) = @_;
417  
418 # db and schema at same version. do nothing
419 0 ▶ if ($db_version eq $target_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($db_version eq $target_version) 0 0 0
420 0 carp 'Upgrade not necessary';
421 0 return;
422 }
423  
424 # strangely the first time this is called can
425 # differ to subsequent times. so we call it
426 # here to be sure.
427 # XXX - just fix it
428 0 $self->storage->sqlt_type;
429  
430 0 my $upgrade_file = $self->ddl_filename(
431 $self->storage->sqlt_type,
432 $target_version,
433 $self->upgrade_directory,
434 $db_version,
435 );
436  
437 0 $self->create_upgrade_path({ upgrade_file => $upgrade_file });
438  
439 0 ▶ unless (-f $upgrade_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $upgrade_file) 0 0 0
440 0 carp "Upgrade not possible, no upgrade file found ($upgrade_file), please create one";
441 0 return;
442 }
443  
444 0 carp "DB version ($db_version) is lower than the schema version (".$self->schema_version."). Attempting upgrade.\n";
445  
446 # backup if necessary then apply upgrade
447 0 $self->_filedata($self->_read_sql_file($upgrade_file));
448 0 ▶ $self->backup() if($self->do_backup);
Branch: 1 branch
BranchTrueFalseTotal
if $self->do_backup 0 0 0
449 0 ▶ $self->txn_do(sub { $self->do_upgrade() });
Subroutine __ANON__: not called CC=1 SCAR=6.9
450  
451 # set row in dbix_class_schema_versions table
452 0 $self->_set_db_version({version => $target_version});
453 }
454  
455 =head2 do_upgrade
456  
457 This is an overwritable method used to run your upgrade. The freeform method
458 allows you to run your upgrade any way you please, you can call C<run_upgrade>
459 any number of times to run the actual SQL commands, and in between you can
460 sandwich your data upgrading. For example, first run all the B<CREATE>
461 commands, then migrate your data from old to new tables/formats, then
462 issue the DROP commands when you are finished. Will run the whole file as it is by default.
463  
464 =cut
465  
466 sub do_upgrade
467 {
468 0 ▶ my ($self) = @_;
Subroutine do_upgrade: not called CC=1 SCAR=6.9
Pod documented
469  
470 # just run all the commands (including inserts) in order
471 0 $self->run_upgrade(qr/.*?/);
472 }
473  
474 =head2 run_upgrade
475  
476 $self->run_upgrade(qr/create/i);
477  
478 Runs a set of SQL statements matching a passed in regular expression. The
479 idea is that this method can be called any number of times from your
480 C<do_upgrade> method, running whichever commands you specify via the
481 regex in the parameter. Probably won't work unless called from the overridable
482 do_upgrade method.
483  
484 =cut
485  
486 sub run_upgrade
487 {
488 0 ▶ my ($self, $stm) = @_;
Subroutine run_upgrade: not called CC=5 SCAR=34.0
Pod documented
489  
490 0 ▶ return unless ($self->_filedata);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_filedata 0 0 0
491 0 my @statements = grep { $_ =~ $stm } @{$self->_filedata};
492 0 $self->_filedata([ grep { $_ !~ /$stm/i } @{$self->_filedata} ]);
493  
494 0 for (@statements)
495 {
496 0 ▶ $self->storage->debugobj->query_start($_) if $self->storage->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->storage->debug 0 0 0
497 0 $self->apply_statement($_);
498 0 ▶ $self->storage->debugobj->query_end($_) if $self->storage->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->storage->debug 0 0 0
499 }
500  
501 0 return 1;
502 }
503  
504 =head2 apply_statement
505  
506 Takes an SQL statement and runs it. Override this if you want to handle errors
507 differently.
508  
509 =cut
510  
511 sub apply_statement {
512 0 ▶ my ($self, $statement) = @_;
Subroutine apply_statement: not called CC=2 SCAR=17.9
Pod documented
513  
514 0 ▶ $self->storage->dbh->do($_) or carp "SQL was: $_";
Branch: 1 branch
BranchTrueFalseTotal
$self->storage->dbh->do($_) or carp("SQL was: $_") 0 0 0
515 }
516  
517 =head2 get_db_version
518  
519 Returns the version that your database is currently at. This is determined by the values in the
520 dbix_class_schema_versions table that C<upgrade> and C<install> write to.
521  
522 =cut
523  
524 sub get_db_version
525 {
526 0 ▶ my ($self, $rs) = @_;
Subroutine get_db_version: not called CC=2 SCAR=17.9
Pod documented
527  
528 0 my $vtable = $self->{vschema}->resultset('Table');
529 my $version = try {
530 0 ▶ $vtable->search({}, { order_by => { -desc => 'installed' }, rows => 1 } )
Subroutine __ANON__: not called CC=1 SCAR=6.9
531 ->get_column ('version')
532 ->next;
533 0 };
534 0 ▶ return $version || 0;
Condition0%0/2
$version || 0
l!l
00
MC/DC0%0/1
$version || 0
$version
Truth table0%0/2
A
A= $version
Aresult
11
00
535 }
536  
537 =head2 schema_version
538  
539 Returns the current schema class' $VERSION
540  
541 =cut
542  
543 =head2 backup
544  
545 This is an overwritable method which is called just before the upgrade, to
546 allow you to make a backup of the database. Per default this method attempts
547 to call C<< $self->storage->backup >>, to run the standard backup on each
548 database type.
549  
550 This method should return the name of the backup file, if appropriate..
551  
552 This method is disabled by default. Set $schema->do_backup(1) to enable it.
553  
554 =cut
555  
556 sub backup
557 {
558 0 ▶ my ($self) = @_;
Subroutine backup: not called CC=1 SCAR=6.9
Pod documented
559 ## Make each ::DBI::Foo do this
560 0 $self->storage->backup($self->backup_directory());
561 }
562  
563 =head2 connection
564  
565 Overloaded method. This checks the DBIC schema version against the DB version and
566 warns if they are not the same or if the DB is unversioned. It also provides
567 compatibility between the old versions table (SchemaVersions) and the new one
568 (dbix_class_schema_versions).
569  
570 To avoid the checks on connect, set the environment var DBIC_NO_VERSION_CHECK or alternatively you can set the ignore_version attr in the forth argument like so:
571  
572 my $schema = MyApp::Schema->connect(
573 $dsn,
574 $user,
575 $password,
576 { ignore_version => 1 },
577 );
578  
579 =cut
580  
581 sub connection {
582 0 ▶ my $self = shift;
Subroutine connection: not called CC=1 SCAR=6.9
Pod documented
583 0 $self->next::method(@_);
584 0 $self->_on_connect();
585 0 return $self;
586 }
587  
588 sub _on_connect
589 {
590 0 ▶ my ($self) = @_;
Subroutine _on_connect: not called CC=9 SCAR=45.0
591  
592 0 my $conn_info = $self->storage->connect_info;
593 0 $self->{vschema} = DBIx::Class::Version->connect(@$conn_info);
594 0 ▶ my $conn_attrs = $self->{vschema}->storage->_dbic_connect_attributes || {};
Condition0%0/2
$self->{'vschema'}->storage->_dbic_connect_attributes || {}
l!l
00
MC/DC0%0/1
$self->{'vschema'}->storage->_dbic_connect_attributes || {}
$self->{'vschema'}->storage->_dbic_connect_attributes
Truth table0%0/2
A
A= $self->{'vschema'}->storage->_dbic_connect_attributes
Aresult
11
00
595  
596 0 my $vtable = $self->{vschema}->resultset('Table');
597  
598 # useful when connecting from scripts etc
599 0 ▶ return if ($conn_attrs->{ignore_version} || ($ENV{DBIC_NO_VERSION_CHECK} && !exists $conn_attrs->{ignore_version}));
Branch: 1 branch
BranchTrueFalseTotal
if $conn_attrs->{"ignore_version"} or $ENV{"DBIC_NO_VERSION_CHECK"} and not exists $conn_attrs->{"ignore_version"} 0 0 0
Condition0%0/6
$conn_attrs->{"ignore_version"} or $ENV{"DBIC_NO_VERSION_CHECK"} and not exists $conn_attrs->{"ignore_version"}
l!l&&r!l&&!r
000
$ENV{"DBIC_NO_VERSION_CHECK"} and not exists $conn_attrs->{"ignore_version"}
!ll&&!rl&&r
000
MC/DC0%0/3
$conn_attrs->{"ignore_version"} or $ENV{"DBIC_NO_VERSION_CHECK"} and not exists $conn_attrs->{"ignore_version"}
$conn_attrs->{"ignore_version"} $ENV{"DBIC_NO_VERSION_CHECK"} not exists $conn_attrs->{"ignore_version"}
Truth table0%0/4
A or B and not(C)
A= $conn_attrs->{"ignore_version"}B= $ENV{"DBIC_NO_VERSION_CHECK"}C= not exists $conn_attrs->{"ignore_version"}
ABCresult
1XX1
0111
00X0
0100
600  
601 # check for legacy versions table and move to new if exists
602 0 ▶ unless ($self->_source_exists($vtable)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_source_exists($vtable)) 0 0 0
603 0 my $vtable_compat = DBIx::Class::VersionCompat->connect(@$conn_info)->resultset('TableCompat');
604 0 ▶ if ($self->_source_exists($vtable_compat)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_source_exists($vtable_compat)) 0 0 0
605 0 $self->{vschema}->deploy;
606 0 map { $vtable->create({ installed => $_->Installed, version => $_->Version }) } $vtable_compat->all;
607 0 $self->storage->_get_dbh->do("DROP TABLE " . $vtable_compat->result_source->from);
608 }
609 }
610  
611 0 my $pversion = $self->get_db_version();
612  
613 0 ▶ if($pversion eq $self->schema_version)
Branch: 1 branch
BranchTrueFalseTotal
if ($pversion eq $self->schema_version) 0 0 0
614 {
615 #carp "This version is already installed";
616 0 return 1;
617 }
618  
619 0 ▶ if(!$pversion)
Branch: 1 branch
BranchTrueFalseTotal
unless ($pversion) 0 0 0
620 {
621 0 carp "Your DB is currently unversioned. Please call upgrade on your schema to sync the DB.";
622 0 return 1;
623 }
624  
625 0 carp "Versions out of sync. This is " . $self->schema_version .
626 ", your database contains version $pversion, please call upgrade on your Schema.";
627 }
628  
629 # is this just a waste of time? if not then merge with DBI.pm
630 sub _create_db_to_schema_diff {
631 0 ▶ my $self = shift;
Subroutine _create_db_to_schema_diff: not called CC=5 SCAR=34.0
632  
633 0 my %driver_to_db_map = (
634 'mysql' => 'MySQL'
635 );
636  
637 0 my $db = $driver_to_db_map{$self->storage->dbh->{Driver}->{Name}};
638 0 ▶ unless ($db) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($db) 0 0 0
639 0 print "Sorry, this is an unsupported DB\n";
640 0 return;
641 }
642  
643 0 ▶ unless (DBIx::Class::Optional::Dependencies->req_ok_for ('deploy')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("DBIx::Class::Optional::Dependencies"->req_ok_for("deploy")) 0 0 0
644 0 $self->throw_exception("Unable to proceed without " . DBIx::Class::Optional::Dependencies->req_missing_for ('deploy') );
645 }
646  
647 0 my $db_tr = SQL::Translator->new({
648 add_drop_table => 1,
649 parser => 'DBI',
650 parser_args => { dbh => $self->storage->dbh }
651 });
652  
653 0 $db_tr->producer($db);
654 0 my $dbic_tr = SQL::Translator->new;
655 0 $dbic_tr->parser('SQL::Translator::Parser::DBIx::Class');
656 0 $dbic_tr->data($self);
657 0 $dbic_tr->producer($db);
658  
659 0 $db_tr->schema->name('db_schema');
660 0 $dbic_tr->schema->name('dbic_schema');
661  
662 # is this really necessary?
663 0 foreach my $tr ($db_tr, $dbic_tr) {
664 0 my $data = $tr->data;
665 0 $tr->parser->($tr, $$data);
666 }
667  
668 0 my $diff = SQL::Translator::Diff::schema_diff($db_tr->schema, $db,
669 $dbic_tr->schema, $db,
670 { ignore_constraint_names => 1, ignore_index_names => 1, caseopt => 1 });
671  
672 0 my $filename = $self->ddl_filename(
673 $db,
674 $self->schema_version,
675 $self->upgrade_directory,
676 'PRE',
677 );
678 0 my $file;
679 0 ▶ if(!open($file, ">$filename"))
Branch: 1 branch
BranchTrueFalseTotal
unless (open $file, ">$filename") 0 0 0
680 {
681 0 $self->throw_exception("Can't open $filename for writing ($!)");
682 0 next;
683 }
684 0 print $file $diff;
685 0 close($file);
686  
687 0 carp "WARNING: There may be differences between your DB and your DBIC schema. Please review and if necessary run the SQL in $filename to sync your DB.";
688 }
689  
690  
691 sub _set_db_version {
692 0 ▶ my $self = shift;
Subroutine _set_db_version: not called CC=3 SCAR=24.8
693 0 my ($params) = @_;
694 0 ▶ $params ||= {};
Condition0%0/2
$params ||= {}
l!l
00
MC/DC0%0/1
$params ||= {}
$params
Truth table0%0/2
A
A= $params
Aresult
11
00
695  
696 0 ▶ my $version = $params->{version} ? $params->{version} : $self->schema_version;
Branch: 1 branch
BranchTrueFalseTotal
$params->{'version'} ? : 0 0 0
697 0 my $vtable = $self->{vschema}->resultset('Table');
698  
699 ##############################################################################
700 # !!! NOTE !!!
701 ##############################################################################
702 #
703 # The travesty below replaces the old nice timestamp format of %Y-%m-%d %H:%M:%S
704 # This is necessary since there are legitimate cases when upgrades can happen
705 # back to back within the same second. This breaks things since we relay on the
706 # ability to sort by the 'installed' value. The logical choice of an autoinc
707 # is not possible, as it will break multiple legacy installations. Also it is
708 # not possible to format the string sanely, as the column is a varchar(20).
709 # The 'v' character is added to the front of the string, so that any version
710 # formatted by this new function will sort _after_ any existing 200... strings.
711 0 my @tm = gettimeofday();
712 0 my @dt = gmtime ($tm[0]);
713 0 my $o = $vtable->create({
714 version => $version,
715 installed => sprintf("v%04d%02d%02d_%02d%02d%02d.%03.0f",
716 $dt[5] + 1900,
717 $dt[4] + 1,
718 $dt[3],
719 $dt[2],
720 $dt[1],
721 $dt[0],
722 int($tm[1] / 1000), # convert to millisecs
723 ),
724 });
725 }
726  
727 sub _read_sql_file {
728 0 ▶ my $self = shift;
Subroutine _read_sql_file: not called CC=5 SCAR=34.0
729 0 ▶ my $file = shift || return;
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
730  
731 0 ▶ open my $fh, '<', $file or carp("Can't open upgrade file, $file ($!)");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or carp("Can't open upgrade file, $file ($!)") 0 0 0
732 0 my @data = split /\n/, join '', <$fh>;
733 0 close $fh;
734  
735 @data = split /;/,
736 join '',
737 0 ▶ grep { $_ &&
Branch: 1 branch
BranchTrueFalseTotal
$_ and not /^--/ and not /^(BEGIN|BEGIN TRANSACTION|COMMIT)/im 0 0 0
Condition0%0/3
$_ and not /^--/
!ll&&!rl&&r
000
MC/DC0%0/2
$_ and not /^--/
$_ not /^--/
Truth table0%0/3
A and not(B)
A= $_B= not /^--/
ABresult
0X0
100
111
738 !/^--/ &&
739 !/^(BEGIN|BEGIN TRANSACTION|COMMIT)/mi }
740 @data;
741  
742 0 return \@data;
743 }
744  
745 sub _source_exists
746 {
747 0 ▶ my ($self, $rs) = @_;
Subroutine _source_exists: not called CC=1 SCAR=6.9
748  
749 return try {
750 0 ▶ $rs->search(\'1=0')->cursor->next;
Subroutine __ANON__: not called CC=1 SCAR=6.9
751 0 1;
752 } catch {
753 0 ▶ 0;
Subroutine __ANON__: not called CC=1 SCAR=6.9
754 0 };
755 }
756  
757 1 1;
758  
759  
760 =head1 AUTHOR AND CONTRIBUTORS
761  
762 See L<AUTHOR|DBIx::Class/AUTHOR> and L<CONTRIBUTORS|DBIx::Class/CONTRIBUTORS> in DBIx::Class
763  
764 =head1 LICENSE
765  
766 You may distribute this code under the same terms as Perl itself.
« DBIx/Class/Storage/DBI/SQLAnywhere.pm ↑ Summary DBIx/Class/Storage/DBI/SQLite.pm »