move some general comments to a general role, along with the interface, and initial...
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler.pm
CommitLineData
b974984a 1package DBIx::Class::DeploymentHandler;
2
3use Moose;
4use Method::Signatures::Simple;
12fdd461 5require DBIx::Class::Schema; # loaded for type constraint
12fdd461 6require DBIx::Class::ResultSet; # loaded for type constraint
e1f67607 7use Carp::Clan '^DBIx::Class::DeploymentHandler';
b974984a 8
334bced5 9with 'DBIx::Class::DeploymentHandler::WithSqltDeployMethod';
e70a1600 10with 'DBIx::Class::DeploymentHandler::WithDatabaseToSchemaVersions';
2e68a8e1 11
cf400f48 12BEGIN {
13 use Moose::Util::TypeConstraints;
14 subtype 'DBIx::Class::DeploymentHandler::Databases'
15 => as 'ArrayRef[Str]';
16
17 coerce 'DBIx::Class::DeploymentHandler::Databases'
18 => from 'Str'
19 => via { [$_] };
20 no Moose::Util::TypeConstraints;
21}
22
b974984a 23has schema => (
61847972 24 isa => 'DBIx::Class::Schema',
25 is => 'ro',
26 required => 1,
4ea147c6 27 handles => [qw( ddl_filename schema_version )],
b974984a 28);
29
d3b45f46 30has upgrade_directory => ( # configuration
61847972 31 isa => 'Str',
32 is => 'ro',
33 required => 1,
4ea147c6 34 default => 'sql',
b974984a 35);
36
d3b45f46 37has backup_directory => ( # configuration
61847972 38 isa => 'Str',
39 is => 'ro',
8bf3eee1 40 predicate => 'has_backup_directory',
b974984a 41);
42
d3b45f46 43has do_backup => ( # configuration
61847972 44 isa => 'Bool',
45 is => 'ro',
46 default => undef,
b974984a 47);
48
12fdd461 49has version_rs => (
61847972 50 isa => 'DBIx::Class::ResultSet',
51 is => 'ro',
52 lazy_build => 1,
53 handles => [qw( is_installed db_version )],
12fdd461 54);
55
9e1c29c2 56method _build_version_rs {
57 $self->schema->set_us_up_the_bomb;
58 $self->schema->resultset('__VERSION')
59}
60
d3b45f46 61has databases => ( # configuration
cf400f48 62 coerce => 1,
63 isa => 'DBIx::Class::DeploymentHandler::Databases',
64 is => 'ro',
9e401dc2 65 default => sub { [qw( MySQL SQLite PostgreSQL )] },
66);
67
d3b45f46 68has sqltargs => ( # configuration
ecc3b6be 69 isa => 'HashRef',
70 is => 'ro',
71 default => sub { {} },
72);
73
b974984a 74method install($new_version) {
12fdd461 75 carp 'Install not possible as versions table already exists in database'
ceef4ff5 76 if $self->is_installed;
b974984a 77
12fdd461 78 $new_version ||= $self->schema_version;
b974984a 79
80 if ($new_version) {
d3b45f46 81 $self->deploy;
12fdd461 82
83 $self->version_rs->create({
61847972 84 version => $new_version,
85 # ddl => $ddl,
86 # upgrade_sql => $upgrade_sql,
12fdd461 87 });
b974984a 88 }
89}
90
b974984a 91method upgrade {
12fdd461 92 my $db_version = $self->db_version;
93 my $schema_version = $self->schema_version;
b974984a 94
b974984a 95 unless ($db_version) {
61847972 96 # croak?
97 carp 'Upgrade not possible as database is unversioned. Please call install first.';
98 return;
b974984a 99 }
100
12fdd461 101 if ( $db_version eq $schema_version ) {
61847972 102 # croak?
103 carp "Upgrade not necessary\n";
104 return;
b974984a 105 }
106
e70a1600 107 my @version_list = $self->ordered_schema_versions;
b974984a 108
109 # remove all versions in list above the required version
12fdd461 110 while ( @version_list && ( $version_list[-1] ne $schema_version ) ) {
61847972 111 pop @version_list;
b974984a 112 }
113
114 # remove all versions in list below the current version
12fdd461 115 while ( @version_list && ( $version_list[0] ne $db_version ) ) {
61847972 116 shift @version_list;
b974984a 117 }
118
119 # check we have an appropriate list of versions
12fdd461 120 die if @version_list < 2;
b974984a 121
122 # do sets of upgrade
12fdd461 123 while ( @version_list >= 2 ) {
61847972 124 $self->upgrade_single_step( $version_list[0], $version_list[1] );
125 shift @version_list;
b974984a 126 }
127}
128
2e68a8e1 129__PACKAGE__->meta->make_immutable;
130
b974984a 1311;
61847972 132
133__END__
134
2eaf903b 135vim: ts=2 sw=2 expandtab