638f81332b08a8ece3ba394f47e779f5336a3239
[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  
22  # the following is necessary for some setups
23  use MyApp::Schema::DBICDHStorageResult;
24  
25  use Moose;
26  extends 'DBIx::Class::DeploymentHandler::VersionStorage::Standard';
27
28  sub _build_version_rs {
29    $_[0]->schema->register_class(
30      __VERSION =>
31        'MyApp::Schema::DBICDHStorageResult'
32    );
33    $_[0]->schema->resultset('__VERSION')
34  }
35
36  no Moose;
37  __PACKAGE__->meta->make_immutable;
38  1;
39
40 There's not a whole lot special there.  The only real bit of code to point out
41 is the C<register_class> call.  We make sure to point C<__VERSION> to the
42 result class that we will define next.
43
44 =head2 Version Result Class
45
46  package MyApp::Schema::DBICDHStorageResult;
47  use parent 'DBIx::Class::DeploymentHandler::VersionStorage::Standard::VersionResult';
48  __PACKAGE__->table('fl_bench_journal_versions');
49  1;
50
51 As you can see, this is almost silly how simple it is, we just change the
52 table being set on the original result.
53
54 =head2 Our very own DeploymentHandler
55
56  package MyApp::Schema::DeploymentHandler;
57  use Moose;
58  extends 'DBIx::Class::DeploymentHandler::Dad';
59
60  # a single with would be better, but we can't do that
61  # see: http://rt.cpan.org/Public/Bug/Display.html?id=46347
62  with 'DBIx::Class::DeploymentHandler::WithApplicatorDumple' => {
63      interface_role       => 'DBIx::Class::DeploymentHandler::HandlesDeploy',
64      class_name           => 'DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator',
65      delegate_name        => 'deploy_method',
66      attributes_to_assume => ['schema'],
67      attributes_to_copy   => [qw( databases script_directory sql_translator_args )],
68    },
69    'DBIx::Class::DeploymentHandler::WithApplicatorDumple' => {
70      interface_role       => 'DBIx::Class::DeploymentHandler::HandlesVersioning',
71      class_name           => 'DBIx::Class::DeploymentHandler::VersionHandler::Monotonic',
72      delegate_name        => 'version_handler',
73      attributes_to_assume => [qw( database_version schema_version to_version )],
74    },
75    'DBIx::Class::DeploymentHandler::WithApplicatorDumple' => {
76      interface_role       => 'DBIx::Class::DeploymentHandler::HandlesVersionStorage',
77      class_name           => 'MyApp::Schema::DBICDHStorage',
78      delegate_name        => 'version_storage',
79      attributes_to_assume => ['schema'],
80    };
81  with 'DBIx::Class::DeploymentHandler::WithReasonableDefaults';
82
83  sub prepare_version_storage_install {
84    my $self = shift;
85
86    $self->prepare_resultsource_install({
87        result_source => $self->version_storage->version_rs->result_source
88    });
89  }
90
91  sub install_version_storage {
92    my $self = shift;
93
94    my $version = (shift || {})->{version} || $self->schema_version;
95    $self->install_resultsource({
96        result_source => $self->version_storage->version_rs->result_source,
97        version       => $version,
98    });
99  }
100
101  sub prepare_install {
102     $_[0]->prepare_deploy;
103     $_[0]->prepare_version_storage_install;
104  }
105
106  no Moose;
107  __PACKAGE__->meta->make_immutable;
108  1;
109
110 Note: if you are using decimal numbers for versioning, you should ammend
111 this DeploymentHandler package, setting it's VersionHandler class_name from
112 Monotonic ( which handles integer only version numbers ) to ExplicitVersions
113 or DatabaseToSchemaVersions, as these handle version numbers as strings
114 instead of integers.
115