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