split deployment method out of Deployment Handler
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler.pm
CommitLineData
b974984a 1package DBIx::Class::DeploymentHandler;
2
3use Moose;
4use Method::Signatures::Simple;
12fdd461 5require DBIx::Class::Schema; # loaded for type constraint
6require DBIx::Class::Storage; # loaded for type constraint
7require DBIx::Class::ResultSet; # loaded for type constraint
e1f67607 8use Carp::Clan '^DBIx::Class::DeploymentHandler';
ecc3b6be 9use SQL::Translator;
9e1c29c2 10require SQL::Translator::Diff;
d76abae0 11use Try::Tiny;
b974984a 12
334bced5 13with 'DBIx::Class::DeploymentHandler::WithSqltDeployMethod';
2e68a8e1 14
cf400f48 15BEGIN {
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
b974984a 26has schema => (
61847972 27 isa => 'DBIx::Class::Schema',
28 is => 'ro',
29 required => 1,
4ea147c6 30 handles => [qw( ddl_filename schema_version )],
b974984a 31);
32
33has upgrade_directory => (
61847972 34 isa => 'Str',
35 is => 'ro',
36 required => 1,
4ea147c6 37 default => 'sql',
b974984a 38);
39
40has backup_directory => (
61847972 41 isa => 'Str',
42 is => 'ro',
b974984a 43);
44
45has storage => (
61847972 46 isa => 'DBIx::Class::Storage',
47 is => 'ro',
48 lazy_build => 1,
b974984a 49);
50
7eec7eb7 51method _build_storage {
61847972 52 my $s = $self->schema->storage;
53 $s->_determine_driver;
54 $s
7eec7eb7 55}
12fdd461 56
b974984a 57has do_backup => (
61847972 58 isa => 'Bool',
59 is => 'ro',
60 default => undef,
b974984a 61);
62
63has do_diff_on_init => (
61847972 64 isa => 'Bool',
65 is => 'ro',
66 default => undef,
b974984a 67);
68
12fdd461 69has version_rs => (
61847972 70 isa => 'DBIx::Class::ResultSet',
71 is => 'ro',
72 lazy_build => 1,
73 handles => [qw( is_installed db_version )],
12fdd461 74);
75
9e1c29c2 76method _build_version_rs {
77 $self->schema->set_us_up_the_bomb;
78 $self->schema->resultset('__VERSION')
79}
80
9e401dc2 81has databases => (
cf400f48 82 coerce => 1,
83 isa => 'DBIx::Class::DeploymentHandler::Databases',
84 is => 'ro',
9e401dc2 85 default => sub { [qw( MySQL SQLite PostgreSQL )] },
86);
87
ecc3b6be 88has sqltargs => (
89 isa => 'HashRef',
90 is => 'ro',
91 default => sub { {} },
92);
93
741620a6 94method deploy {
46a47cd4 95 my $schema = $self->schema;
96 my $type = undef;
741620a6 97 my $sqltargs = $self->sqltargs;
46a47cd4 98 my $dir = $self->upgrade_directory;
99 my $storage = $self->storage;
741620a6 100
101 my $deploy = sub {
102 my $line = shift;
d76abae0 103 return if(!$line || $line =~ /^--|^BEGIN TRANSACTION|^COMMIT|^\s+$/);
741620a6 104 $storage->_query_start($line);
d76abae0 105 try {
741620a6 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) });
d76abae0 109 }
110 catch {
111 carp "$_ (running '${line}')"
741620a6 112 }
113 $storage->_query_end($line);
114 };
d76abae0 115 my @statements = $self->deployment_statements();
741620a6 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
b974984a 128method install($new_version) {
12fdd461 129 carp 'Install not possible as versions table already exists in database'
ceef4ff5 130 if $self->is_installed;
b974984a 131
12fdd461 132 $new_version ||= $self->schema_version;
b974984a 133
134 if ($new_version) {
741620a6 135 $self->deploy();
12fdd461 136
137 $self->version_rs->create({
61847972 138 version => $new_version,
139 # ddl => $ddl,
140 # upgrade_sql => $upgrade_sql,
12fdd461 141 });
b974984a 142 }
143}
144
8636376a 145method ordered_schema_versions { undef }
b974984a 146
147method upgrade {
12fdd461 148 my $db_version = $self->db_version;
149 my $schema_version = $self->schema_version;
b974984a 150
b974984a 151 unless ($db_version) {
61847972 152 # croak?
153 carp 'Upgrade not possible as database is unversioned. Please call install first.';
154 return;
b974984a 155 }
156
12fdd461 157 if ( $db_version eq $schema_version ) {
61847972 158 # croak?
159 carp "Upgrade not necessary\n";
160 return;
b974984a 161 }
162
12fdd461 163 my @version_list = $self->ordered_schema_versions ||
164 ( $db_version, $schema_version );
b974984a 165
166 # remove all versions in list above the required version
12fdd461 167 while ( @version_list && ( $version_list[-1] ne $schema_version ) ) {
61847972 168 pop @version_list;
b974984a 169 }
170
171 # remove all versions in list below the current version
12fdd461 172 while ( @version_list && ( $version_list[0] ne $db_version ) ) {
61847972 173 shift @version_list;
b974984a 174 }
175
176 # check we have an appropriate list of versions
12fdd461 177 die if @version_list < 2;
b974984a 178
179 # do sets of upgrade
12fdd461 180 while ( @version_list >= 2 ) {
61847972 181 $self->upgrade_single_step( $version_list[0], $version_list[1] );
182 shift @version_list;
b974984a 183 }
184}
185
2e68a8e1 186__PACKAGE__->meta->make_immutable;
187
b974984a 1881;
61847972 189
190__END__
191
192vim: ts=2,sw=2,expandtab