remove dead function, fix logic error, add (for now) proxy method
[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
10 has schema => (
11   isa      => 'DBIx::Class::Schema',
12   is       => 'ro',
13   required => 1,
14   handles => [qw( schema_version )],
15 );
16
17 has upgrade_directory => (
18   isa      => 'Str',
19   is       => 'ro',
20   required => 1,
21   default  => 'upgrades',
22 );
23
24 has backup_directory => (
25   isa => 'Str',
26   is  => 'ro',
27 );
28
29 has storage => (
30   isa        => 'DBIx::Class::Storage',
31   is         => 'ro',
32   lazy_build => 1,
33 );
34
35 method _build_storage {
36   my $s = $self->schema->storage;
37   $s->_determine_driver;
38   $s
39 }
40
41 has _filedata => (
42   isa => 'Str',
43   is  => 'rw',
44 );
45
46 has do_backup => (
47   isa     => 'Bool',
48   is      => 'ro',
49   default => undef,
50 );
51
52 has do_diff_on_init => (
53   isa     => 'Bool',
54   is      => 'ro',
55   default => undef,
56 );
57
58 has version_rs => (
59   isa        => 'DBIx::Class::ResultSet',
60   is         => 'ro',
61   lazy_build => 1,
62   handles    => [qw( is_installed db_version )],
63 );
64
65 method _build_version_rs { $self->schema->resultset('VersionResult') }
66
67 method backup { $self->storage->backup($self->backup_directory) }
68
69 method create_ddl_dir { $self->storage->create_ddl_dir( $self->schema, @_ ) }
70
71 method install($new_version) {
72   carp 'Install not possible as versions table already exists in database'
73     if $self->is_installed;
74
75   $new_version ||= $self->schema_version;
76
77   if ($new_version) {
78     $self->schema->deploy;
79
80     $self->version_rs->create({
81       version     => $new_version,
82       # ddl         => $ddl,
83       # upgrade_sql => $upgrade_sql,
84     });
85   }
86 }
87
88 method create_upgrade_path { }
89
90 method ordered_schema_versions { }
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 method upgrade_single_step($db_version, $target_version) {
132   if ($db_version eq $target_version) {
133     # croak?
134     carp "Upgrade not necessary\n";
135     return;
136   }
137
138   my $upgrade_file = $self->ddl_filename(
139     $self->storage->sqlt_type,
140     $target_version,
141     $self->upgrade_directory,
142     $db_version,
143   );
144
145   $self->create_upgrade_path({ upgrade_file => $upgrade_file });
146
147   unless (-f $upgrade_file) {
148     # croak?
149     carp "Upgrade not possible, no upgrade file found ($upgrade_file), please create one\n";
150     return;
151   }
152
153   carp "DB version ($db_version) is lower than the schema version (".$self->schema_version."). Attempting upgrade.\n";
154
155   $self->_filedata($self->_read_sql_file($upgrade_file)); # I don't like this --fREW 2010-02-22
156   $self->backup if $self->do_backup;
157   $self->schema->txn_do(sub { $self->do_upgrade });
158
159   $self->version_rs->create({
160     version     => $target_version,
161     # ddl         => $ddl,
162     # upgrade_sql => $upgrade_sql,
163   });
164 }
165
166 method do_upgrade { $self->run_upgrade(qr/.*?/) }
167
168 method run_upgrade($stm) {
169   return unless $self->_filedata;
170   my @statements = grep { $_ =~ $stm } @{$self->_filedata};
171
172   for (@statements) {
173     $self->storage->debugobj->query_start($_) if $self->storage->debug;
174     $self->apply_statement($_);
175     $self->storage->debugobj->query_end($_) if $self->storage->debug;
176   }
177 }
178
179 method apply_statement($statement) {
180   # croak?
181   $self->storage->dbh->do($_) or carp "SQL was: $_"
182 }
183
184 method _read_sql_file($file) {
185   return unless $file;
186
187   open my $fh, '<', $file or carp("Can't open upgrade file, $file ($!)");
188   my @data = split /\n/, join '', <$fh>;
189   close $fh;
190
191   @data = grep {
192     $_ &&
193     !/^--/ &&
194     !/^(BEGIN|BEGIN TRANSACTION|COMMIT)/m
195   } split /;/,
196     join '', @data;
197
198   return \@data;
199 }
200
201 1;
202
203 __END__
204
205 vim: ts=2,sw=2,expandtab