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