File Coverage

blib/lib/DBIx/Class/DeploymentHandler/VersionStorage/WithSchema.pm
Criterion Covered Total %
statement 23 33 69.7
branch n/a
condition n/a
subroutine 7 11 63.6
pod 0 3 0.0
total 30 47 63.8


line stmt bran cond sub pod time code
1 1     1   792003 use strict;
  1         8  
  1         33  
2 1     1   6 use warnings;
  1         2  
  1         60  
3             package DBIx::Class::DeploymentHandler::VersionStorage::WithSchema;
4              
5             # ABSTRACT: Version storage for DeploymentHandler that includes the schema
6              
7 1     1   7 use Moo;
  1         2  
  1         7  
8 1     1   387 use DBIx::Class::DeploymentHandler::LogImporter ':log';
  1         2  
  1         13  
9 1     1   3066 use DBIx::Class::DeploymentHandler::VersionStorage::WithSchema::VersionResult;
  1         5  
  1         460  
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 2     2   363392 $_[0]->schema->register_class(
27             __VERSION =>
28             'DBIx::Class::DeploymentHandler::VersionStorage::WithSchema::VersionResult'
29             );
30              
31 2         1045 return $_[0]->schema->resultset('__VERSION')
32             }
33              
34             sub database_version {
35 0     0 0 0 my $self = shift;
36 0         0 my $schema = ref $self->schema;
37 0         0 $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 2     2 0 13697 my $self = shift;
45 2         5 my $version = $_[0]->{version};
46              
47 2         11 my $schema = ref $self->schema;
48              
49 2     0   24 log_debug { "Adding version $version to schema $schema" };
  0         0  
50 2         168 $self->version_rs->create({ %{ $_[0] }, schema => $schema });
  2         22  
51             }
52              
53             sub delete_database_version {
54 0     0 0   my $self = shift;
55 0           my $version = $_[0]->{version};
56              
57 0           my $schema = ref $self->schema;
58              
59 0     0     log_debug { "Deleting version $version from schema $schema" };
  0            
60 0           $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 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 schema, which
120             contains the C 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 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 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 this schema because DH will die. You
138             have to C 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
154             and is similar to L.