WithSchema.pm untested

filter: Statementstmt 0.0% 0 / 43 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 56 CC 1 SCAR 6.9
CC
1
Cov
0%
_build_version_rs
2.0
database_version
2.0
add_database_version
2.0
CRAP 2.0
1 use strict;
2 use warnings;
3 package DBIx::Class::DeploymentHandler::VersionStorage::WithSchema;
4  
5 # ABSTRACT: Version storage for DeploymentHandler that includes the schema
6  
7 use Moo;
8 use DBIx::Class::DeploymentHandler::LogImporter ':log';
9 use DBIx::Class::DeploymentHandler::VersionStorage::WithSchema::VersionResult;
10 our $VERSION = '0.004';
11  
12 has schema => (
13 is => 'ro',
14 required => 1,
15 );
16  
17 has version_rs => (
18 is => 'ro',
19 builder => '_build_version_rs',
20 handles => [qw/version_storage_is_installed/],
21 );
22  
23 with 'DBIx::Class::DeploymentHandler::HandlesVersionStorage';
24  
25 sub _build_version_rs {
26 $_[0]->schema->register_class(
27 __VERSION =>
28 'DBIx::Class::DeploymentHandler::VersionStorage::WithSchema::VersionResult'
29 );
30  
31 return $_[0]->schema->resultset('__VERSION')
32 }
33  
34 sub database_version {
35 my $self = shift;
36 my $schema = ref $self->schema;
37 $self->version_rs->search({ schema => $schema }, {
38 order_by => { -desc => 'id' },
39 rows => 1
40 })->get_column('version')->next;
41 }
42  
43 sub add_database_version {
44 my $self = shift;
45 my $version = $_[0]->{version};
46  
47 my $schema = ref $self->schema;
48  
49 log_debug { "Adding version $version to schema $schema" };
50 $self->version_rs->create({ %{ $_[0] }, schema => $schema });
51 }
52  
53 sub delete_database_version {
54 my $self = shift;
55 my $version = $_[0]->{version};
56  
57 my $schema = ref $self->schema;
58  
59 log_debug { "Deleting version $version from schema $schema" };
60 $self->version_rs->search({ version => $version, schema => $schema })->delete;
61 }
62  
63 __PACKAGE__->meta->make_immutable;
64  
65 1;
66  
67 =head1 DESCRIPTION
68  
69 The standard DeploymentHandler storage only stores the version of the schema.
70 This means you can't deploy multiple DH-handled DBIC schemata into the same
71 database.
72  
73 This module has an extra column to store the schema that it is deploying as well
74 as the version.
75  
76 =head1 SYNOPSIS
77  
78 To use it, you'll have to create a new subclass of
79 L<DBIx::Class::DeploymentHandler::Dad> that uses this module instead of the
80 standard one, and instantiate that in your script instead.
81  
82 # See t/lib/Dad.pm for a working example
83 package My::DH::Dad;
84  
85 use Moose;
86 extends 'DBIx::Class::DeploymentHandler::Dad';
87  
88 with 'DBIx::Class::DeploymentHandler::WithApplicatorDumple' => {
89 ...
90 'DBIx::Class::DeploymentHandler::WithApplicatorDumple' => {
91 interface_role => 'DBIx::Class::DeploymentHandler::HandlesVersionStorage',
92 class_name => 'DBIx::Class::DeploymentHandler::VersionStorage::WithSchema',
93 delegate_name => 'version_storage',
94 attributes_to_assume => ['schema'],
95 };
96  
97 # in a script ...
98 my $handler = My::DH::Dad->new( ... );
99  
100 # For the "core" schema (see below)
101 $handler->prepare_install( core => 1 );
102 $handler->install;
103  
104 # For modules
105 $handler->prepare_install;
106 my $ddl = $handler->deploy;
107 $handler->add_schema_version({
108 ddl => $ddl,
109 version => $schema->to_version
110 });
111  
112 The original idea behind this module is that you could merge any number of
113 schemata together to create one big one, and DH would keep track of the version
114 of each schema. However, DH won't support that easily, so instead, you'll have
115 to have a single "core" module, and any number of modular schemata installed
116 into it.
117  
118 The reason for this is that DH tries to install a version table for every schema
119 and we can't avoid it. To bypass this problem we define a I<core> schema, which
120 contains the C<version> table, and other schemata that don't.
121  
122 When you prepare the installation files for your core schema you should tell
123 your Dad subclass that you are doing so. This will usually only happen once
124 ever, because once you've created your core schema's first version you're
125 golden. This is the only time you will I<install> a schema.
126  
127 $handler->prepare_install( core => 1 );
128 $handler->install;
129  
130 When you prepare the installation files for a module schema, you run
131 C<prepare_install> without that extra argument.
132  
133 $handler->prepare_install;
134  
135 This ensures that the first version of your module doesn't contain a
136 C<__VERSION> SQL file, and thus it won't immediately crash when you deploy it.
137 Speaking of deploy, you can't C<install> this schema because DH will die. You
138 have to C<deploy> it.
139  
140 $handler->deploy;
141  
142 When you deploy a schema like this, DH doesn't record the version, so you have
143 to do that yourself. You can get the DDL out of the deploy step.
144  
145 my $ddl = $handler->deploy;
146 $handler->add_schema_version({
147 ddl => $ddl,
148 version => $schema->to_version
149 });
150  
151 =head1 SEE ALSO
152  
153 This module implements L<DBIx::Class::DeploymentHandler::HandlesVersionStorage>
154 and is similar to L<DBIx::Class::DeploymentHandler::VersionStorage::Standard>.