WithSchema.pm

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