fix POD bug from RT#79301
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler / Cookbook / CustomResultSource.pod
1 package DBIx::Class::DeploymentHandler::Cookbook::CustomResultSource;
2
3 =pod
4
5 =head1 NAME
6
7 DBIx::Class::DeploymentHandler::Cookbook::CustomResultSource - Customize how
8 your DBICDH versions are stored
9
10 =head1 DESCRIPTION
11
12 One of the reasons for the absurd level of flexibility that
13 L<DBIx::Class::DeploymentHandler> is so that you can do things that we did not
14 originally anticipate.  Surprisingly, I never added a method to change the
15 table for the version storage.  That's fine though, the following recipe
16 shows how one can do it in style:
17
18 =head2 Version Storage
19
20  package MyApp::Schema::DBICDHStorage;
21  use Moose;
22  extends 'DBIx::Class::DeploymentHandler::VersionStorage::Standard';
23
24  sub _build_version_rs {
25    $_[0]->schema->register_class(
26      __VERSION =>
27        'MyApp::Schema::DBICDHStorageResult'
28    );
29    $_[0]->schema->resultset('__VERSION')
30  }
31
32  no Moose;
33  __PACKAGE__->meta->make_immutable;
34  1;
35
36 There's not a whole lot special there.  The only real bit of code to point out
37 is the C<register_class> call.  We make sure to point C<__VERSION> to the
38 result class that we will define next.
39
40 =head2 Version Result Class
41
42  package MyApp::Schema::DBICDHStorageResult;
43  use parent 'DBIx::Class::DeploymentHandler::VersionStorage::Standard::VersionResult';
44  __PACKAGE__->table('fl_bench_journal_versions');
45  1;
46
47 As you can see, this is almost silly how simple it is, we just change the
48 table being set on the original result.
49
50 =head2 Our very own DeploymentHandler
51
52  package MyApp::Schema::DeploymentHandler;
53  use Moose;
54  extends 'DBIx::Class::DeploymentHandler::Dad';
55
56  # a single with would be better, but we can't do that
57  # see: http://rt.cpan.org/Public/Bug/Display.html?id=46347
58  with 'DBIx::Class::DeploymentHandler::WithApplicatorDumple' => {
59      interface_role       => 'DBIx::Class::DeploymentHandler::HandlesDeploy',
60      class_name           => 'DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator',
61      delegate_name        => 'deploy_method',
62      attributes_to_assume => ['schema'],
63      attributes_to_copy   => [qw( databases script_directory sql_translator_args )],
64    },
65    'DBIx::Class::DeploymentHandler::WithApplicatorDumple' => {
66      interface_role       => 'DBIx::Class::DeploymentHandler::HandlesVersioning',
67      class_name           => 'DBIx::Class::DeploymentHandler::VersionHandler::Monotonic',
68      delegate_name        => 'version_handler',
69      attributes_to_assume => [qw( database_version schema_version to_version )],
70    },
71    'DBIx::Class::DeploymentHandler::WithApplicatorDumple' => {
72      interface_role       => 'DBIx::Class::DeploymentHandler::HandlesVersionStorage',
73      class_name           => 'MyApp::Schema::DBICDHStorage',
74      delegate_name        => 'version_storage',
75      attributes_to_assume => ['schema'],
76    };
77  with 'DBIx::Class::DeploymentHandler::WithReasonableDefaults';
78
79  sub prepare_version_storage_install {
80    my $self = shift;
81
82    $self->prepare_resultsource_install({
83        result_source => $self->version_storage->version_rs->result_source
84    });
85  }
86
87  sub install_version_storage {
88    my $self = shift;
89
90    my $version = (shift || {})->{version} || $self->schema_version;
91    $self->install_resultsource({
92        result_source => $self->version_storage->version_rs->result_source,
93        version       => $version,
94    });
95  }
96
97  sub prepare_install {
98     $_[0]->prepare_deploy;
99     $_[0]->prepare_version_storage_install;
100  }
101
102  no Moose;
103  __PACKAGE__->meta->make_immutable;
104  1;
105
106 Note: if you are using decimal numbers for versioning, you should ammend
107 this DeploymentHandler package, setting it's VersionHandler class_name from
108 Monotonic ( which handles integer only version numbers ) to ExplicitVersions
109 or DatabaseToSchemaVersions, as these handle version numbers as strings
110 instead of integers.
111