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