less bogus output from tests
[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::Storage;   # loaded for type constraint
7 require DBIx::Class::ResultSet; # loaded for type constraint
8 use Carp::Clan '^DBIx::Class::DeploymentHandler';
9 use SQL::Translator;
10 require SQL::Translator::Diff;
11 use Try::Tiny;
12
13 with 'DBIx::Class::DeploymentHandler::WithSqltDeployMethod';
14
15 BEGIN {
16   use Moose::Util::TypeConstraints;
17   subtype 'DBIx::Class::DeploymentHandler::Databases'
18     => as 'ArrayRef[Str]';
19
20   coerce 'DBIx::Class::DeploymentHandler::Databases'
21     => from 'Str'
22     => via { [$_] };
23   no Moose::Util::TypeConstraints;
24 }
25
26 has schema => (
27   isa      => 'DBIx::Class::Schema',
28   is       => 'ro',
29   required => 1,
30   handles => [qw( ddl_filename schema_version )],
31 );
32
33 has upgrade_directory => ( # configuration
34   isa      => 'Str',
35   is       => 'ro',
36   required => 1,
37   default  => 'sql',
38 );
39
40 has backup_directory => ( # configuration
41   isa => 'Str',
42   is  => 'ro',
43   predicate  => 'has_backup_directory',
44 );
45
46 has storage => (
47   isa        => 'DBIx::Class::Storage',
48   is         => 'ro',
49   lazy_build => 1,
50 );
51
52 method _build_storage {
53   my $s = $self->schema->storage;
54   $s->_determine_driver;
55   $s
56 }
57
58 has do_backup => ( # configuration
59   isa     => 'Bool',
60   is      => 'ro',
61   default => undef,
62 );
63
64 has version_rs => (
65   isa        => 'DBIx::Class::ResultSet',
66   is         => 'ro',
67   lazy_build => 1,
68   handles    => [qw( is_installed db_version )],
69 );
70
71 method _build_version_rs {
72    $self->schema->set_us_up_the_bomb;
73    $self->schema->resultset('__VERSION')
74 }
75
76 has databases => ( # configuration
77   coerce  => 1,
78   isa     => 'DBIx::Class::DeploymentHandler::Databases',
79   is      => 'ro',
80   default => sub { [qw( MySQL SQLite PostgreSQL )] },
81 );
82
83 has sqltargs => ( # configuration
84   isa => 'HashRef',
85   is  => 'ro',
86   default => sub { {} },
87 );
88
89 method deploy {
90   my $storage  = $self->storage;
91
92   my $deploy = sub {
93     my $line = shift;
94     # the \nCOMMIT below is entirely to make the tests quieter,
95     # there is surely a better way to fix it (/m breaks everything)
96     return if(!$line || $line =~ /^(--|BEGIN TRANSACTION|\nCOMMIT|\s+$)/);
97     $storage->_query_start($line);
98     try {
99       # do a dbh_do cycle here, as we need some error checking in
100       # place (even though we will ignore errors)
101       $storage->dbh_do (sub { $_[1]->do($line) });
102     }
103     catch {
104       carp "$_ (running '${line}')"
105     }
106     $storage->_query_end($line);
107   };
108   my @statements = $self->deployment_statements;
109   if (@statements > 1) {
110     foreach my $statement (@statements) {
111       $deploy->( $statement );
112     }
113   }
114   elsif (@statements == 1) {
115     foreach my $line ( split(";\n", $statements[0])) {
116       $deploy->( $line );
117     }
118   }
119 }
120
121 method install($new_version) {
122   carp 'Install not possible as versions table already exists in database'
123     if $self->is_installed;
124
125   $new_version ||= $self->schema_version;
126
127   if ($new_version) {
128     $self->deploy;
129
130     $self->version_rs->create({
131       version     => $new_version,
132       # ddl         => $ddl,
133       # upgrade_sql => $upgrade_sql,
134     });
135   }
136 }
137
138 method ordered_schema_versions { undef }
139
140 method upgrade {
141   my $db_version     = $self->db_version;
142   my $schema_version = $self->schema_version;
143
144   unless ($db_version) {
145     # croak?
146     carp 'Upgrade not possible as database is unversioned. Please call install first.';
147     return;
148   }
149
150   if ( $db_version eq $schema_version ) {
151     # croak?
152     carp "Upgrade not necessary\n";
153     return;
154   }
155
156   my @version_list = $self->ordered_schema_versions ||
157     ( $db_version, $schema_version );
158
159   # remove all versions in list above the required version
160   while ( @version_list && ( $version_list[-1] ne $schema_version ) ) {
161     pop @version_list;
162   }
163
164   # remove all versions in list below the current version
165   while ( @version_list && ( $version_list[0] ne $db_version ) ) {
166     shift @version_list;
167   }
168
169   # check we have an appropriate list of versions
170   die if @version_list < 2;
171
172   # do sets of upgrade
173   while ( @version_list >= 2 ) {
174     $self->upgrade_single_step( $version_list[0], $version_list[1] );
175     shift @version_list;
176   }
177 }
178
179 __PACKAGE__->meta->make_immutable;
180
181 1;
182
183 __END__
184
185 vim: ts=2 sw=2 expandtab