c67fb920aa784eedbdd116f019bdcecaa055195e
[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 => (
34   isa      => 'Str',
35   is       => 'ro',
36   required => 1,
37   default  => 'sql',
38 );
39
40 has backup_directory => (
41   isa => 'Str',
42   is  => 'ro',
43 );
44
45 has storage => (
46   isa        => 'DBIx::Class::Storage',
47   is         => 'ro',
48   lazy_build => 1,
49 );
50
51 method _build_storage {
52   my $s = $self->schema->storage;
53   $s->_determine_driver;
54   $s
55 }
56
57 has do_backup => (
58   isa     => 'Bool',
59   is      => 'ro',
60   default => undef,
61 );
62
63 has do_diff_on_init => (
64   isa     => 'Bool',
65   is      => 'ro',
66   default => undef,
67 );
68
69 has version_rs => (
70   isa        => 'DBIx::Class::ResultSet',
71   is         => 'ro',
72   lazy_build => 1,
73   handles    => [qw( is_installed db_version )],
74 );
75
76 method _build_version_rs {
77    $self->schema->set_us_up_the_bomb;
78    $self->schema->resultset('__VERSION')
79 }
80
81 has databases => (
82   coerce  => 1,
83   isa     => 'DBIx::Class::DeploymentHandler::Databases',
84   is      => 'ro',
85   default => sub { [qw( MySQL SQLite PostgreSQL )] },
86 );
87
88 has sqltargs => (
89   isa => 'HashRef',
90   is  => 'ro',
91   default => sub { {} },
92 );
93
94 method deploy {
95   my $schema   = $self->schema;
96   my $type     = undef;
97   my $sqltargs = $self->sqltargs;
98   my $dir      = $self->upgrade_directory;
99   my $storage  = $self->storage;
100
101   my $deploy = sub {
102     my $line = shift;
103     return if(!$line || $line =~ /^--|^BEGIN TRANSACTION|^COMMIT|^\s+$/);
104     $storage->_query_start($line);
105     try {
106       # do a dbh_do cycle here, as we need some error checking in
107       # place (even though we will ignore errors)
108       $storage->dbh_do (sub { $_[1]->do($line) });
109     }
110     catch {
111       carp "$_ (running '${line}')"
112     }
113     $storage->_query_end($line);
114   };
115   my @statements = $self->deployment_statements();
116   if (@statements > 1) {
117     foreach my $statement (@statements) {
118       $deploy->( $statement );
119     }
120   }
121   elsif (@statements == 1) {
122     foreach my $line ( split(";\n", $statements[0])) {
123       $deploy->( $line );
124     }
125   }
126 }
127
128 method install($new_version) {
129   carp 'Install not possible as versions table already exists in database'
130     if $self->is_installed;
131
132   $new_version ||= $self->schema_version;
133
134   if ($new_version) {
135     $self->deploy();
136
137     $self->version_rs->create({
138       version     => $new_version,
139       # ddl         => $ddl,
140       # upgrade_sql => $upgrade_sql,
141     });
142   }
143 }
144
145 method ordered_schema_versions { undef }
146
147 method upgrade {
148   my $db_version     = $self->db_version;
149   my $schema_version = $self->schema_version;
150
151   unless ($db_version) {
152     # croak?
153     carp 'Upgrade not possible as database is unversioned. Please call install first.';
154     return;
155   }
156
157   if ( $db_version eq $schema_version ) {
158     # croak?
159     carp "Upgrade not necessary\n";
160     return;
161   }
162
163   my @version_list = $self->ordered_schema_versions ||
164     ( $db_version, $schema_version );
165
166   # remove all versions in list above the required version
167   while ( @version_list && ( $version_list[-1] ne $schema_version ) ) {
168     pop @version_list;
169   }
170
171   # remove all versions in list below the current version
172   while ( @version_list && ( $version_list[0] ne $db_version ) ) {
173     shift @version_list;
174   }
175
176   # check we have an appropriate list of versions
177   die if @version_list < 2;
178
179   # do sets of upgrade
180   while ( @version_list >= 2 ) {
181     $self->upgrade_single_step( $version_list[0], $version_list[1] );
182     shift @version_list;
183   }
184 }
185
186 __PACKAGE__->meta->make_immutable;
187
188 1;
189
190 __END__
191
192 vim: ts=2,sw=2,expandtab