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